diff options
author | Toni Uhlig <matzeton@googlemail.com> | 2018-06-24 00:11:08 +0200 |
---|---|---|
committer | Toni Uhlig <matzeton@googlemail.com> | 2018-06-24 00:11:08 +0200 |
commit | 7107a975a849ff4a7c437a5490a5f4ddc9f9c9a9 (patch) | |
tree | 34cb470ac95c7b31c8d7de8be1e29bf41f039df0 /src | |
parent | 09b1850ff27e492d74791bdbf44d25154d007e98 (diff) |
added timestamps in log_colored and log_file modules
Signed-off-by: Toni Uhlig <matzeton@googlemail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/log.c | 17 | ||||
-rw-r--r-- | src/log.h | 3 | ||||
-rw-r--r-- | src/log_colored.c | 78 | ||||
-rw-r--r-- | src/log_colored.h | 2 | ||||
-rw-r--r-- | src/log_file.c | 85 |
5 files changed, 97 insertions, 88 deletions
@@ -1,4 +1,5 @@ #include <stdio.h> +#include <time.h> #include "log.h" @@ -8,3 +9,19 @@ log_close_cb log_close = NULL; log_fmt_cb log_fmt = NULL; log_fmtex_cb log_fmtex = NULL; log_fmtexerr_cb log_fmtexerr = NULL; + + +char * +curtime_str(char *buf, size_t siz) +{ + time_t t; + struct tm *tmp; + + t = time(NULL); + tmp = localtime(&t); + + if (!strftime(buf, siz, "%d %b %y - %H:%M:%S", tmp)) + snprintf(buf, siz, "%s", "UNKNOWN_TIME"); + + return buf; +} @@ -67,4 +67,7 @@ extern log_fmt_cb log_fmt; extern log_fmtex_cb log_fmtex; extern log_fmtexerr_cb log_fmtexerr; +char * +curtime_str(char *buf, size_t siz); + #endif diff --git a/src/log_colored.c b/src/log_colored.c index 5c989a1..fcf6b11 100644 --- a/src/log_colored.c +++ b/src/log_colored.c @@ -1,5 +1,7 @@ #ifdef HAVE_CONFIG_H #include "config.h" +#else +#define GETENV_FUNC getenv #endif #include <stdio.h> @@ -12,6 +14,20 @@ #include "log_colored.h" +#define LOG(time, facl, pid, out) { printf("[%s]" facl "[%d] %s\n", time, pid, out); } while(0) +#define LOGEX(time, facl, pid, src, line, out) \ + { printf("[%s]" facl "[%d] %s.%zu: %s\n", time, pid, src, line, out); } while(0) +#define LOGEXERR(time, facl, pid, src, line, out, serrno) \ + { \ + if (serrno) { \ + printf("[%s]" facl "[%d] %s.%zu: %s failed: %s\n", \ + time, pid, src, line, out, strerror(serrno)); \ + } else { \ + printf("[%s]" facl "[%d] %s.%zu: %s failed\n", \ + time, pid, src, line, out); \ + } \ + } while(0) + int log_open_colored(void) { @@ -36,6 +52,7 @@ void log_close_colored(void) void log_fmt_colored(log_priority prio, const char *fmt, ...) { pid_t my_pid; + char time[64]; char out[LOGMSG_MAXLEN+1] = {0}; va_list arglist; @@ -47,21 +64,22 @@ void log_fmt_colored(log_priority prio, const char *fmt, ...) va_end(arglist); my_pid = getpid(); + curtime_str(time, sizeof time); switch (prio) { case DEBUG: - printf("[DEBUG] [%d] %s\n", my_pid, out); + LOG(time, "[DEBUG] ", my_pid, out); break; case NOTICE: - printf("[" GRN "NOTICE" RESET "] [%d] %s\n", my_pid, out); + LOG(time, "[" GRN "NOTICE" RESET "] ", my_pid, out); break; case WARNING: - printf("[" YEL "WARNING" RESET "][%d] %s\n", my_pid, out); + LOG(time, "[" YEL "WARNING" RESET "]", my_pid, out); break; case ERROR: - printf("[" RED "ERROR" RESET "] [%d] %s\n", my_pid, out); + LOG(time, "[" RED "ERROR" RESET "] ", my_pid, out); break; case CMD: - printf("[" BLUE "CMD" RESET "] [%d] %s\n", my_pid, out); + LOG(time, "[" BLU "CMD" RESET "] ", my_pid, out); break; } } @@ -70,6 +88,7 @@ void log_fmtex_colored(log_priority prio, const char *srcfile, size_t line, const char *fmt, ...) { pid_t my_pid; + char time[64]; char out[LOGMSG_MAXLEN+1] = {0}; va_list arglist; @@ -81,21 +100,19 @@ void log_fmtex_colored(log_priority prio, const char *srcfile, va_end(arglist); my_pid = getpid(); + curtime_str(time, sizeof time); switch (prio) { case DEBUG: - printf("[DEBUG] [%d] %s.%zu: %s\n", my_pid, srcfile, line, out); + LOGEX(time, "[DEBUG] ", my_pid, srcfile, line, out); break; case NOTICE: - printf("[" GRN "NOTICE" RESET "] [%d] %s.%zu: %s\n", - my_pid, srcfile, line, out); + LOGEX(time, "[" GRN "NOTICE" RESET "] ", my_pid, srcfile, line, out); break; case WARNING: - printf("[" YEL "WARNING" RESET "][%d] %s.%zu: %s\n", - my_pid, srcfile, line, out); + LOGEX(time, "[" YEL "WARNING" RESET "]", my_pid, srcfile, line, out); break; case ERROR: - printf("[" RED "ERROR" RESET "] [%d] %s.%zu: %s\n", - my_pid, srcfile, line, out); + LOGEX(time, "[" RED "ERROR" RESET "] ", my_pid, srcfile, line, out); break; case CMD: break; @@ -107,6 +124,7 @@ void log_fmtexerr_colored(log_priority prio, const char *srcfile, { pid_t my_pid; int saved_errno = errno; + char time[64]; char out[LOGMSG_MAXLEN+1] = {0}; va_list arglist; @@ -118,45 +136,19 @@ void log_fmtexerr_colored(log_priority prio, const char *srcfile, va_end(arglist); my_pid = getpid(); + curtime_str(time, sizeof time); switch (prio) { case DEBUG: - if (saved_errno) - printf("[DEBUG] [%d] %s.%zu: %s failed: %s\n", - my_pid, srcfile, line, out, - strerror(saved_errno)); - else - printf("[DEBUG] [%d] %s.%zu: %s failed\n", - my_pid, srcfile, line, out); + LOGEXERR(time, "[DEBUG] ", my_pid, srcfile, line, out, saved_errno); break; case NOTICE: - if (saved_errno) - printf("[" GRN "NOTICE" RESET "] [%d] %s.%zu: %s failed: %s\n", - my_pid, srcfile, - line, out, strerror(saved_errno)); - else - printf("[" GRN "NOTICE" RESET "] [%d] %s.%zu: %s failed\n", - my_pid, srcfile, - line, out); + LOGEXERR(time, "[" GRN "NOTICE" RESET "] ", my_pid, srcfile, line, out, saved_errno); break; case WARNING: - if (saved_errno) - printf("[" YEL "WARNING" RESET "][%d] %s.%zu: %s failed: %s\n", - my_pid, srcfile, - line, out, strerror(saved_errno)); - else - printf("[" YEL "WARNING" RESET "][%d] %s.%zu: %s failed\n", - my_pid, srcfile, - line, out); + LOGEXERR(time, "[" YEL "WARNING" RESET "]", my_pid, srcfile, line, out, saved_errno); break; case ERROR: - if (saved_errno) - printf("[" RED "ERROR" RESET "] [%d] %s.%zu: %s failed: %s\n", - my_pid, srcfile, - line, out, strerror(saved_errno)); - else - printf("[" RED "ERROR" RESET "] [%d] %s.%zu: %s failed\n", - my_pid, srcfile, - line, out); + LOGEXERR(time, "[" RED "ERROR" RESET "] ", my_pid, srcfile, line, out, saved_errno); break; case CMD: break; diff --git a/src/log_colored.h b/src/log_colored.h index b372c9b..422cbe7 100644 --- a/src/log_colored.h +++ b/src/log_colored.h @@ -8,7 +8,7 @@ #define GRN "\x1B[32;1m" #define YEL "\x1B[33;1m" #define RED "\x1B[31;1;5m" -#define BLUE "\x1B[34;1;1m" +#define BLU "\x1B[34;1;1m" /* LOG_SET_FUNCS comfort */ #define LOG_COLORED_FUNCS log_open_colored, log_close_colored, \ log_fmt_colored, log_fmtex_colored, log_fmtexerr_colored diff --git a/src/log_file.c b/src/log_file.c index c9c9f12..27ad0a7 100644 --- a/src/log_file.c +++ b/src/log_file.c @@ -7,6 +7,24 @@ #include "log_file.h" +#define LOG(fp, time, facl, pid, out) \ + { fprintf(fp, "[%s]" facl "[%d] %s\n", time, pid, out); } while(0) +#define LOGEX(fp, time, facl, pid, src, line, out) \ + { \ + fprintf(fp, "[%s]" facl "[%d] %s.%zu: %s\n", \ + time, pid, src, line, out); \ + } while(0) +#define LOGEXERR(fp, time, facl, pid, src, line, out, serrno) \ + { \ + if (serrno) { \ + fprintf(fp, "[%s]" facl "[%d] %s.%zu: %s failed: %s\n", \ + time, pid, src, line, out, strerror(serrno)); \ + } else { \ + fprintf(fp, "[%s]" facl "[%d] %s.%zu: %s failed\n", \ + time, pid, src, line, out); \ + } \ + } while(0) + char *log_file = NULL; static FILE *flog = NULL; @@ -42,6 +60,7 @@ void log_close_file(void) void log_fmt_file(log_priority prio, const char *fmt, ...) { pid_t my_pid; + char time[64]; char out[LOGMSG_MAXLEN+1] = {0}; va_list arglist; @@ -53,21 +72,22 @@ void log_fmt_file(log_priority prio, const char *fmt, ...) va_end(arglist); my_pid = getpid(); + curtime_str(time, sizeof time); switch (prio) { case DEBUG: - fprintf(flog, "[DEBUG] [%d] %s\n", my_pid, out); + LOG(flog, time, "[DEBUG] ", my_pid, out); break; case NOTICE: - fprintf(flog, "[NOTICE] [%d] %s\n", my_pid, out); + LOG(flog, time, "[NOTICE] ", my_pid, out); break; case WARNING: - fprintf(flog, "[WARNING][%d] %s\n", my_pid, out); + LOG(flog, time, "[WARNING]", my_pid, out); break; case ERROR: - fprintf(flog, "[ERROR] [%d] %s\n", my_pid, out); + LOG(flog, time, "[ERROR] ", my_pid, out); break; case CMD: - fprintf(flog, "[CMD] [%d] %s\n", my_pid, out); + LOG(flog, time, "[CMD] ", my_pid, out); break; } } @@ -76,6 +96,7 @@ void log_fmtex_file(log_priority prio, const char *srcfile, size_t line, const char *fmt, ...) { pid_t my_pid; + char time[64]; char out[LOGMSG_MAXLEN+1] = {0}; va_list arglist; @@ -87,22 +108,19 @@ void log_fmtex_file(log_priority prio, const char *srcfile, va_end(arglist); my_pid = getpid(); + curtime_str(time, sizeof time); switch (prio) { case DEBUG: - fprintf(flog, "[DEBUG] [%d] %s.%zu: %s\n", my_pid, srcfile, - line, out); + LOGEX(flog, time, "[DEBUG] ", my_pid, srcfile, line, out); break; case NOTICE: - fprintf(flog, "[NOTICE] [%d] %s.%zu: %s\n", - my_pid, srcfile, line, out); + LOGEX(flog, time, "[NOTICE] ", my_pid, srcfile, line, out); break; case WARNING: - fprintf(flog, "[WARNING][%d] %s.%zu: %s\n", - my_pid, srcfile, line, out); + LOGEX(flog, time, "[WARNING]", my_pid, srcfile, line, out); break; case ERROR: - fprintf(flog, "[ERROR] [%d] %s.%zu: %s\n", - my_pid, srcfile, line, out); + LOGEX(flog, time, "[ERROR] ", my_pid, srcfile, line, out); break; case CMD: break; @@ -114,6 +132,7 @@ void log_fmtexerr_file(log_priority prio, const char *srcfile, { pid_t my_pid; int saved_errno = errno; + char time[64]; char out[LOGMSG_MAXLEN+1] = {0}; va_list arglist; @@ -125,45 +144,23 @@ void log_fmtexerr_file(log_priority prio, const char *srcfile, va_end(arglist); my_pid = getpid(); + curtime_str(time, sizeof time); switch (prio) { case DEBUG: - if (saved_errno) - fprintf(flog, "[DEBUG] [%d] %s.%zu: %s failed: %s\n", - my_pid, srcfile, line, out, - strerror(saved_errno)); - else - fprintf(flog, "[DEBUG] [%d] %s.%zu: %s failed\n", - my_pid, srcfile, line, out); + LOGEXERR(flog, time, "[DEBUG] ", my_pid, srcfile, line, out, + saved_errno); break; case NOTICE: - if (saved_errno) - fprintf(flog, "[NOTICE] [%d] %s.%zu: %s failed: %s\n", - my_pid, srcfile, - line, out, strerror(saved_errno)); - else - fprintf(flog, "[NOTICE] [%d] %s.%zu: %s failed\n", - my_pid, srcfile, - line, out); + LOGEXERR(flog, time, "[NOTICE] ", my_pid, srcfile, line, out, + saved_errno); break; case WARNING: - if (saved_errno) - fprintf(flog, "[WARNING][%d] %s.%zu: %s failed: %s\n", - my_pid, srcfile, - line, out, strerror(saved_errno)); - else - fprintf(flog, "[WARNING][%d] %s.%zu: %s failed\n", - my_pid, srcfile, - line, out); + LOGEXERR(flog, time, "[WARNING]", my_pid, srcfile, line, out, + saved_errno); break; case ERROR: - if (saved_errno) - fprintf(flog, "[ERROR] [%d] %s.%zu: %s failed: %s\n", - my_pid, srcfile, - line, out, strerror(saved_errno)); - else - fprintf(flog, "[ERROR] [%d] %s.%zu: %s failed\n", - my_pid, srcfile, - line, out); + LOGEXERR(flog, time, "[ERROR] ", my_pid, srcfile, line, out, + saved_errno); break; case CMD: break; |