Skip to content

Commit a138269

Browse files
committed
date output in log in format YY.DD.MM-HH:MM:SS.microsecond
1 parent 21bb200 commit a138269

1 file changed

Lines changed: 31 additions & 7 deletions

File tree

libdll_log.h

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ static inline void dll_log_file_open() {
209209
timestamp_buff, sizeof(timestamp_buff) - 1, "%y-%m-%d-%T", localtime(&tv.tv_sec));
210210
snprintf(logfilename_buff,
211211
sizeof(logfilename_buff) - 1,
212-
"./%s_%s_%s.%03d.log",
212+
"./%s.%s.%s.%03d.log",
213213
LIBDLL_LOG_BASE_FILENAME,
214214
dll_log_src_filename_get(),
215215
timestamp_buff,
@@ -257,6 +257,7 @@ static inline char * dll_log_get_severity_arg(_dll_log_severity_t sev) {
257257
static inline void dll_log_prefix(_dll_log_severity_t sev,
258258
const char * func,
259259
const char * restrict postfix) {
260+
static char old_timestamp[128];
260261
static char prefix_fmt[256];
261262

262263
const char * restrict depth_fmt = dll_log_get_method_depth_fmt();
@@ -265,18 +266,41 @@ static inline void dll_log_prefix(_dll_log_severity_t sev,
265266
const char * const restrict sev_fmt = dll_log_get_severity_fmt(sev);
266267
const char * const restrict sev_arg = dll_log_get_severity_arg(sev);
267268

269+
struct timeval tv;
270+
char new_timestamp_buff[128];
271+
272+
gettimeofday(&tv, NULL);
273+
int millisec = tv.tv_usec / 1000.0;
274+
if (millisec >= 1000) {
275+
millisec -= 1000;
276+
tv.tv_sec++;
277+
}
278+
279+
strftime(new_timestamp_buff,
280+
sizeof(new_timestamp_buff) - 1,
281+
"%y.%m.%d-%T",
282+
localtime(&tv.tv_sec));
283+
284+
const bool is_timestamp_same =
285+
old_timestamp[0] ? !strcmp(old_timestamp, new_timestamp_buff) : false;
286+
268287
prefix_fmt[0] = 0;
269288

270-
const size_t prefix_fmt_written_len = snprintf(prefix_fmt,
271-
sizeof(prefix_fmt) - 1,
272-
"%s%s %%s%s ",
273-
depth_fmt,
274-
sev_fmt,
275-
postfix ? postfix : "");
289+
const size_t prefix_fmt_written_len =
290+
snprintf(prefix_fmt,
291+
sizeof(prefix_fmt) - 1,
292+
"%s%s %-17s.%03ld ms⎹ %%s%s ",
293+
depth_fmt,
294+
sev_fmt,
295+
!is_timestamp_same ? new_timestamp_buff : "",
296+
tv.tv_usec,
297+
postfix ? postfix : "");
276298

277299
prefix_fmt[prefix_fmt_written_len] = 0;
278300

279301
dll_log_fprintf(prefix_fmt, depth_arg, sev_arg, func);
302+
303+
strcpy(old_timestamp, new_timestamp_buff);
280304
}
281305

282306
static inline void

0 commit comments

Comments
 (0)