242 lines
5.6 KiB
C
242 lines
5.6 KiB
C
#include "MESA_handle_logger.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdarg.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include<sys/stat.h>
|
|
|
|
#define LOGMSG_MAX_LEN 4096
|
|
#define FLUSH_LOG_NUM 4096
|
|
typedef struct log_handle_t
|
|
{
|
|
int runtime_log_level;
|
|
int flush_log_count;
|
|
FILE *fp;
|
|
char runtime_log_file[1200];
|
|
char cur_log_file[LOGMSG_MAX_LEN];
|
|
} log_handle_t;
|
|
|
|
|
|
|
|
//const int HANDLE_LOGGER_VERSION_20190218 = 1;
|
|
|
|
#define GIT_VERSION_CATTER(v) __attribute__((__used__)) const char * GIT_VERSION_##v = NULL
|
|
#define GIT_VERSION_EXPEND(v) GIT_VERSION_CATTER(v)
|
|
|
|
#ifdef __cplusplus
|
|
extern "C"
|
|
{
|
|
#endif
|
|
|
|
/* VERSION TAG */
|
|
#ifdef GIT_VERSION
|
|
GIT_VERSION_EXPEND(GIT_VERSION);
|
|
#else
|
|
static __attribute__((__used__)) const char * GIT_VERSION_UNKNOWN = NULL;
|
|
#endif
|
|
#undef GIT_VERSION_CATTER
|
|
#undef GIT_VERSION_EXPEND
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
|
|
static unsigned char weekday_str[7][4] =
|
|
{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
|
|
|
|
static unsigned char month_str[12][4] = {"Jan", "Feb", "Mar", "Apr",
|
|
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
|
|
|
|
static int create_dir(const char *dir_path, int path_len)
|
|
{
|
|
if(dir_path == NULL)
|
|
return -1;
|
|
|
|
char *buf = (char *)calloc(path_len+1, 1);
|
|
int ret = -1;
|
|
|
|
memcpy(buf, dir_path, path_len);
|
|
if(access(buf, R_OK) != 0)
|
|
{
|
|
if(mkdir(buf, 0755)!= 0)
|
|
ret = -1;
|
|
else
|
|
ret = 0;
|
|
}
|
|
else
|
|
ret = 1;
|
|
free(buf);
|
|
buf = NULL;
|
|
return ret;
|
|
}
|
|
|
|
static int create_path(const char *path, int path_len)
|
|
{
|
|
const char *p_cur = path;
|
|
int i = 0;
|
|
|
|
if(create_dir(path, path_len) >= 0)
|
|
return 0;
|
|
|
|
for(;i<=path_len;i++,p_cur++)
|
|
{
|
|
if(*p_cur == '/')
|
|
{
|
|
if(create_dir(path, i+1) < 0)
|
|
return -1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void *MESA_create_runtime_log_handle(const char *file_path, int level)
|
|
{
|
|
if(file_path == NULL)
|
|
return NULL;
|
|
|
|
FILE *fp = NULL;
|
|
log_handle_t *p_handle = NULL;
|
|
|
|
//creating file_path failed, return NULL
|
|
char *p_path = rindex(file_path, '/');
|
|
if(p_path != 0)
|
|
{
|
|
if(create_path(file_path, p_path - file_path) < 0)
|
|
return NULL;
|
|
}
|
|
if(NULL == (fp = fopen(file_path, "w")))
|
|
return NULL;
|
|
|
|
//fclose(fp);
|
|
//remove(file_path);
|
|
p_handle = (log_handle_t *)calloc(sizeof(log_handle_t), 1);
|
|
|
|
if(p_handle == NULL)
|
|
return NULL;
|
|
|
|
strncpy(p_handle->runtime_log_file, file_path, 1024);
|
|
p_handle->runtime_log_file[1024] = '\0';
|
|
p_handle->runtime_log_level = level;
|
|
//p_handle->fp = fp;
|
|
return (void *)p_handle;
|
|
}
|
|
|
|
void MESA_destroy_runtime_log_handle(void *handle)
|
|
{
|
|
if(handle != NULL)
|
|
{
|
|
log_handle_t *p_handle = (log_handle_t *)handle;
|
|
if (p_handle->fp != NULL)
|
|
{
|
|
fclose(p_handle->fp);
|
|
p_handle->fp = NULL;
|
|
}
|
|
free(handle);
|
|
handle = NULL;
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
void MESA_handle_runtime_log(void *handle, int level, const char *module, const char *fmt, ...)
|
|
{
|
|
char buf[LOGMSG_MAX_LEN + 1];
|
|
time_t t;
|
|
int len;
|
|
va_list ap;
|
|
FILE *fp;
|
|
struct tm local_time;
|
|
char tmp_log_file_name[1400];
|
|
log_handle_t *p_handle = (log_handle_t *)handle;
|
|
|
|
if(p_handle == NULL || p_handle->runtime_log_file == NULL)return;
|
|
|
|
if(level < p_handle->runtime_log_level) return;
|
|
|
|
time(&t);
|
|
if(NULL == (localtime_r(&t, &local_time))) return;
|
|
len = snprintf(buf, sizeof(buf), "%s %s %d %02d:%02d:%02d %d", weekday_str[local_time.tm_wday],
|
|
month_str[local_time.tm_mon], local_time.tm_mday, local_time.tm_hour, local_time.tm_min, local_time.tm_sec, local_time.tm_year+1900);
|
|
//len = strlen(buf);
|
|
|
|
switch(level)
|
|
{
|
|
case RLOG_LV_DEBUG:
|
|
len += snprintf(buf + len,
|
|
LOGMSG_MAX_LEN - len, ", %s, ", "DEBUG");
|
|
break;
|
|
|
|
case RLOG_LV_INFO:
|
|
len += snprintf(buf + len,
|
|
LOGMSG_MAX_LEN - len, ", %s, ", "INFO");
|
|
break;
|
|
|
|
case RLOG_LV_FATAL:
|
|
len += snprintf(buf + len,
|
|
LOGMSG_MAX_LEN - len, ", %s, ", "FATAL");
|
|
break;
|
|
|
|
default:
|
|
len += snprintf(buf + len,
|
|
LOGMSG_MAX_LEN - len, ", %s, ", "UNKNOWN");
|
|
break;
|
|
}
|
|
|
|
if(0 >= LOGMSG_MAX_LEN - len) return;
|
|
|
|
len += snprintf(buf + len, LOGMSG_MAX_LEN - len, "%s, ", module);
|
|
|
|
if(0 >= LOGMSG_MAX_LEN - len) return;
|
|
|
|
va_start(ap, fmt);
|
|
len += vsnprintf(buf + len, LOGMSG_MAX_LEN - len, fmt, ap);
|
|
va_end(ap);
|
|
|
|
if(0 >= LOGMSG_MAX_LEN - len) return;
|
|
|
|
len += snprintf(buf + len, LOGMSG_MAX_LEN - len, "\n");
|
|
|
|
|
|
sprintf(tmp_log_file_name, "%s.%04d-%02d-%02d", p_handle->runtime_log_file,
|
|
local_time.tm_year + 1900, local_time.tm_mon + 1,
|
|
local_time.tm_mday);
|
|
|
|
OPEN_LOG_FILE:
|
|
if(p_handle->fp == NULL)
|
|
{
|
|
if(NULL == (fp = fopen(tmp_log_file_name, "a"))) return;
|
|
p_handle->fp = fp;
|
|
p_handle->flush_log_count = 0;
|
|
memcpy(p_handle->cur_log_file, tmp_log_file_name, strlen(tmp_log_file_name));
|
|
}
|
|
|
|
if (0 != memcmp(tmp_log_file_name, p_handle->cur_log_file, strlen(tmp_log_file_name)))
|
|
{
|
|
fclose(p_handle->fp);
|
|
p_handle->fp = NULL;
|
|
goto OPEN_LOG_FILE;
|
|
}
|
|
|
|
if (0 > fprintf(p_handle->fp, "%s", buf))
|
|
{
|
|
fclose(p_handle->fp);
|
|
p_handle->fp = NULL;
|
|
}
|
|
else
|
|
{
|
|
p_handle->flush_log_count+=1;
|
|
if (p_handle->flush_log_count >= FLUSH_LOG_NUM)
|
|
{
|
|
fflush(p_handle->fp);
|
|
p_handle->flush_log_count = 0;
|
|
}
|
|
}
|
|
|
|
return;
|
|
}
|
|
|