diff --git a/HISTORY b/HISTORY index bc43378..339f104 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.06 2014-06-13 +Version 1.06 2014-06-16 * update source code from FastDFS V5.02 * add function short2buff and buff2short * add object memory pool (fast_mblock.h and fast_mblock.c) @@ -7,6 +7,7 @@ Version 1.06 2014-06-13 * bug fixed: ini_file_reader.c can't include relative path sub config, such as #include ../../conf/common.conf * add get_url_content_ex to support buffer passed by caller + * logger can set rotate time format Version 1.05 2012-07-08 * update source code from FastDFS V3.09 diff --git a/src/logger.c b/src/logger.c index f66a763..5c7571c 100644 --- a/src/logger.c +++ b/src/logger.c @@ -73,6 +73,7 @@ int log_init_ex(LogContext *pContext) pContext->log_to_cache = false; pContext->rotate_immediately = false; pContext->time_precision = LOG_TIME_PRECISION_SECOND; + strcpy(pContext->rotate_time_format, "%Y%m%d_%H%M%S"); pContext->log_buff = (char *)malloc(LOG_BUFF_SIZE); if (pContext->log_buff == NULL) @@ -148,6 +149,13 @@ void log_set_time_precision(LogContext *pContext, const int time_precision) pContext->time_precision = time_precision; } +void log_set_rotate_time_format(LogContext *pContext, const char *time_format) +{ + snprintf(pContext->rotate_time_format, + sizeof(pContext->rotate_time_format), + "%s", time_format); +} + void log_destroy_ex(LogContext *pContext) { if (pContext->log_fd >= 0 && pContext->log_fd != STDERR_FILENO) @@ -193,6 +201,7 @@ static int log_rotate(LogContext *pContext) { struct tm tm; time_t current_time; + int len; char new_filename[MAX_PATH_SIZE + 32]; if (*(pContext->log_filename) == '\0') @@ -204,11 +213,18 @@ static int log_rotate(LogContext *pContext) current_time = get_current_time(); localtime_r(¤t_time, &tm); - sprintf(new_filename, "%s.%04d%02d%02d_%02d%02d%02d", \ - pContext->log_filename, \ - tm.tm_year+1900, tm.tm_mon+1, tm.tm_mday, \ - tm.tm_hour, tm.tm_min, tm.tm_sec); - if (rename(pContext->log_filename, new_filename) != 0) + + memset(new_filename, 0, sizeof(new_filename)); + len = sprintf(new_filename, "%s", pContext->log_filename); + strftime(new_filename + len, sizeof(new_filename) - len, + pContext->rotate_time_format, &tm); + if (access(new_filename, F_OK) == 0) + { + fprintf(stderr, "file: "__FILE__", line: %d, " \ + "file: %s already exist, rotate file fail", + __LINE__, new_filename); + } + else if (rename(pContext->log_filename, new_filename) != 0) { fprintf(stderr, "file: "__FILE__", line: %d, " \ "rename %s to %s fail, errno: %d, error info: %s", \ @@ -426,7 +442,7 @@ static void doLogEx(LogContext *pContext, struct timeval *tv, \ } } -static void doLog(LogContext *pContext, const char *caption, \ +void log_it_ex2(LogContext *pContext, const char *caption, \ const char *text, const int text_len, const bool bNeedSync) { struct timeval tv; @@ -490,7 +506,7 @@ void log_it_ex1(LogContext *pContext, const int priority, \ break; } - doLog(pContext, caption, text, text_len, bNeedSync); + log_it_ex2(pContext, caption, text, text_len, bNeedSync); } void log_it_ex(LogContext *pContext, const int priority, const char *format, ...) @@ -545,7 +561,7 @@ void log_it_ex(LogContext *pContext, const int priority, const char *format, ... break; } - doLog(pContext, caption, text, len, bNeedSync); + log_it_ex2(pContext, caption, text, len, bNeedSync); } @@ -565,7 +581,7 @@ void log_it_ex(LogContext *pContext, const int priority, const char *format, ... va_end(ap); \ } \ \ - doLog(pContext, caption, text, len, bNeedSync); \ + log_it_ex2(pContext, caption, text, len, bNeedSync); \ void logEmergEx(LogContext *pContext, const char *format, ...) diff --git a/src/logger.h b/src/logger.h index 79f5025..fed3828 100644 --- a/src/logger.h +++ b/src/logger.h @@ -61,6 +61,11 @@ typedef struct log_context /* save the log filename */ char log_filename[MAX_PATH_SIZE]; + + /* the time format for rotated filename, + * default: %Y%m%d_%H%M%S + * */ + char rotate_time_format[32]; } LogContext; extern LogContext g_log_context; @@ -122,6 +127,14 @@ void log_set_cache_ex(LogContext *pContext, const bool bLogCache); */ void log_set_time_precision(LogContext *pContext, const int time_precision); +/** set rotate time format, the time format same as function strftime + * parameters: + * pContext: the log context + * time_format: rotate time format + * return: none +*/ +void log_set_rotate_time_format(LogContext *pContext, const char *time_format); + /** destroy function * parameters: * pContext: the log context @@ -152,6 +165,19 @@ void log_it_ex(LogContext *pContext, const int priority, \ void log_it_ex1(LogContext *pContext, const int priority, \ const char *text, const int text_len); +/** log to file + * parameters: + * pContext: the log context + * caption: such as INFO, ERROR, NULL for no caption + * text: text string to log + * text_len: text string length (bytes) + * bNeedSync: if sync to file immediatelly + * return: none +*/ +void log_it_ex2(LogContext *pContext, const char *caption, \ + const char *text, const int text_len, const bool bNeedSync); + + /** sync log buffer to log file * parameters: * args: should be (LogContext *)