[sheepdog] [PATCH] logger: add date and time to log messages even if an output is stdout
Hitoshi Mitake
mitake.hitoshi at lab.ntt.co.jp
Thu Dec 20 10:48:37 CET 2012
From: Hitoshi Mitake <h.mitake at gmail.com>
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 <mitake.hitoshi at lab.ntt.co.jp>
---
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
More information about the sheepdog
mailing list