hunting/utils/LogC/src/logUb.c
2023-08-15 00:06:04 +08:00

89 lines
2.1 KiB
C

#include "logUb.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
static const char *LogLevelStr[] = {
"VERBOSE",
"DEBUG",
"INFO",
"WARR",
"ERROR",
"TRACE",
"TEST_TIPS",
};
void ub_log_free(ILog *impl)
{
if (impl != NULL) {
free(impl);
impl = NULL;
}
}
#define LOG_BUFF_SIZE (256)
#define LOG_TIME_BUF_SIZE (32)
static void ub_log_fmt(ILog *log, const LogLeveL level, const char *fmt, ...)
{
time_t rawtime;
time(&rawtime);
char ubt_time_buf[LOG_TIME_BUF_SIZE] = {0};
struct tm *info = localtime(&rawtime);
strftime(ubt_time_buf, LOG_TIME_BUF_SIZE, "%y-%m-%d %H:%M:%S", info);
va_list args;
size_t length;
static char ubt_log_buf[LOG_BUFF_SIZE];
va_start(args, fmt);
length = vsnprintf(ubt_log_buf, sizeof(ubt_log_buf) - 1, fmt, args);
if (length > LOG_BUFF_SIZE - 1)
length = LOG_BUFF_SIZE - 1;
printf("[%s][%s] %s\n", ubt_time_buf, LogLevelStr[level], ubt_log_buf);
va_end(args);
}
RETURN_CODE_C ub_log_init(ILog *impl)
{
return CreateReturnCode(C_RETURN_CODE_OK);
}
RETURN_CODE_C ub_log_unInit(ILog *impl)
{
return CreateReturnCode(C_RETURN_CODE_OK);
}
void init_Log_Ubuntu(LogUbuntu *lu)
{
((ILog*)lu)->log_fmt = ub_log_fmt;
((ILog*)lu)->init = ub_log_init;
((ILog*)lu)->free = ub_log_free;
((ILog*)lu)->unInit = ub_log_unInit;
}
RETURN_CODE_C new_log(LogUbuntu **log_ub)
{
if (!log_ub) {
printf("C_RETURN_CODE_INVALID_PARAMENTER\n");
return CreateReturnCode(C_RETURN_CODE_INVALID_PARAMENTER);
}
// log_ub为NULL
if (!(*log_ub)) {
*log_ub = (LogUbuntu *)malloc(sizeof(LogUbuntu));
// malloc success
if (*log_ub) {
printf("new_log succeed.\n");
new_i_Log((ILog **)log_ub);
init_Log_Ubuntu(*log_ub);
return CreateReturnCode(C_RETURN_CODE_OK);
}
// malloc failed
// init_Log_Ubuntu(*log_ub);
printf("new_log failed.\n");
return CreateReturnCode(C_RETURN_CODE_NOT_OK);
}
// log_ub 不为NULL
init_Log_Ubuntu(*log_ub);
return CreateReturnCode(C_RETURN_CODE_OK);
}