Merge pull request #1 from armink/master

updata
pull/55/head
秦天龙 6 years ago committed by GitHub
commit 1ad6aedfe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,6 +1,6 @@
The MIT License (MIT) 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 Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the a copy of this software and associated documentation files (the

@ -35,4 +35,7 @@
/* EasyLogger file log plugin's using file max size */ /* EasyLogger file log plugin's using file max size */
#define ELOG_FILE_MAX_SIZE (10 * 1024 * 1024) #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_ */ #endif /* _ELOG_FILE_CFG_H_ */

@ -1,7 +1,7 @@
/* /*
* This file is part of the EasyLogger Library. * 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 * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
@ -50,7 +50,7 @@ extern "C" {
#define ELOG_LVL_TOTAL_NUM 6 #define ELOG_LVL_TOTAL_NUM 6
/* EasyLogger software version number */ /* EasyLogger software version number */
#define ELOG_SW_VERSION "2.0.3" #define ELOG_SW_VERSION "2.1.99"
/* EasyLogger assert for developer. */ /* EasyLogger assert for developer. */
#ifdef ELOG_ASSERT_ENABLE #ifdef ELOG_ASSERT_ENABLE
@ -69,12 +69,12 @@ extern "C" {
#endif #endif
#ifndef ELOG_OUTPUT_ENABLE #ifndef ELOG_OUTPUT_ENABLE
#define elog_a(tag, ...) #define elog_assert(tag, ...)
#define elog_e(tag, ...) #define elog_error(tag, ...)
#define elog_w(tag, ...) #define elog_warn(tag, ...)
#define elog_i(tag, ...) #define elog_info(tag, ...)
#define elog_d(tag, ...) #define elog_debug(tag, ...)
#define elog_v(tag, ...) #define elog_verbose(tag, ...)
#else /* ELOG_OUTPUT_ENABLE */ #else /* ELOG_OUTPUT_ENABLE */
#if ELOG_OUTPUT_LVL >= ELOG_LVL_ASSERT #if ELOG_OUTPUT_LVL >= ELOG_LVL_ASSERT
#define elog_assert(tag, ...) \ #define elog_assert(tag, ...) \

@ -31,6 +31,8 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <file/elog_file.h> #include <file/elog_file.h>
#include <file/elog_file_cfg.h> #include <file/elog_file_cfg.h>
@ -53,6 +55,7 @@ ElogErrCode elog_file_init(void)
cfg.name = ELOG_FILE_NAME; cfg.name = ELOG_FILE_NAME;
cfg.max_size = ELOG_FILE_MAX_SIZE; cfg.max_size = ELOG_FILE_MAX_SIZE;
cfg.max_rotate = ELOG_FILE_MAX_ROTATE;
elog_file_config(&cfg); elog_file_config(&cfg);
@ -61,6 +64,52 @@ __exit:
return result; 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) void elog_file_write(const char *log, size_t size)
{ {
ELOG_ASSERT(init_ok); ELOG_ASSERT(init_ok);
@ -68,12 +117,19 @@ void elog_file_write(const char *log, size_t size)
struct stat statbuf; struct stat statbuf;
statbuf.st_size = 0; statbuf.st_size = 0;
elog_file_port_lock();
fstat(fd, &statbuf); fstat(fd, &statbuf);
if (unlikely(statbuf.st_size > local_cfg.max_size)) 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; return;
}
elog_file_port_lock(); }
fwrite(log, size, 1, fp); fwrite(log, size, 1, fp);
@ -103,6 +159,7 @@ void elog_file_config(ElogFileCfg *cfg)
local_cfg.name = cfg->name; local_cfg.name = cfg->name;
local_cfg.max_size = cfg->max_size; local_cfg.max_size = cfg->max_size;
local_cfg.max_rotate = cfg->max_rotate;
fp = fopen(local_cfg.name, "a+"); fp = fopen(local_cfg.name, "a+");
if (fp) if (fp)

@ -49,6 +49,7 @@ extern "C" {
typedef struct { typedef struct {
char *name; /* file name */ char *name; /* file name */
size_t max_size; /* file max size */ size_t max_size; /* file max size */
int max_rotate; /* max rotate file count */
} ElogFileCfg; } ElogFileCfg;
/* elog_file.c */ /* elog_file.c */

@ -35,4 +35,7 @@
/* EasyLogger file log plugin's using file max size */ /* EasyLogger file log plugin's using file max size */
#define ELOG_FILE_MAX_SIZE /* @note you must define it for a value */ #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_ */ #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) { for (i = 0; i < size; i += width) {
/* package header */ /* 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 */ /* calculate log length */
if ((fmt_result > -1) && (fmt_result <= ELOG_LINE_BUF_SIZE)) { if ((fmt_result > -1) && (fmt_result <= ELOG_LINE_BUF_SIZE)) {
log_len = fmt_result; log_len = fmt_result;

@ -283,8 +283,6 @@ static void *async_output(void *arg) {
size_t get_log_size = 0; size_t get_log_size = 0;
static char poll_get_buf[ELOG_ASYNC_POLL_GET_LOG_BUF_SIZE]; static char poll_get_buf[ELOG_ASYNC_POLL_GET_LOG_BUF_SIZE];
ELOG_ASSERT(init_ok);
while(true) { while(true) {
/* waiting log */ /* waiting log */
sem_wait(&output_notice); sem_wait(&output_notice);

Loading…
Cancel
Save