111 lines
3.1 KiB
C++
Raw Normal View History

2021-09-18 13:45:24 +08:00
#include "SH_log.h"
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <stdarg.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>
2024-07-09 09:49:42 +08:00
#include "../common/SH_global.h"
#include "../common/SH_CommonFunc.hpp"
2021-09-18 13:45:24 +08:00
struct _LogParam {
pthread_mutex_t *m_mutex;
int m_limit_size;
int m_log_buf_size;
int m_today;
FILE *m_fp;
char m_log_full_name[256];
char *m_buf;
} gLogParam;
int log_init(const char* file_name, int log_buf_size, int limit_size)
{
memset(gLogParam.m_log_full_name, 0, 256);
gLogParam.m_mutex = new pthread_mutex_t;
pthread_mutex_init(gLogParam.m_mutex, NULL);
gLogParam.m_log_buf_size = log_buf_size;
gLogParam.m_buf = new char[gLogParam.m_log_buf_size];
memset(gLogParam.m_buf, '\0', sizeof(char) * gLogParam.m_log_buf_size);
gLogParam.m_limit_size = limit_size;
struct timeval now = {0,0};
gettimeofday(&now, NULL);
2024-07-09 09:49:42 +08:00
time_t t ;
t = now.tv_sec;
struct tm* sys_tm = localtime(&t);
char fileTime[20]={0x00};
char fileName[100]={0x00};
int n = snprintf(fileTime, 64, "%d-%02d-%02d.log", sys_tm->tm_year+1900, sys_tm->tm_mon+1, sys_tm->tm_mday);
sprintf(fileName,"%s%s",file_name,fileTime);
strcpy(gLogParam.m_log_full_name, fileName);
2021-09-18 13:45:24 +08:00
return 0;
}
void log_write(const char *level, const char* format, ...)
{
2024-07-09 09:49:42 +08:00
struct timeval now = {0,0};
gettimeofday(&now, NULL);
time_t t;
t = now.tv_sec;
struct tm* sys_tm = localtime(&t);
char fileTime[20]={0x00};
char fileName[100]={0x00};
int n = snprintf(fileTime, 64, "%d-%02d-%02d.log", sys_tm->tm_year+1900, sys_tm->tm_mon+1, sys_tm->tm_mday);
sprintf(fileName,"%s%s",SOFTWARE_RUN_LOG,fileTime);
strcpy(gLogParam.m_log_full_name, fileName);
2021-09-18 13:45:24 +08:00
FILE *fp = fopen(gLogParam.m_log_full_name, "a");
if(fp == NULL) {
return;
}
memset(gLogParam.m_buf, 0, sizeof(char)*gLogParam.m_log_buf_size);
pthread_mutex_lock(gLogParam.m_mutex);
2024-07-09 09:49:42 +08:00
n = snprintf(gLogParam.m_buf, 64, "%d-%02d-%02d %02d:%02d:%02d %s: ", sys_tm->tm_year+1900, sys_tm->tm_mon+1, sys_tm->tm_mday,
2021-09-18 13:45:24 +08:00
sys_tm->tm_hour, sys_tm->tm_min, sys_tm->tm_sec, level);
int old_seek = ftell(fp);
fseek(fp, 0, SEEK_END);
int file_size = ftell(fp);
fseek(fp, old_seek, SEEK_SET);
if (file_size > gLogParam.m_limit_size) {
fclose(fp);
char cmd[256] = { 0 };
sprintf(cmd, "cp %s %s_bak", gLogParam.m_log_full_name, gLogParam.m_log_full_name);
int iRet = system(cmd);
if (iRet == -1)
{
perror("system() error");
}
print_brown("iRet = %d,cmd = %s\n",iRet,cmd);
2021-09-18 13:45:24 +08:00
remove(gLogParam.m_log_full_name);
fp = fopen(gLogParam.m_log_full_name, "a");
if(fp == NULL) {
return;
}
}
va_list valst;
va_start(valst, format);
int m = vsnprintf(gLogParam.m_buf + n, gLogParam.m_log_buf_size -n -1, format, valst);
gLogParam.m_buf[n + m - 1] = '\n';
fwrite(gLogParam.m_buf, n + m, 1, fp);
va_end(valst);
fclose(fp);
pthread_mutex_unlock(gLogParam.m_mutex);
}