mirror of
https://github.com/armink/EasyLogger.git
synced 2025-01-19 07:42:52 +08:00
commit
1ad6aedfe3
2
LICENSE
2
LICENSE
@ -1,6 +1,6 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015-2018 Armink (armink.ztl@gmail.com)
|
||||
Copyright (c) 2015-2019 Armink (armink.ztl@gmail.com)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
|
@ -35,4 +35,7 @@
|
||||
/* EasyLogger file log plugin's using file max size */
|
||||
#define ELOG_FILE_MAX_SIZE (10 * 1024 * 1024)
|
||||
|
||||
/* EasyLogger file log plugin's using max rotate file count */
|
||||
#define ELOG_FILE_MAX_ROTATE 10
|
||||
|
||||
#endif /* _ELOG_FILE_CFG_H_ */
|
||||
|
@ -1,7 +1,7 @@
|
||||
/*
|
||||
* This file is part of the EasyLogger Library.
|
||||
*
|
||||
* Copyright (c) 2015-2018, Armink, <armink.ztl@gmail.com>
|
||||
* Copyright (c) 2015-2019, Armink, <armink.ztl@gmail.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
@ -50,7 +50,7 @@ extern "C" {
|
||||
#define ELOG_LVL_TOTAL_NUM 6
|
||||
|
||||
/* EasyLogger software version number */
|
||||
#define ELOG_SW_VERSION "2.0.3"
|
||||
#define ELOG_SW_VERSION "2.1.99"
|
||||
|
||||
/* EasyLogger assert for developer. */
|
||||
#ifdef ELOG_ASSERT_ENABLE
|
||||
@ -69,12 +69,12 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
#ifndef ELOG_OUTPUT_ENABLE
|
||||
#define elog_a(tag, ...)
|
||||
#define elog_e(tag, ...)
|
||||
#define elog_w(tag, ...)
|
||||
#define elog_i(tag, ...)
|
||||
#define elog_d(tag, ...)
|
||||
#define elog_v(tag, ...)
|
||||
#define elog_assert(tag, ...)
|
||||
#define elog_error(tag, ...)
|
||||
#define elog_warn(tag, ...)
|
||||
#define elog_info(tag, ...)
|
||||
#define elog_debug(tag, ...)
|
||||
#define elog_verbose(tag, ...)
|
||||
#else /* ELOG_OUTPUT_ENABLE */
|
||||
#if ELOG_OUTPUT_LVL >= ELOG_LVL_ASSERT
|
||||
#define elog_assert(tag, ...) \
|
||||
|
@ -31,6 +31,8 @@
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <file/elog_file.h>
|
||||
#include <file/elog_file_cfg.h>
|
||||
@ -53,6 +55,7 @@ ElogErrCode elog_file_init(void)
|
||||
|
||||
cfg.name = ELOG_FILE_NAME;
|
||||
cfg.max_size = ELOG_FILE_MAX_SIZE;
|
||||
cfg.max_rotate = ELOG_FILE_MAX_ROTATE;
|
||||
|
||||
elog_file_config(&cfg);
|
||||
|
||||
@ -61,6 +64,52 @@ __exit:
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* rotate the log file xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0
|
||||
*
|
||||
* it will return true when rotate successfully
|
||||
*/
|
||||
static bool elog_file_rotate(void)
|
||||
{
|
||||
#define SUFFIX_LEN 10
|
||||
/* mv xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */
|
||||
size_t base = strlen(local_cfg.name);
|
||||
char *oldpath = NULL, *newpath = NULL;
|
||||
int n;
|
||||
FILE *fp_bak = fp;
|
||||
|
||||
oldpath = (char *) malloc(base + SUFFIX_LEN);
|
||||
newpath = (char *) malloc(base + SUFFIX_LEN);
|
||||
|
||||
if (oldpath == NULL || newpath == NULL) {
|
||||
return false;
|
||||
}
|
||||
|
||||
memcpy(oldpath, local_cfg.name, base);
|
||||
memcpy(newpath, local_cfg.name, base);
|
||||
|
||||
for (n = local_cfg.max_rotate - 1; n >= 0; --n) {
|
||||
snprintf(oldpath + base, SUFFIX_LEN, n ? ".%d" : "", n - 1);
|
||||
snprintf(newpath + base, SUFFIX_LEN, ".%d", n);
|
||||
rename(oldpath, newpath);
|
||||
}
|
||||
free(oldpath);
|
||||
free(newpath);
|
||||
|
||||
fp = fopen(local_cfg.name, "a+");
|
||||
if (fp) {
|
||||
if (fp_bak) {
|
||||
fclose(fp_bak);
|
||||
}
|
||||
fd = fileno(fp);
|
||||
return true;
|
||||
} else {
|
||||
fp = fp_bak;
|
||||
fd = -1;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void elog_file_write(const char *log, size_t size)
|
||||
{
|
||||
ELOG_ASSERT(init_ok);
|
||||
@ -68,13 +117,20 @@ void elog_file_write(const char *log, size_t size)
|
||||
struct stat statbuf;
|
||||
|
||||
statbuf.st_size = 0;
|
||||
fstat(fd, &statbuf);
|
||||
|
||||
if (unlikely(statbuf.st_size > local_cfg.max_size))
|
||||
return;
|
||||
|
||||
elog_file_port_lock();
|
||||
|
||||
fstat(fd, &statbuf);
|
||||
|
||||
if (unlikely(statbuf.st_size > local_cfg.max_size)) {
|
||||
/* rotate the log file */
|
||||
if (local_cfg.max_rotate <= 0 || !elog_file_rotate()) {
|
||||
/* not enabled rotate or rotate failed */
|
||||
elog_file_port_unlock();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
fwrite(log, size, 1, fp);
|
||||
|
||||
#ifdef ELOG_FILE_FLUSH_CAHCE_ENABLE
|
||||
@ -103,6 +159,7 @@ void elog_file_config(ElogFileCfg *cfg)
|
||||
|
||||
local_cfg.name = cfg->name;
|
||||
local_cfg.max_size = cfg->max_size;
|
||||
local_cfg.max_rotate = cfg->max_rotate;
|
||||
|
||||
fp = fopen(local_cfg.name, "a+");
|
||||
if (fp)
|
||||
|
@ -49,6 +49,7 @@ extern "C" {
|
||||
typedef struct {
|
||||
char *name; /* file name */
|
||||
size_t max_size; /* file max size */
|
||||
int max_rotate; /* max rotate file count */
|
||||
} ElogFileCfg;
|
||||
|
||||
/* elog_file.c */
|
||||
|
@ -35,4 +35,7 @@
|
||||
/* EasyLogger file log plugin's using file max size */
|
||||
#define ELOG_FILE_MAX_SIZE /* @note you must define it for a value */
|
||||
|
||||
/* EasyLogger file log plugin's using max rotate file count */
|
||||
#define ELOG_FILE_MAX_ROTATE /* @note you must define it for a value */
|
||||
|
||||
#endif /* _ELOG_FILE_CFG_H_ */
|
||||
|
@ -699,7 +699,7 @@ void elog_hexdump(const char *name, uint8_t width, uint8_t *buf, uint16_t size)
|
||||
|
||||
for (i = 0; i < size; i += width) {
|
||||
/* package header */
|
||||
fmt_result = snprintf(log_buf, ELOG_LINE_BUF_SIZE, "D/HEX %s: %04X-%04X: ", name, i, i + width);
|
||||
fmt_result = snprintf(log_buf, ELOG_LINE_BUF_SIZE, "D/HEX %s: %04X-%04X: ", name, i, i + width - 1);
|
||||
/* calculate log length */
|
||||
if ((fmt_result > -1) && (fmt_result <= ELOG_LINE_BUF_SIZE)) {
|
||||
log_len = fmt_result;
|
||||
|
@ -283,8 +283,6 @@ static void *async_output(void *arg) {
|
||||
size_t get_log_size = 0;
|
||||
static char poll_get_buf[ELOG_ASYNC_POLL_GET_LOG_BUF_SIZE];
|
||||
|
||||
ELOG_ASSERT(init_ok);
|
||||
|
||||
while(true) {
|
||||
/* waiting log */
|
||||
sem_wait(&output_notice);
|
||||
|
Loading…
x
Reference in New Issue
Block a user