logger can delete old rotated files

pull/1/head
yuqing 2014-06-24 16:37:03 +08:00
parent 5a65898707
commit 101caed964
3 changed files with 80 additions and 12 deletions

View File

@ -12,6 +12,7 @@ Version 1.06 2014-06-24
* logger can log header line * logger can log header line
* #include <stdbool.h> to use C99 bool * #include <stdbool.h> to use C99 bool
* add libfastcommon.spec for building RPM * add libfastcommon.spec for building RPM
* logger can delete old rotated files
Version 1.05 2012-07-08 Version 1.05 2012-07-08
* update source code from FastDFS V3.09 * update source code from FastDFS V3.09

View File

@ -70,8 +70,6 @@ int log_init_ex(LogContext *pContext)
memset(pContext, 0, sizeof(LogContext)); memset(pContext, 0, sizeof(LogContext));
pContext->log_level = LOG_INFO; pContext->log_level = LOG_INFO;
pContext->log_fd = STDERR_FILENO; pContext->log_fd = STDERR_FILENO;
pContext->log_to_cache = false;
pContext->rotate_immediately = false;
pContext->time_precision = LOG_TIME_PRECISION_SECOND; pContext->time_precision = LOG_TIME_PRECISION_SECOND;
strcpy(pContext->rotate_time_format, "%Y%m%d_%H%M%S"); strcpy(pContext->rotate_time_format, "%Y%m%d_%H%M%S");
@ -160,6 +158,11 @@ void log_set_rotate_time_format(LogContext *pContext, const char *time_format)
"%s", time_format); "%s", time_format);
} }
void log_set_keep_days(LogContext *pContext, const int keep_days)
{
pContext->keep_days = keep_days;
}
void log_set_header_callback(LogContext *pContext, LogHeaderCallback header_callback) void log_set_header_callback(LogContext *pContext, LogHeaderCallback header_callback)
{ {
pContext->print_header_callback = header_callback; pContext->print_header_callback = header_callback;
@ -215,12 +218,59 @@ int log_notify_rotate(void *args)
return 0; return 0;
} }
int log_delete_old_files(void *args)
{
LogContext *pContext;
char old_filename[MAX_PATH_SIZE + 32];
int len;
struct tm tm;
time_t the_time;
if (args == NULL)
{
return EINVAL;
}
pContext = (LogContext *)args;
if (*(pContext->log_filename) == '\0' || \
*(pContext->rotate_time_format) == '\0')
{
return EINVAL;
}
if (pContext->keep_days <= 0) {
return 0;
}
the_time = get_current_time() - pContext->keep_days * 86400;
while (1) {
the_time -= 86400;
localtime_r(&the_time, &tm);
memset(old_filename, 0, sizeof(old_filename));
len = sprintf(old_filename, "%s", pContext->log_filename);
strftime(old_filename + len, sizeof(old_filename) - len,
pContext->rotate_time_format, &tm);
if (unlink(old_filename) != 0)
{
if (errno != ENOENT)
{
fprintf(stderr, "file: "__FILE__", line: %d, " \
"unlink %s fail, errno: %d, error info: %s\n", \
__LINE__, old_filename, errno, STRERROR(errno));
}
break;
}
}
return 0;
}
static int log_rotate(LogContext *pContext) static int log_rotate(LogContext *pContext)
{ {
struct tm tm; struct tm tm;
time_t current_time; time_t current_time;
int len; int len;
char new_filename[MAX_PATH_SIZE + 32]; char old_filename[MAX_PATH_SIZE + 32];
if (*(pContext->log_filename) == '\0') if (*(pContext->log_filename) == '\0')
{ {
@ -232,21 +282,21 @@ static int log_rotate(LogContext *pContext)
current_time = get_current_time(); current_time = get_current_time();
localtime_r(&current_time, &tm); localtime_r(&current_time, &tm);
memset(new_filename, 0, sizeof(new_filename)); memset(old_filename, 0, sizeof(old_filename));
len = sprintf(new_filename, "%s", pContext->log_filename); len = sprintf(old_filename, "%s", pContext->log_filename);
strftime(new_filename + len, sizeof(new_filename) - len, strftime(old_filename + len, sizeof(old_filename) - len,
pContext->rotate_time_format, &tm); pContext->rotate_time_format, &tm);
if (access(new_filename, F_OK) == 0) if (access(old_filename, F_OK) == 0)
{ {
fprintf(stderr, "file: "__FILE__", line: %d, " \ fprintf(stderr, "file: "__FILE__", line: %d, " \
"file: %s already exist, rotate file fail", "file: %s already exist, rotate file fail\n",
__LINE__, new_filename); __LINE__, old_filename);
} }
else if (rename(pContext->log_filename, new_filename) != 0) else if (rename(pContext->log_filename, old_filename) != 0)
{ {
fprintf(stderr, "file: "__FILE__", line: %d, " \ fprintf(stderr, "file: "__FILE__", line: %d, " \
"rename %s to %s fail, errno: %d, error info: %s", \ "rename %s to %s fail, errno: %d, error info: %s\n", \
__LINE__, pContext->log_filename, new_filename, \ __LINE__, pContext->log_filename, old_filename, \
errno, STRERROR(errno)); errno, STRERROR(errno));
} }

View File

@ -72,6 +72,9 @@ typedef struct log_context
* */ * */
char rotate_time_format[32]; char rotate_time_format[32];
/* keep days for rotated log files */
int keep_days;
/* /*
* log the header (title line) callback * log the header (title line) callback
* */ * */
@ -145,6 +148,13 @@ void log_set_time_precision(LogContext *pContext, const int time_precision);
*/ */
void log_set_rotate_time_format(LogContext *pContext, const char *time_format); void log_set_rotate_time_format(LogContext *pContext, const char *time_format);
/** set keep days
* parameters:
* pContext: the log context
* keep_days: the keep days
* return: none
*/
void log_set_keep_days(LogContext *pContext, const int keep_days);
/** set print header callback /** set print header callback
* parameters: * parameters:
@ -212,6 +222,13 @@ int log_sync_func(void *args);
*/ */
int log_notify_rotate(void *args); int log_notify_rotate(void *args);
/** delete old log files
* parameters:
* args: should be (LogContext *)
* return: error no, 0 for success, != 0 fail
*/
int log_delete_old_files(void *args);
void logEmergEx(LogContext *pContext, const char *format, ...); void logEmergEx(LogContext *pContext, const char *format, ...);
void logCritEx(LogContext *pContext, const char *format, ...); void logCritEx(LogContext *pContext, const char *format, ...);
void logAlertEx(LogContext *pContext, const char *format, ...); void logAlertEx(LogContext *pContext, const char *format, ...);