From a6599e9fdb5d2d99ddedfb7e6e65f39bd8eae202 Mon Sep 17 00:00:00 2001 From: tqcq Date: Mon, 11 Dec 2023 15:35:08 +0800 Subject: [PATCH] feature add time --- src/ulib/log/logger.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ulib/log/logger.cpp b/src/ulib/log/logger.cpp index 3c3fc13..2575bab 100644 --- a/src/ulib/log/logger.cpp +++ b/src/ulib/log/logger.cpp @@ -8,6 +8,7 @@ #include #include #include +#include namespace tqcq { @@ -30,13 +31,23 @@ Logger::Log(int32_t level, const char *file, const char *func, int32_t line, con * @brief time file:line@func tag level_name msg */ std::string pattern = "{} {}:{}@{} {} [{:<5}]: {}"; + std::string log_time; + { + std::time_t now = time(NULL); + std::tm* timeinfo = std::localtime(&now); + + struct timeval timeval_now; + gettimeofday(&timeval_now, NULL); + + log_time = fmt::format("{:02}-{:02} {:02}:{:02}:{:02}.{:06}", timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, timeval_now.tv_usec); + } // auto add CR bool need_append_line_break = !msg || *msg == '\0' || msg[strlen(msg) - 1] != '\n'; if (need_append_line_break) { pattern.append(1, '\n'); } - fmt::print(pattern, "", file + stripped_prefix_len_, line, func, tag, level_name, msg); + fmt::print(pattern, log_time, file + stripped_prefix_len_, line, func, tag, level_name, msg); } void