1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <math.h>
#include <pthread.h>
#include "logging.h"
static FILE * s_pFile = NULL;
static char * s_pDirName = "log/";
static char * s_pFileName = "logging";
static unsigned int s_uiFileNum = 0;
static unsigned int s_uiLineCount = 0;
static pthread_mutex_t s_mutex;
/* Log level */
static char s_acLevel[LOG_MAX_LEVEL][8] = { "INF", "WAR", "ERR", "INF" };
/* Log module */
static char s_acModule[LOG_MAX_MOD - LOG_MIN_MOD - 1][8] = {
"MOD1",
"MOD2",
"MOD3",
"MOD4",
"MOD5",
"MOD6",
"MOD7",
"MOD8",
"MOD9",
"MODA",
"MODB",
"MODC",
"MODD",
"MODE",
"MODF",
"MODG",
"MODH"
};
/* Timestamp for log file name (conpact format) */
static void LOG_GetTime(char *pStrBuf)
{
time_t now;
struct tm *tm_now;
if (NULL == pStrBuf)
{
return;
}
time(&now);
tm_now = localtime(&now);
sprintf(pStrBuf, "%d%02d%02d%02d%02d%02d", tm_now->tm_year + 1900, tm_now->tm_mon+1, tm_now->tm_mday,
tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
return;
}
/* Timestamp for log message (loose format) */
static void LOG_GetTimestamp(char *pStrBuf)
{
time_t now;
struct tm *tm_now;
if (NULL == pStrBuf)
{
return;
}
time(&now);
tm_now = localtime(&now);
sprintf(pStrBuf, "%d-%02d-%02d %02d:%02d:%02d", tm_now->tm_year + 1900, tm_now->tm_mon+1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
return;
}
/* Log rotate: add time stamps to log file name, so each log file will be different from the others */
static int LOG_Rotate()
{
int iRet = -1;
char acDate[LOG_DATE_LEN + 1];
char acFileNameNew[LOG_FILENAME_LEN + 1];
char acFileNameOld[LOG_FILENAME_LEN + 1];
memset(acDate, 0x00, LOG_DATE_LEN+1);
LOG_GetTime(acDate);
memset(acFileNameOld, 0x00, LOG_FILENAME_LEN + 1);
sprintf(acFileNameOld, "%s%s.txt", s_pDirName, s_pFileName);
memset(acFileNameNew, 0x00, LOG_FILENAME_LEN + 1);
sprintf(acFileNameNew, "%s%s_%s_%04d.txt", s_pDirName, s_pFileName, acDate, s_uiFileNum);
iRet = rename(acFileNameOld, acFileNameNew);
if (0 != iRet)
{
printf("Error, failed to call rename %s to %s.\n", acFileNameOld, acFileNameNew);
return -1;
}
/* Log file number threshold checking */
if (s_uiFileNum >= MAX_LOG_FILE_NUM)
{
printf("Error, maximum file number reached(%d)!!!!\n", s_uiFileNum);
assert(0);
}
return 0;
}
int LOG_Init()
{
char acFileFullName[LOG_FILENAME_LEN + 1];
int iRet = -1;
memset(acFileFullName, 0x00, LOG_FILENAME_LEN + 1);
sprintf(acFileFullName, "%s%s.txt", s_pDirName, s_pFileName);
s_pFile = fopen(acFileFullName, "w+");
if (NULL == s_pFile)
{
printf("[ERROR]LOG_Init: Failed to open file %s.\n", acFileFullName);
return -1;
}
/* Mutex lock initialize */
iRet = pthread_mutex_init(&s_mutex, NULL);;
if (0 != iRet)
{
printf("[ERROR]LOG_Init: Failed to call pthread_mutex_init()\n");
return -1;
}
s_uiLineCount = 0;
s_uiFileNum ++;
return 0;
}
void LOG_Logging(int iLevel, int iModule, char * pStrFormat, ...)
{
char acMsg[LOG_MSG_LEN + 1];
char acDate[LOG_DATE_LEN + 1];
char acBuf[LOG_BUF_LEN + 1];
va_list list;
va_start(list, pStrFormat);
/* Parameters check */
if (iModule <= LOG_MIN_MOD || iModule >= LOG_MAX_MOD || iLevel >= LOG_MAX_LEVEL)
{
assert(0);
}
/* Get message content */
memset(acMsg, 0x00, LOG_MSG_LEN + 1);
vsnprintf(acMsg, LOG_BUF_LEN + 1, pStrFormat, list);
/* Get time stamp */
memset(acDate, 0x00, LOG_DATE_LEN + 1);
LOG_GetTimestamp(acDate);
/* Format log messages to write to file */
memset(acBuf, 0x00, sizeof(acBuf));
sprintf(acBuf, "%s [%s] %s: %s", acDate, s_acLevel[iLevel], s_acModule[iModule - LOG_MIN_MOD -1], acMsg);
pthread_mutex_lock(&s_mutex);
/* Output the error message directly to screen. */
if (iLevel == LERR || iLevel == LSCR)
{
printf(acBuf);
}
/* Log file rotate, if the threshold (maximum line number) reached */
if (s_uiLineCount >= MAX_LOG_LINE_NUM)
{
fclose(s_pFile);
s_pFile = NULL;
LOG_Rotate();
LOG_Init();
}
fputs(acBuf, s_pFile);
fflush(s_pFile);
s_uiLineCount++;
pthread_mutex_unlock(&s_mutex);
va_end(list);
return;
}
void LOG_Close()
{
if(NULL == s_pFile)
{
return;
}
fclose(s_pFile);
s_pFile = NULL;
LOG_Rotate();
pthread_mutex_destroy(&s_mutex);
return;
}
/*********************************************************/
/* logging.h */
#ifndef _LOGGING_H_
#define _LOGGING_H_
#include <stdio.h>
#define MAX_LOG_LINE_NUM 500000 /* Max line number of a single file */
#define MAX_LOG_FILE_NUM 9999
#define LOG_BUF_LEN 256
#define LOG_MSG_LEN 128
#define LOG_FILENAME_LEN 64
#define LOG_DATE_LEN 32
#define LOG_MODULE_LEN 16
#define LOG_MAX_LEVEL 4
/* Values for 2nd parameter of LOG_Logging() */
enum enLogModules
{
LOG_MIN_MOD = 100,
LOG_MOD1,
LOG_MOD2,
LOG_MOD3,
LOG_MOD4,
LOG_MOD5,
LOG_MOD6,
LOG_MOD7,
LOG_MOD8,
LOG_MOD9,
LOG_MODA,
LOG_MODB,
LOG_MODC,
LOG_MODD,
LOG_MODE,
LOG_MODF,
LOG_MODG,
LOG_MODH,
LOG_MAX_MOD
};
enum log_level
{
LINF,
LWAR,
LERR,
LSCR
};
extern FILE *g_pFile; /* Log fle handler */
int LOG_Init();
void LOG_Logging(int level, int module, char * format, ...);
void LOG_Close();
#endif
| |