From 7a6129861ecca5bd0dc6ee1eff2062e7a073ec0b Mon Sep 17 00:00:00 2001 From: vraatikka Date: Sat, 27 Jul 2013 12:14:30 +0300 Subject: [PATCH] Added timestamp to log file headers and to the beginning of each line. Removed additional line feeds. Example: SkySQL MaxScale Sat Jul 27 12:13:10 2013 ------------------------------------------ 2013 07/27 12:13:10 Loaded module testroute: V1.0.0 2013 07/27 12:13:10 Initialise debug CLI router module V1.0.1. 2013 07/27 12:13:10 Loaded module debugcli: V1.0.1 --- log_manager/log_manager.cc | 40 +++++++++++++++++++++++++++++--------- log_manager/test/testlog.c | 17 +++++++++++++++- utils/skygw_utils.cc | 33 ++++++++++++++++++------------- 3 files changed, 67 insertions(+), 23 deletions(-) diff --git a/log_manager/log_manager.cc b/log_manager/log_manager.cc index c9533d698..9d66e43ca 100644 --- a/log_manager/log_manager.cc +++ b/log_manager/log_manager.cc @@ -31,6 +31,7 @@ #define MAX_SUFFIXLEN 250 #define MAX_PATHLEN 512 #define MAXNBLOCKBUFS 10 + /** * BUFSIZ comes from the system. It equals with block size or * its multiplication. @@ -500,10 +501,14 @@ static int logmanager_write_log( char* str, va_list valist) { - logfile_t* lf; - char* wp; - int err = 0; - blockbuf_t* bb; + logfile_t* lf; + char* wp; + int err = 0; + blockbuf_t* bb; + time_t t; + struct tm tm; + const char* timestamp_formatstr = "%04d %02d/%02d %02d:%02d:%02d "; + const int timestamp_len = 4+1+2+1+2+1+2+1+2+1+2+3; CHK_LOGMANAGER(lm); @@ -543,14 +548,31 @@ static int logmanager_write_log( * Seek write position and register to block buffer. * Then print formatted string to write position. */ - wp = blockbuf_get_writepos(&bb, id, str_len, flush); - + wp = blockbuf_get_writepos(&bb, id, timestamp_len-1+str_len, flush); + /** Generate timestamp */ + t = time(NULL); + tm = *(localtime(&t)); + snprintf(wp, + timestamp_len, + timestamp_formatstr, + tm.tm_year+1900, + tm.tm_mon+1, + tm.tm_mday, + tm.tm_hour, + tm.tm_min, + tm.tm_sec); + /** + * Write next string to overwrite terminating null character of the + * timestamp string. + */ if (use_valist) { - vsnprintf(wp, str_len, str, valist); + vsnprintf(wp+timestamp_len-1, str_len, str, valist); } else { - snprintf(wp, str_len, str); + snprintf(wp+timestamp_len-1, str_len, str); + } + if (wp[timestamp_len-1+str_len-2] != '\n') { + wp[timestamp_len-1+str_len-1]='\n'; } - wp[str_len-1]='\n'; /** lock-free unregistration, includes flush if bb_isfull */ blockbuf_unregister(bb); diff --git a/log_manager/test/testlog.c b/log_manager/test/testlog.c index ee3dabe8c..28c02ba01 100644 --- a/log_manager/test/testlog.c +++ b/log_manager/test/testlog.c @@ -40,9 +40,10 @@ static void* thr_run_morelog(void* data); #define NTHR 256 #define NITER 100 +#if 0 #define TEST1 #define TEST2 - +#endif int main(int argc, char* argv[]) { int err = 0; @@ -54,6 +55,8 @@ int main(int argc, char* argv[]) simple_mutex_t* mtx; size_t nactive; thread_t* thr[NTHR]; + time_t t; + struct tm tm; i = atexit(skygw_logmanager_exit); @@ -64,6 +67,18 @@ int main(int argc, char* argv[]) r = skygw_logmanager_init(NULL, argc, argv); ss_dassert(r); + t = time(NULL); + tm = *(localtime(&t)); + err = skygw_log_write_flush(NULL, + LOGFILE_ERROR, + "%04d %02d/%02d %02d.%02d.%02d", + tm.tm_year+1900, + tm.tm_mon+1, + tm.tm_mday, + tm.tm_hour, + tm.tm_min, + tm.tm_sec); + skygw_logmanager_init(NULL, argc, argv); logstr = ("First write with flush."); err = skygw_log_write_flush(NULL, LOGFILE_ERROR, logstr); diff --git a/utils/skygw_utils.cc b/utils/skygw_utils.cc index b7bf45d63..043cd7a32 100644 --- a/utils/skygw_utils.cc +++ b/utils/skygw_utils.cc @@ -1498,15 +1498,18 @@ return_mes_rc: static bool file_write_header( skygw_file_t* file) { - bool succp = FALSE; - size_t wbytes1; - size_t wbytes2; - size_t len1; - size_t len2; - const char* header_buf1; - char* header_buf2 = NULL; - time_t* t; - struct tm* tm; + bool succp = FALSE; + size_t wbytes1; + size_t wbytes2; + size_t wbytes3; + size_t len1; + size_t len2; + size_t len3; + const char* header_buf1; + char* header_buf2 = NULL; + const char* header_buf3; + time_t* t; + struct tm* tm; t = (time_t *)malloc(sizeof(time_t)); tm = (struct tm *)malloc(sizeof(struct tm)); @@ -1514,25 +1517,29 @@ static bool file_write_header( *tm = *localtime(t); CHK_FILE(file); - header_buf1 = "\n----------\nSkySQL MaxScale "; - header_buf2 = strdup(asctime(tm)); + header_buf1 = "\n\nSkySQL MaxScale\t"; + header_buf2 = strdup(asctime(tm)); + header_buf3 = "------------------------------------------\n"; if (header_buf2 == NULL) { goto return_succp; } len1 = strlen(header_buf1); len2 = strlen(header_buf2); + len3 = strlen(header_buf3); #if defined(LAPTOP_TEST) usleep(DISKWRITE_LATENCY); #else wbytes1=fwrite((void*)header_buf1, len1, 1, file->sf_file); wbytes2=fwrite((void*)header_buf2, len2, 1, file->sf_file); + wbytes3=fwrite((void*)header_buf3, len3, 1, file->sf_file); - if (wbytes1 != 1 || wbytes2 != 1) { + if (wbytes1 != 1 || wbytes2 != 1 || wbytes3 != 1) { fprintf(stderr, - "Writing header %s %s to %s failed.\n", + "Writing header %s %s %s to %s failed.\n", header_buf1, header_buf2, + header_buf3, file->sf_fname); perror("Logfile header write.\n"); goto return_succp;