secgateway/Platform/user/ulog/ulog-api/ulog_api.c

52 lines
1.1 KiB
C
Executable File

#include <stdio.h>
#include "common_types.h"
#include "ulog_api.h"
#define LOG_MSG_SZ 1024
ulog_t *ulog_init(const char *module_name)
{
ulog_t *log;
u32 len = sizeof(module_name);
if (len > MAX_MODULE_NAME_SZ) {
fprintf(stderr, "The length:%d of module_name can't more than %d", len, MAX_MODULE_NAME_SZ);
return NULL;
}
log = (ulog_t *)malloc(sizeof(*log));
if (log == NULL) {
fprintf(stderr, "Allocating log memory is failure");
return NULL;
}
strncpy(log->module_name, module_name, len);
return log;
}
void ulog_close(ulog_t *log)
{
if (log == NULL) {
return;
}
free(log);
}
void ulog_record(const ulog_t *log, int level, const char *fmt, ...)
{
if (log == NULL) {
perror("Log is null");
return;
}
char log_buf[LOG_MSG_SZ];
va_list args;
va_start(args, fmt);
vsnprintf(log_buf, sizeof(log_buf), log_buf, args);
va_end(args);
syslog(level, "[%s] %s", log->module_name, log_buf);
}