logger support compress the log file
parent
c7ba512e09
commit
72f0214695
3
HISTORY
3
HISTORY
|
|
@ -1,7 +1,8 @@
|
||||||
Version 1.25 2016-02-22
|
Version 1.25 2016-02-29
|
||||||
* php7_ext_wrapper.h add more macro defines
|
* php7_ext_wrapper.h add more macro defines
|
||||||
* compile passed in FreeBSD 10.2
|
* compile passed in FreeBSD 10.2
|
||||||
* bugfixed: free task point correctly in free_queue_destroy
|
* bugfixed: free task point correctly in free_queue_destroy
|
||||||
|
* logger support compress the log file
|
||||||
|
|
||||||
Version 1.24 2016-02-15
|
Version 1.24 2016-02-15
|
||||||
* php extension compiled on PHP 7
|
* php extension compiled on PHP 7
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,9 @@ struct mpool_node {
|
||||||
struct mpool_chain {
|
struct mpool_chain {
|
||||||
struct mpool_node *head;
|
struct mpool_node *head;
|
||||||
struct mpool_node *tail;
|
struct mpool_node *tail;
|
||||||
} g_mpool = {NULL, NULL};
|
};
|
||||||
|
|
||||||
|
static struct mpool_chain g_mpool = {NULL, NULL};
|
||||||
|
|
||||||
#define ALIGNED_TASK_INFO_SIZE MEM_ALIGN(sizeof(struct fast_task_info))
|
#define ALIGNED_TASK_INFO_SIZE MEM_ALIGN(sizeof(struct fast_task_info))
|
||||||
|
|
||||||
|
|
|
||||||
87
src/logger.c
87
src/logger.c
|
|
@ -288,6 +288,11 @@ void log_take_over_stdout_ex(LogContext *pContext)
|
||||||
pContext->take_over_stdout = true;
|
pContext->take_over_stdout = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void log_set_compress_log_flag_ex(LogContext *pContext, const bool compress_log_flag)
|
||||||
|
{
|
||||||
|
pContext->compress_log_flag = compress_log_flag;
|
||||||
|
}
|
||||||
|
|
||||||
void log_set_fd_flags(LogContext *pContext, const int flags)
|
void log_set_fd_flags(LogContext *pContext, const int flags)
|
||||||
{
|
{
|
||||||
pContext->fd_flags = flags;
|
pContext->fd_flags = flags;
|
||||||
|
|
@ -334,6 +339,33 @@ int log_notify_rotate(void *args)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int log_delete_old_file(LogContext *pContext,
|
||||||
|
const char *old_filename)
|
||||||
|
{
|
||||||
|
char full_filename[MAX_PATH_SIZE + 128];
|
||||||
|
if (pContext->compress_log_flag)
|
||||||
|
{
|
||||||
|
snprintf(full_filename, sizeof(full_filename), "%s.gz", old_filename);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
snprintf(full_filename, sizeof(full_filename), "%s", old_filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (unlink(full_filename) != 0)
|
||||||
|
{
|
||||||
|
if (errno != ENOENT)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "file: "__FILE__", line: %d, " \
|
||||||
|
"unlink %s fail, errno: %d, error info: %s\n", \
|
||||||
|
__LINE__, full_filename, errno, STRERROR(errno));
|
||||||
|
}
|
||||||
|
return errno != 0 ? errno : EPERM;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int log_delete_matched_old_files(LogContext *pContext,
|
static int log_delete_matched_old_files(LogContext *pContext,
|
||||||
const int prefix_len)
|
const int prefix_len)
|
||||||
{
|
{
|
||||||
|
|
@ -428,6 +460,7 @@ int log_delete_old_files(void *args)
|
||||||
int full_len;
|
int full_len;
|
||||||
int prefix_len;
|
int prefix_len;
|
||||||
int len;
|
int len;
|
||||||
|
int result;
|
||||||
struct tm tm;
|
struct tm tm;
|
||||||
|
|
||||||
if (args == NULL)
|
if (args == NULL)
|
||||||
|
|
@ -480,19 +513,14 @@ int log_delete_old_files(void *args)
|
||||||
len = sprintf(old_filename, "%s.", pContext->log_filename);
|
len = sprintf(old_filename, "%s.", pContext->log_filename);
|
||||||
strftime(old_filename + len, sizeof(old_filename) - len,
|
strftime(old_filename + len, sizeof(old_filename) - len,
|
||||||
pContext->rotate_time_format, &tm);
|
pContext->rotate_time_format, &tm);
|
||||||
if (unlink(old_filename) != 0)
|
if ((result=log_delete_old_file(pContext, old_filename)) != 0)
|
||||||
{
|
{
|
||||||
if (errno != ENOENT)
|
if (result != ENOENT)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "file: "__FILE__", line: %d, " \
|
return result;
|
||||||
"unlink %s fail, errno: %d, error info: %s\n", \
|
|
||||||
__LINE__, old_filename, errno, STRERROR(errno));
|
|
||||||
return errno != 0 ? errno : EPERM;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -504,12 +532,36 @@ int log_delete_old_files(void *args)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void log_gzip(const char *filename)
|
||||||
|
{
|
||||||
|
char *gzip;
|
||||||
|
char cmd[MAX_PATH_SIZE + 128];
|
||||||
|
|
||||||
|
if (access("/bin/gzip", F_OK) == 0)
|
||||||
|
{
|
||||||
|
gzip = "/bin/gzip";
|
||||||
|
}
|
||||||
|
else if (access("/usr/bin/gzip", F_OK) == 0)
|
||||||
|
{
|
||||||
|
gzip = "/usr/bin/gzip";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
gzip = "gzip";
|
||||||
|
}
|
||||||
|
|
||||||
|
snprintf(cmd, sizeof(cmd), "%s %s", gzip, filename);
|
||||||
|
system(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
int log_rotate(LogContext *pContext)
|
int log_rotate(LogContext *pContext)
|
||||||
{
|
{
|
||||||
struct tm tm;
|
struct tm tm;
|
||||||
time_t current_time;
|
time_t current_time;
|
||||||
int len;
|
int len;
|
||||||
|
int result;
|
||||||
char old_filename[MAX_PATH_SIZE + 32];
|
char old_filename[MAX_PATH_SIZE + 32];
|
||||||
|
bool exist;
|
||||||
|
|
||||||
if (*(pContext->log_filename) == '\0')
|
if (*(pContext->log_filename) == '\0')
|
||||||
{
|
{
|
||||||
|
|
@ -540,6 +592,7 @@ int log_rotate(LogContext *pContext)
|
||||||
fprintf(stderr, "file: "__FILE__", line: %d, " \
|
fprintf(stderr, "file: "__FILE__", line: %d, " \
|
||||||
"file: %s already exist, rotate file fail\n",
|
"file: %s already exist, rotate file fail\n",
|
||||||
__LINE__, old_filename);
|
__LINE__, old_filename);
|
||||||
|
exist = true;
|
||||||
}
|
}
|
||||||
else if (rename(pContext->log_filename, old_filename) != 0)
|
else if (rename(pContext->log_filename, old_filename) != 0)
|
||||||
{
|
{
|
||||||
|
|
@ -547,9 +600,21 @@ int log_rotate(LogContext *pContext)
|
||||||
"rename %s to %s fail, errno: %d, error info: %s\n", \
|
"rename %s to %s fail, errno: %d, error info: %s\n", \
|
||||||
__LINE__, pContext->log_filename, old_filename, \
|
__LINE__, pContext->log_filename, old_filename, \
|
||||||
errno, STRERROR(errno));
|
errno, STRERROR(errno));
|
||||||
|
exist = false;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
exist = true;
|
||||||
|
}
|
||||||
|
|
||||||
return log_open(pContext);
|
result = log_open(pContext);
|
||||||
|
|
||||||
|
if (exist && pContext->compress_log_flag)
|
||||||
|
{
|
||||||
|
log_gzip(old_filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int log_check_rotate(LogContext *pContext)
|
static int log_check_rotate(LogContext *pContext)
|
||||||
|
|
|
||||||
11
src/logger.h
11
src/logger.h
|
|
@ -67,6 +67,9 @@ typedef struct log_context
|
||||||
/* if stdout to the log file */
|
/* if stdout to the log file */
|
||||||
bool take_over_stdout;
|
bool take_over_stdout;
|
||||||
|
|
||||||
|
/* if compress the log file use gzip command */
|
||||||
|
bool compress_log_flag;
|
||||||
|
|
||||||
/* time precision */
|
/* time precision */
|
||||||
char time_precision;
|
char time_precision;
|
||||||
|
|
||||||
|
|
@ -115,6 +118,9 @@ int log_init2();
|
||||||
#define log_take_over_stderr() log_take_over_stderr_ex(&g_log_context)
|
#define log_take_over_stderr() log_take_over_stderr_ex(&g_log_context)
|
||||||
#define log_take_over_stdout() log_take_over_stdout_ex(&g_log_context)
|
#define log_take_over_stdout() log_take_over_stdout_ex(&g_log_context)
|
||||||
|
|
||||||
|
#define log_set_compress_log_flag(compress_log_flag) \
|
||||||
|
log_set_compress_log_flag_ex(&g_log_context, compress_log_flag)
|
||||||
|
|
||||||
#define log_header(pContext, header, header_len) \
|
#define log_header(pContext, header, header_len) \
|
||||||
log_it_ex2(pContext, NULL, header, header_len, false, false)
|
log_it_ex2(pContext, NULL, header, header_len, false, false)
|
||||||
|
|
||||||
|
|
@ -203,6 +209,11 @@ void log_take_over_stderr_ex(LogContext *pContext);
|
||||||
*/
|
*/
|
||||||
void log_take_over_stdout_ex(LogContext *pContext);
|
void log_take_over_stdout_ex(LogContext *pContext);
|
||||||
|
|
||||||
|
/** set compress_log_flag to true
|
||||||
|
* return: none
|
||||||
|
*/
|
||||||
|
void log_set_compress_log_flag_ex(LogContext *pContext, const bool compress_log_flag);
|
||||||
|
|
||||||
/** set log fd flags
|
/** set log fd flags
|
||||||
* parameters:
|
* parameters:
|
||||||
* pContext: the log context
|
* pContext: the log context
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue