From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 6804841DD2; Sat, 4 Mar 2023 09:14:56 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 4A1FF40ED4; Sat, 4 Mar 2023 09:14:56 +0100 (CET) Received: from smtp.cecloud.com (unknown [1.203.97.246]) by mails.dpdk.org (Postfix) with ESMTP id AE8A440687 for ; Sat, 4 Mar 2023 09:14:54 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by smtp.cecloud.com (Postfix) with ESMTP id 9BA517C0112 for ; Sat, 4 Mar 2023 16:14:51 +0800 (CST) X-MAIL-GRAY: 0 X-MAIL-DELIVERY: 1 X-ANTISPAM-LEVEL: 2 X-ABS-CHECKED: 0 Received: from localhost.localdomain (unknown [171.217.50.114]) by smtp.cecloud.com (postfix) whith ESMTP id P250386T281460407071088S1677917686057689_; Sat, 04 Mar 2023 16:14:48 +0800 (CST) X-IP-DOMAINF: 1 X-UNIQUE-TAG: X-RL-SENDER: luzhipeng@cestc.cn X-SENDER: luzhipeng@cestc.cn X-LOGIN-NAME: luzhipeng@cestc.cn X-FST-TO: dev@dpdk.org X-RCPT-COUNT: 3 X-SENDER-IP: 171.217.50.114 X-ATTACHMENT-NUM: 0 X-System-Flag: 0 From: luzhipeng To: dev@dpdk.org Cc: thomas@monjalon.net, Zhipeng Lu Subject: [PATCH v3] log: add timestamp for log Date: Sat, 4 Mar 2023 16:14:21 +0800 Message-Id: <20230304081421.1352-1-luzhipeng@cestc.cn> X-Mailer: git-send-email 2.34.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org From: Zhipeng Lu add timestamp for log Signed-off-by: Zhipeng Lu --- 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..46dbd4b7bf 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*/ +static 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