feature add time

This commit is contained in:
tqcq 2023-12-11 15:35:08 +08:00
parent aeec5ebd92
commit a6599e9fdb

View File

@ -8,6 +8,7 @@
#include <sstream> #include <sstream>
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include <sys/time.h>
namespace tqcq { 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 * @brief time file:line@func tag level_name msg
*/ */
std::string pattern = "{} {}:{}@{} {} [{:<5}]: {}"; 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 // auto add CR
bool need_append_line_break = !msg || *msg == '\0' || msg[strlen(msg) - 1] != '\n'; bool need_append_line_break = !msg || *msg == '\0' || msg[strlen(msg) - 1] != '\n';
if (need_append_line_break) { if (need_append_line_break) {
pattern.append(1, '\n'); 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 void