-
Notifications
You must be signed in to change notification settings - Fork 361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Log datetime #2191
Log datetime #2191
Changes from all commits
40ccea7
5430bb4
d81c7cf
852b7b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,12 @@ | |
#include "Version.h" | ||
#include "config.h" | ||
|
||
#include <fmt/chrono.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This commit not only adds configurable time formats, but also migrates to fmt. Please don't do two things in one commit! If you believe fmt is better, you can suggest doing that. |
||
#include <cassert> | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
#include <time.h> | ||
#include <cstdio> | ||
#include <cstring> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why change this line? It's not related to the rest of this commit, is it? Sneaking in unrelated changes makes a PR very hard to read, and there isn't even a good reason to do so. |
||
#include <chrono> | ||
#include <functional> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this header? |
||
|
||
#ifdef HAVE_SYSLOG | ||
#include <syslog.h> | ||
|
@@ -50,7 +51,15 @@ ToAndroidLogLevel(LogLevel log_level) noexcept | |
|
||
static LogLevel log_threshold = LogLevel::NOTICE; | ||
|
||
static bool enable_timestamp; | ||
static bool enable_timestamp = false; | ||
|
||
static constexpr size_t LOG_DATE_BUF_SIZE = std::char_traits<char>::length("Jan 22 15:43:14.000 : ") + 1; | ||
|
||
template<typename DurationType> | ||
static const char * | ||
log_date(char buf[LOG_DATE_BUF_SIZE]); | ||
|
||
static auto log_time_formatter = log_date<std::chrono::seconds>; | ||
|
||
#ifdef HAVE_SYSLOG | ||
static bool enable_syslog; | ||
|
@@ -62,25 +71,56 @@ SetLogThreshold(LogLevel _threshold) noexcept | |
log_threshold = _threshold; | ||
} | ||
|
||
template <typename DurationType> | ||
inline constexpr const char *date_format_of = ""; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
template<> | ||
inline constexpr const char *date_format_of<std::chrono::minutes> = "{:%b %d %H:%M} : "; | ||
template<> | ||
inline constexpr const char *date_format_of<std::chrono::seconds> = "{:%b %d %H:%M:%S} : "; | ||
template<> | ||
inline constexpr const char *date_format_of<std::chrono::milliseconds> = "{:%b %d %H:%M:%S} : "; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wow, that must be the most complicated way to do this. What's the reason for making this so complicated. |
||
|
||
template<typename DurationType> | ||
static const char * | ||
log_date(char buf[LOG_DATE_BUF_SIZE]) | ||
{ | ||
using namespace std::chrono; | ||
|
||
/* | ||
* NOTE: fmt of chrono::time_point with %S writes | ||
* a floating point number of seconds if the | ||
* duration resolution is less than second | ||
Comment on lines
+91
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do I care? |
||
*/ | ||
auto [p, n] = fmt::format_to_n(buf, LOG_DATE_BUF_SIZE, date_format_of<DurationType>, | ||
floor<DurationType>(system_clock::now())); | ||
assert(n < LOG_DATE_BUF_SIZE); | ||
*p = 0; | ||
return buf; | ||
} | ||
|
||
static auto | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why |
||
null_log_time_formatter = [](char buf[LOG_DATE_BUF_SIZE]) -> const char* { | ||
buf[0] = 0; | ||
return buf; | ||
Comment on lines
+104
to
+105
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Too complicated. Why not |
||
}; | ||
|
||
void | ||
EnableLogTimestamp() noexcept | ||
EnableLogTimestamp(LogTimestamp _log_time_stamp) noexcept | ||
{ | ||
#ifdef HAVE_SYSLOG | ||
assert(!enable_syslog); | ||
#endif | ||
assert(!enable_timestamp); | ||
|
||
enable_timestamp = true; | ||
} | ||
|
||
static const char * | ||
log_date() noexcept | ||
{ | ||
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]; | ||
time_t t = time(nullptr); | ||
strftime(buf, LOG_DATE_BUF_SIZE, "%b %d %H:%M:%S : ", localtime(&t)); | ||
return buf; | ||
switch (_log_time_stamp) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
case LogTimestamp::MINUTES: log_time_formatter = log_date<std::chrono::minutes>; break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bad indent |
||
case LogTimestamp::SECONDS: log_time_formatter = log_date<std::chrono::seconds>; break; | ||
case LogTimestamp::MILLISECONDS: log_time_formatter = log_date<std::chrono::milliseconds>; break; | ||
case LogTimestamp::NONE: log_time_formatter = null_log_time_formatter; break; | ||
} | ||
} | ||
|
||
#ifdef HAVE_SYSLOG | ||
|
@@ -147,8 +187,9 @@ LogFinishSysLog() noexcept | |
static void | ||
FileLog(const Domain &domain, std::string_view message) noexcept | ||
{ | ||
char date_buf[LOG_DATE_BUF_SIZE]; | ||
fmt::print(stderr, "{}{}: {}\n", | ||
enable_timestamp ? log_date() : "", | ||
log_time_formatter(date_buf), | ||
domain.GetName(), | ||
StripRight(message)); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using custom_target with
cp
looks extremely fragile. Why can't we use Meson's standard commands for installing data files?