DPDK patches and discussions
 help / color / mirror / Atom feed
From: luzhipeng <luzhipeng@cestc.cn>
To: dev@dpdk.org
Cc: thomas@monjalon.net, zhipeng Lu <luzhipeng@cestc.cn>
Subject: [PATCH] log: add timestamp for log
Date: Fri,  3 Mar 2023 17:54:35 +0800	[thread overview]
Message-ID: <20230303095435.555-1-luzhipeng@cestc.cn> (raw)

From: zhipeng Lu <luzhipeng@cestc.cn>

add timestamp for log

Signed-off-by: zhipeng Lu <luzhipeng@cestc.cn>
---
 lib/eal/common/eal_common_log.c | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/lib/eal/common/eal_common_log.c b/lib/eal/common/eal_common_log.c
index bd7b188ceb..2b481116b6 100644
--- a/lib/eal/common/eal_common_log.c
+++ b/lib/eal/common/eal_common_log.c
@@ -480,6 +480,27 @@ rte_log_dump(FILE *f)
 	}
 }
 
+/* get timestamp*/
+void
+rte_log_get_timestamp_prefix(char *buf, int buf_size)
+{
+    struct tm *info;
+    char date[24];
+    struct timespec ts;
+    long usec;
+
+    clock_gettime(CLOCK_REALTIME, &ts);
+    info = localtime(&ts.tv_sec);
+    usec = ts.tv_nsec / 1000;
+    if (info == NULL) {
+        snprintf(buf, buf_size, "[%s.%06ld] ", "unknown date", usec);
+        return;
+    }
+
+    strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", info);
+    snprintf(buf, buf_size, "[%s.%06ld] ", date, usec);
+}
+
 /*
  * Generates a log message The message will be sent in the stream
  * defined by the previous call to rte_openlog_stream().
@@ -489,6 +510,7 @@ rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
 {
 	FILE *f = rte_log_get_stream();
 	int ret;
+	char timestamp[64];
 
 	if (logtype >= rte_logs.dynamic_types_len)
 		return -1;
@@ -498,7 +520,8 @@ rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
 	/* save loglevel and logtype in a global per-lcore variable */
 	RTE_PER_LCORE(log_cur_msg).loglevel = level;
 	RTE_PER_LCORE(log_cur_msg).logtype = logtype;
-
+	rte_log_get_timestamp_prefix(timestamp, sizeof(timestamp));
+	fprintf(f,"%s ",timestamp);
 	ret = vfprintf(f, format, ap);
 	fflush(f);
 	return ret;
-- 
2.31.1




             reply	other threads:[~2023-03-06  7:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-03  9:54 luzhipeng [this message]
2023-03-03 10:02 luzhipeng
2023-03-04  3:04 luzhipeng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230303095435.555-1-luzhipeng@cestc.cn \
    --to=luzhipeng@cestc.cn \
    --cc=dev@dpdk.org \
    --cc=thomas@monjalon.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).