[plugin/file] remove the posix API in file plugin.

pull/78/head
armink 6 years ago
parent a607e1715b
commit 29bc4e5ee5

@ -26,21 +26,17 @@
* Created on: 2019-01-05 * Created on: 2019-01-05
*/ */
#include <unistd.h> #include <stdio.h>
#include <sys/types.h> #include <io.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <file/elog_file.h> #include "elog_file.h"
#include <file/elog_file_cfg.h>
/* initialize OK flag */ /* initialize OK flag */
static bool init_ok = false; static bool init_ok = false;
static FILE *fp = NULL; static FILE *fp = NULL;
static int fd = -1;
static ElogFileCfg local_cfg; static ElogFileCfg local_cfg;
ElogErrCode elog_file_init(void) ElogErrCode elog_file_init(void)
@ -64,88 +60,68 @@ __exit:
return result; return result;
} }
/*
* Reopen file
*/
static bool elog_file_reopen(void)
{
FILE *tmp_fp;
tmp_fp = fopen(local_cfg.name, "a+");
if (tmp_fp) {
if (fp)
fclose(fp);
fp = tmp_fp;
fd = fileno(fp);
return true;
}
return false;
}
/* /*
* rotate the log file xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 * rotate the log file xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0
*/ */
static void elog_file_rotate(void) static bool elog_file_rotate(void)
{ {
#define SUFFIX_LEN 10 #define SUFFIX_LEN 10
/* mv xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */ /* mv xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */
int n; int n, err = 0;
char oldpath[256], newpath[256]; char oldpath[256], newpath[256];
size_t base = strlen(local_cfg.name); size_t base = strlen(local_cfg.name);
bool result = true;
memcpy(oldpath, local_cfg.name, base); memcpy(oldpath, local_cfg.name, base);
memcpy(newpath, local_cfg.name, base); memcpy(newpath, local_cfg.name, base);
fclose(fp);
for (n = local_cfg.max_rotate - 1; n >= 0; --n) { for (n = local_cfg.max_rotate - 1; n >= 0; --n) {
snprintf(oldpath + base, SUFFIX_LEN, n ? ".%d" : "", n - 1); snprintf(oldpath + base, SUFFIX_LEN, n ? ".%d" : "", n - 1);
snprintf(newpath + base, SUFFIX_LEN, ".%d", n); snprintf(newpath + base, SUFFIX_LEN, ".%d", n);
rename(oldpath, newpath); /* remove the old file */
} if (access(newpath, F_OK) == 0) {
} remove(newpath);
}
/* change the new log file to old file name */
if (access(oldpath, F_OK) == 0) {
err = rename(oldpath, newpath);
}
/* if (err < 0) {
* Check if it needed retate result = false;
*/ goto __exit;
static bool elog_file_retate_check(void) }
{ }
struct stat statbuf;
statbuf.st_size = 0;
if (stat(local_cfg.name, &statbuf) < 0)
return false;
if (statbuf.st_size > local_cfg.max_size) __exit:
return true; /* reopen the file */
fp = fopen(local_cfg.name, "a+");
return false; return result;
} }
void elog_file_write(const char *log, size_t size) void elog_file_write(const char *log, size_t size)
{ {
size_t file_size = 0;
ELOG_ASSERT(init_ok); ELOG_ASSERT(init_ok);
ELOG_ASSERT(log); ELOG_ASSERT(log);
struct stat statbuf;
statbuf.st_size = 0;
elog_file_port_lock(); elog_file_port_lock();
fstat(fd, &statbuf); fseek(fp, 0L, SEEK_END);
file_size = ftell(fp);
if (unlikely(statbuf.st_size > local_cfg.max_size)) { if (unlikely(file_size > local_cfg.max_size)) {
#if ELOG_FILE_MAX_ROTATE > 0 #if ELOG_FILE_MAX_ROTATE > 0
if (elog_file_retate_check()) { if (!elog_file_rotate()) {
/* rotate the log file */ goto __exit;
elog_file_rotate();
}
if (!elog_file_reopen()) {
elog_file_port_unlock();
return;
} }
#else #else
return ; goto __exit;
#endif #endif
} }
@ -153,9 +129,9 @@ void elog_file_write(const char *log, size_t size)
#ifdef ELOG_FILE_FLUSH_CAHCE_ENABLE #ifdef ELOG_FILE_FLUSH_CAHCE_ENABLE
fflush(fp); fflush(fp);
fsync(fd);
#endif #endif
__exit:
elog_file_port_unlock(); elog_file_port_unlock();
} }
@ -180,10 +156,6 @@ void elog_file_config(ElogFileCfg *cfg)
local_cfg.max_rotate = cfg->max_rotate; local_cfg.max_rotate = cfg->max_rotate;
fp = fopen(local_cfg.name, "a+"); fp = fopen(local_cfg.name, "a+");
if (fp)
fd = fileno(fp);
else
fd = -1;
elog_file_port_unlock(); elog_file_port_unlock();
} }

@ -31,6 +31,7 @@
#include <stdio.h> #include <stdio.h>
#include <elog.h> #include <elog.h>
#include <elog_file_cfg.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {

Loading…
Cancel
Save