hunting/utils/LogC/src/logUb.c
2023-08-13 01:28:54 +08:00

70 lines
1.5 KiB
C

#include "logUb.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <time.h>
static const char *LogLevelStr[] = {
"INFO",
"Debug",
"ERROR",
"WARR"
};
LogUbuntu *new_Log_Ubuntu(void)
{
LogUbuntu *impl = (LogUbuntu *)malloc(sizeof(LogUbuntu));
if (impl) {
printf("new_log_ubuntu succeed.\n");
init_Log_Ubuntu(impl);
return impl;
}
printf("new_log_ubuntu failed.\n");
return NULL;
}
void del_Log_Ubuntu(LogUbuntu *impl)
{
if (impl != NULL) {
free(impl);
setILog(NULL);
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);
}
void ub_log_init(ILog *impl)
{
}
void ub_log_unInit(ILog *impl)
{
}
void init_Log_Ubuntu(LogUbuntu *lu)
{
((ILog*)lu)->log_fmt = ub_log_fmt;
((ILog*)lu)->init = ub_log_init;
((ILog*)lu)->unInit = ub_log_unInit;
}