Skip to content

Commit

Permalink
wztime: Add option to silence debug log output
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Nov 28, 2023
1 parent 3c71f87 commit 29e7469
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/framework/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ void _debug(int line, code_part part, const char *function, const char *str, ...
char ourtime[15]; //HH:MM:SS

time(&rawtime);
timeinfo = getLocalTime(rawtime);
timeinfo = getLocalTime(rawtime, true);
strftime(ourtime, 15, "%H:%M:%S", &timeinfo);

auto& currInputBuffer = inputBuffer[useInputBuffer1 ? 1 : 0];
Expand Down
22 changes: 14 additions & 8 deletions lib/framework/wztime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,40 @@ tm getUtcTime(std::time_t const &timer)
return timeinfo;
}

optional<tm> getLocalTimeOpt(std::time_t const &timer)
optional<tm> getLocalTimeOpt(std::time_t const &timer, bool quiet)
{
struct tm timeinfo = {};
#if defined(WZ_OS_WIN)
errno_t result = localtime_s(&timeinfo, &timer);
if (result != 0)
{
char sys_msg[80];
if (strerror_s(sys_msg, sizeof(sys_msg), result) != 0)
if (!quiet)
{
strncpy(sys_msg, "unknown error", sizeof(sys_msg));
char sys_msg[80];
if (strerror_s(sys_msg, sizeof(sys_msg), result) != 0)
{
strncpy(sys_msg, "unknown error", sizeof(sys_msg));
}
debug(LOG_ERROR, "localtime_s failed with error: %s", sys_msg);
}
debug(LOG_ERROR, "localtime_s failed with error: %s", sys_msg);
return nullopt;
}
#else
if (!localtime_r(&timer, &timeinfo))
{
debug(LOG_ERROR, "localtime_r failed");
if (!quiet)
{
debug(LOG_ERROR, "localtime_r failed");
}
return nullopt;
}
#endif
return timeinfo;
}

tm getLocalTime(std::time_t const &timer)
tm getLocalTime(std::time_t const &timer, bool quiet)
{
auto result = getLocalTimeOpt(timer);
auto result = getLocalTimeOpt(timer, quiet);
if (!result.has_value())
{
struct tm zeroResult = {};
Expand Down
4 changes: 2 additions & 2 deletions lib/framework/wztime.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ using nonstd::optional;
using nonstd::nullopt;

tm getUtcTime(std::time_t const &timer);
optional<tm> getLocalTimeOpt(std::time_t const &timer);
tm getLocalTime(std::time_t const &timer);
optional<tm> getLocalTimeOpt(std::time_t const &timer, bool quiet = false);
tm getLocalTime(std::time_t const &timer, bool quiet = false);

/**
* Examples of formats:
Expand Down

0 comments on commit 29e7469

Please sign in to comment.