Skip to content

Commit

Permalink
fix localtime threadsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
hongliuliao committed Jul 31, 2021
1 parent d796bea commit fecabdc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
60 changes: 39 additions & 21 deletions src/simple_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ int FileAppender::write_log(char *log, const char *format, va_list ap) {
return 0;
}

struct tm* get_localtime(time_t *input, struct tm *t) {
return localtime_r(input, t);
}

int FileAppender::shift_file(struct timeval tv) {
time_t y_sec = tv.tv_sec - ONE_DAY_SECONDS;
struct tm t;
struct tm* tm = get_localtime(&y_sec, &t); //yesterday
if (tm == NULL) {
return -1;
}
char new_file[100];
bzero(new_file, 100);
sprintf(new_file, "%s.%04d-%02d-%02d",
_log_file.c_str(),
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
std::string new_file_path = _log_dir + "/" + new_file;
if (access(new_file_path.c_str(), F_OK) != 0) {
rename(_log_file_path.c_str(), new_file_path.c_str());
// reopen new log file
_fs.close();
_fs.open(_log_file_path.c_str(), std::fstream::out | std::fstream::app);
}
return 0;
}

int FileAppender::shift_file_if_need(struct timeval tv, struct timezone tz) {
if (_last_sec == 0) {
_last_sec = tv.tv_sec;
Expand All @@ -78,23 +104,8 @@ int FileAppender::shift_file_if_need(struct timeval tv, struct timezone tz) {
long fix_last_sec = _last_sec - tz.tz_minuteswest * 60;
if (fix_now_sec / ONE_DAY_SECONDS - fix_last_sec / ONE_DAY_SECONDS) {
pthread_mutex_lock(&writelock);

struct tm *tm;
time_t y_sec = tv.tv_sec - ONE_DAY_SECONDS;
tm = localtime(&y_sec); //yesterday
char new_file[100];
bzero(new_file, 100);
sprintf(new_file, "%s.%04d-%02d-%02d",
_log_file.c_str(),
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
std::string new_file_path = _log_dir + "/" + new_file;
if (access(new_file_path.c_str(), F_OK) != 0) {
rename(_log_file_path.c_str(), new_file_path.c_str());
// reopen new log file
_fs.close();
_fs.open(_log_file_path.c_str(), std::fstream::out | std::fstream::app);
}
ret = 1;

ret = shift_file(tv);

pthread_mutex_unlock(&writelock);

Expand Down Expand Up @@ -122,8 +133,12 @@ int FileAppender::delete_old_log(timeval tv) {
old_tv.tv_usec = tv.tv_usec;
char old_file[100];
memset(old_file, 0, 100);
struct tm *tm;
tm = localtime(&old_tv.tv_sec);

struct tm t;
struct tm* tm = get_localtime(&old_tv.tv_sec, &t);
if (tm == NULL) {
return -1;
}
sprintf(old_file, "%s.%04d-%02d-%02d",
_log_file.c_str(), tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday - 1);
std::string old_file_path = _log_dir + "/" + old_file;
Expand Down Expand Up @@ -168,8 +183,11 @@ std::string _get_show_time(timeval tv) {
char show_time[40];
memset(show_time, 0, 40);

struct tm *tm;
tm = localtime(&tv.tv_sec);
struct tm t;
struct tm* tm = get_localtime(&tv.tv_sec, &t);
if (tm == NULL) {
return "unkonw time";
}

sprintf(show_time, "%04d-%02d-%02d %02d:%02d:%02d.%03d",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
Expand Down
1 change: 1 addition & 0 deletions src/simple_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class FileAppender {
int init(std::string dir, std::string file);
bool is_inited();
int write_log(char *log, const char *format, va_list ap);
int shift_file(struct timeval tv);
int shift_file_if_need(struct timeval tv, struct timezone tz);
int delete_old_log(timeval tv);
void set_retain_day(int rd);
Expand Down

0 comments on commit fecabdc

Please sign in to comment.