Skip to content

Commit

Permalink
Merge pull request #2386 from metalefty/log_time
Browse files Browse the repository at this point in the history
log: change date format to ISO8601-like
  • Loading branch information
metalefty authored Oct 14, 2022
2 parents 042317a + 662011a commit 5b51010
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
63 changes: 45 additions & 18 deletions common/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <config_ac.h>
#endif

#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
Expand Down Expand Up @@ -1010,12 +1011,10 @@ internal_log_message(const enum logLevels lvl,
const char *msg,
va_list ap)
{
char buff[LOG_BUFFER_SIZE + 31]; /* 19 (datetime) 4 (space+cr+lf+\0) */
char buff[LOG_BUFFER_SIZE + 43]; /* 31 ("[2022-10-07T19:58:33.065+0900] ") + 8 (log level) + 4 (space+cr+lf+\0) */
int len = 0;
enum logReturns rv = LOG_STARTUP_OK;
int writereply = 0;
time_t now_t;
struct tm *now;

if (g_staticLogConfig == NULL)
{
Expand All @@ -1035,20 +1034,18 @@ internal_log_message(const enum logLevels lvl,
return LOG_STARTUP_OK;
}

now_t = time(&now_t);
now = localtime(&now_t);

strftime(buff, 21, "[%Y%m%d-%H:%M:%S] ", now);
getFormattedDateTime(buff, 32);

internal_log_lvl2str(lvl, buff + 20);
internal_log_lvl2str(lvl, buff + 31);

if (g_staticLogConfig->enable_pid)
{
g_snprintf(buff + 28, LOG_BUFFER_SIZE, "[pid:%d tid:%lld] ",
/* 31 (datetime) + 8 (log level) = 39 */
g_snprintf(buff + 39, LOG_BUFFER_SIZE, "[pid:%d tid:%lld] ",
g_getpid(), (long long) tc_get_threadid());
len = g_strlen(buff + 28);
len = g_strlen(buff + 39);
}
len += vsnprintf(buff + 28 + len, LOG_BUFFER_SIZE - len, msg, ap);
len += vsnprintf(buff + 39 + len, LOG_BUFFER_SIZE - len, msg, ap);

/* checking for truncated messages */
if (len > LOG_BUFFER_SIZE)
Expand All @@ -1058,17 +1055,18 @@ internal_log_message(const enum logLevels lvl,
}

/* forcing the end of message string */
/* 31 (datetime) + 8 (log level) = 39 */
#ifdef _WIN32
buff[len + 28] = '\r';
buff[len + 29] = '\n';
buff[len + 30] = '\0';
buff[len + 39] = '\r';
buff[len + 40] = '\n';
buff[len + 41] = '\0';
#else
#ifdef _MACOS
buff[len + 28] = '\r';
buff[len + 29] = '\0';
buff[len + 39] = '\r';
buff[len + 40] = '\0';
#else
buff[len + 28] = '\n';
buff[len + 29] = '\0';
buff[len + 39] = '\n';
buff[len + 40] = '\0';
#endif
#endif

Expand Down Expand Up @@ -1140,3 +1138,32 @@ getLogFile(char *replybuf, int bufsize)

return replybuf;
}

/**
* Returns formatted datetime for log
* @return
*/
char *
getFormattedDateTime(char *replybuf, int bufsize)
{
char buf_datetime[21]; /* 2022-10-07T16:36:04 + . */
char buf_millisec[4]; /* 357 */
char buf_timezone[6]; /* +0900 */

struct tm *now;
struct timeval tv;
int millisec;

gettimeofday(&tv, NULL);
now = localtime(&tv.tv_sec);

millisec = (tv.tv_usec + 500 / 1000);
g_snprintf(buf_millisec, sizeof(buf_millisec), "%03d", millisec);

strftime(buf_datetime, sizeof(buf_datetime), "%FT%T.", now);
strftime(buf_timezone, sizeof(buf_timezone), "%z", now);
g_snprintf(replybuf, bufsize, "[%s%s%s] ", buf_datetime, buf_millisec, buf_timezone);

return replybuf;
}

6 changes: 6 additions & 0 deletions common/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,4 +434,10 @@ log_hexdump_with_location(const char *function_name,
* @return
*/
char *getLogFile(char *replybuf, int bufsize);

/**
* Returns formatted datetime for log
* @return
*/
char *getFormattedDateTime(char *replybuf, int bufsize);
#endif

0 comments on commit 5b51010

Please sign in to comment.