From patchwork Sat Mar 4 04:02:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: luzhipeng X-Patchwork-Id: 124802 X-Patchwork-Delegate: thomas@monjalon.net Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 807F041DCB; Sat, 4 Mar 2023 05:03:16 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 159C240E25; Sat, 4 Mar 2023 05:03:16 +0100 (CET) Received: from smtp.cecloud.com (unknown [1.203.97.240]) by mails.dpdk.org (Postfix) with ESMTP id 26935400D7 for ; Sat, 4 Mar 2023 05:03:13 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by smtp.cecloud.com (Postfix) with ESMTP id 01832900112 for ; Sat, 4 Mar 2023 12:03:11 +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 P141548T281468232003952S1677902589456707_; Sat, 04 Mar 2023 12:03:10 +0800 (CST) X-IP-DOMAINF: 1 X-UNIQUE-TAG: <34463791de791713ca805a6f264f5213> 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 v2] log: add timestamp for log Date: Sat, 4 Mar 2023 12:02:46 +0800 Message-Id: <20230304040246.319-1-luzhipeng@cestc.cn> X-Mailer: git-send-email 2.34.0.windows.1 MIME-Version: 1.0 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 | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/eal/common/eal_common_log.c b/lib/eal/common/eal_common_log.c index bd7b188ceb..0a21572954 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,8 +520,9 @@ 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; - - ret = vfprintf(f, format, ap); + rte_log_get_timestamp_prefix(timestamp, sizeof(timestamp)); + fprintf(f, "%s ", timestamp); + ret = vfprintf(f, format, ap); fflush(f); return ret; }