Skip to content

Commit

Permalink
revert
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Apr 22, 2024
1 parent 1cc790f commit e5fe1be
Showing 1 changed file with 13 additions and 19 deletions.
32 changes: 13 additions & 19 deletions ww/loggers/core_logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,13 @@ static void generateLogFileName(const char *filepath, time_t ts, char *buf, int
struct tm *tm = localtime(&ts);
snprintf(buf, len, "%s.%04d%02d%02d.log", filepath, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
}
static void logfileName(const char *filepath, time_t ts, char *buf, int len)
{
struct tm *tm = localtime(&ts);
snprintf(buf, len, "%s.%04d%02d%02d.log",
filepath,
tm->tm_year + 1900,
tm->tm_mon + 1,
tm->tm_mday);
}

static FILE *logFileShift(logger_t *logger)
{
time_t ts_now = time(NULL);
int interval_days = logger->last_logfile_ts == 0 ? 0 : (ts_now + S_GMTOFF) / SECONDS_PER_DAY - (logger->last_logfile_ts + S_GMTOFF) / SECONDS_PER_DAY;
time_t ts_now = time(NULL);
long interval_days = logger->last_logfile_ts == 0 ? 0
: (ts_now + S_GMTOFF) / SECONDS_PER_DAY -
(logger->last_logfile_ts + S_GMTOFF) / SECONDS_PER_DAY;
if (logger->fp_ == NULL || interval_days > 0)
{
// close old logfile
Expand All @@ -66,18 +59,18 @@ static FILE *logFileShift(logger_t *logger)
if (interval_days >= logger->remain_days)
{
// remove [today-interval_days, today-remain_days] logfile
for (int i = interval_days; i >= logger->remain_days; --i)
for (long i = interval_days; i >= logger->remain_days; --i)
{
time_t ts_rm = ts_now - i * SECONDS_PER_DAY;
logfileName(logger->filepath, ts_rm, rm_logfile, sizeof(rm_logfile));
generateLogFileName(logger->filepath, ts_rm, rm_logfile, sizeof(rm_logfile));
remove(rm_logfile);
}
}
else
{
// remove today-remain_days logfile
time_t ts_rm = ts_now - logger->remain_days * SECONDS_PER_DAY;
logfileName(logger->filepath, ts_rm, rm_logfile, sizeof(rm_logfile));
generateLogFileName(logger->filepath, ts_rm, rm_logfile, sizeof(rm_logfile));
remove(rm_logfile);
}
}
Expand All @@ -86,16 +79,16 @@ static FILE *logFileShift(logger_t *logger)
// open today logfile
if (logger->fp_ == NULL)
{
logfileName(logger->filepath, ts_now, logger->cur_logfile, sizeof(logger->cur_logfile));
logger->fp_ = fopen(logger->cur_logfile, "a");
generateLogFileName(logger->filepath, ts_now, logger->cur_logfile, sizeof(logger->cur_logfile));
logger->fp_ = fopen(logger->cur_logfile, "a");
logger->last_logfile_ts = ts_now;
}

// NOTE: estimate can_write_cnt to avoid frequent fseek/ftell
if (logger->fp_ && --logger->can_write_cnt < 0)
{
fseek(logger->fp_, 0, SEEK_END);
long filesize = ftell(logger->fp_);
unsigned long long filesize = ftell(logger->fp_);
if (filesize > logger->max_filesize)
{
fclose(logger->fp_);
Expand All @@ -111,7 +104,8 @@ static FILE *logFileShift(logger_t *logger)
}
else
{
logger->can_write_cnt = (logger->max_filesize - filesize) / logger->bufsize;
logger->can_write_cnt =
((long long) logger->max_filesize - (long long) filesize) / (long long) logger->bufsize;
}
}

Expand Down Expand Up @@ -190,4 +184,4 @@ logger_t *createCoreLogger(const char *log_file, bool console)
core_logger_handle_t getCoreLoggerHandle()
{
return logger->handler;
}
}

0 comments on commit e5fe1be

Please sign in to comment.