LogBackend: use fmt to format the time stamp

This commit is contained in:
Max Kellermann 2025-03-13 08:42:27 +01:00
parent 4f981adb97
commit 093122aaeb

@ -3,12 +3,15 @@
#include "LogBackend.hxx" #include "LogBackend.hxx"
#include "Log.hxx" #include "Log.hxx"
#include "lib/fmt/Unsafe.hxx"
#include "util/Compiler.h" #include "util/Compiler.h"
#include "util/Domain.hxx" #include "util/Domain.hxx"
#include "util/StringStrip.hxx" #include "util/StringStrip.hxx"
#include "Version.h" #include "Version.h"
#include "config.h" #include "config.h"
#include <fmt/chrono.h>
#include <cassert> #include <cassert>
#include <stdio.h> #include <stdio.h>
@ -19,6 +22,8 @@
#include <syslog.h> #include <syslog.h>
#endif #endif
using std::string_view_literals::operator""sv;
#ifdef ANDROID #ifdef ANDROID
#include <android/log.h> #include <android/log.h>
#include "android/LogListener.hxx" #include "android/LogListener.hxx"
@ -73,14 +78,13 @@ EnableLogTimestamp() noexcept
enable_timestamp = true; enable_timestamp = true;
} }
static const char * static std::string_view
log_date() noexcept log_date() noexcept
{ {
static constexpr size_t LOG_DATE_BUF_SIZE = std::char_traits<char>::length("Jan 22 15:43:14 : ") + 1; static constexpr size_t LOG_DATE_BUF_SIZE = std::char_traits<char>::length("Jan 22 15:43:14 : ") + 1;
static char buf[LOG_DATE_BUF_SIZE]; static char buf[LOG_DATE_BUF_SIZE];
time_t t = time(nullptr); time_t t = time(nullptr);
strftime(buf, LOG_DATE_BUF_SIZE, "%b %d %H:%M:%S : ", localtime(&t)); return FmtUnsafeSV(buf, "{:%b %d %H:%M:%S} : "sv, fmt::localtime(t));
return buf;
} }
#ifdef HAVE_SYSLOG #ifdef HAVE_SYSLOG
@ -148,7 +152,7 @@ static void
FileLog(const Domain &domain, std::string_view message) noexcept FileLog(const Domain &domain, std::string_view message) noexcept
{ {
fmt::print(stderr, "{}{}: {}\n", fmt::print(stderr, "{}{}: {}\n",
enable_timestamp ? log_date() : "", enable_timestamp ? log_date() : ""sv,
domain.GetName(), domain.GetName(),
StripRight(message)); StripRight(message));