From: Hitoshi Mitake <[email protected]>

This patch makes logger to add date and time to log messages even if
an output is stdout. It can be useful for debugging.

Signed-off-by: Hitoshi Mitake <[email protected]>
---
 lib/logger.c |   23 +++++++++++++++++++++++
 1 files changed, 23 insertions(+), 0 deletions(-)

diff --git a/lib/logger.c b/lib/logger.c
index 094b453..8511ac9 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -150,12 +150,35 @@ static void notrace free_logarea(void)
        shmdt(la);
 }
 
+static const char *month(int m)
+{
+       const char *s[] = {
+               "Jan", "Feb", "Mar", "Apr", "May", "Jun",
+               "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
+       };
+
+       if (!(1 <= m && m <= 12))
+               return "Invalid month";
+
+       return s[m - 1];
+}
+
 static notrace int log_vsnprintf(char *buff, size_t size, int prio,
                                 const char *func, int line, const char *fmt,
                                 va_list ap)
 {
        char *p = buff;
 
+       if (!la) {
+               time_t t = time(NULL);
+               struct tm tm;
+               gmtime_r(&t, &tm);
+
+               snprintf(p, size, "%s %02d %02d:%02d:%02d ", month(tm.tm_mon),
+                       tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
+               p += strlen(p);
+       }
+
        if (worker_name && worker_idx)
                snprintf(p, size, "[%s %d] ", worker_name, worker_idx);
        else if (worker_name)
-- 
1.7.2.5

-- 
sheepdog mailing list
[email protected]
http://lists.wpkg.org/mailman/listinfo/sheepdog

Reply via email to