feat(logs): add -DBEAST_ENHANCED_LOGGING with file:line numbers for JLOG macro (#552)

This commit is contained in:
Niq Dudfield
2025-10-14 07:44:03 +07:00
committed by GitHub
parent ad0531ad6c
commit 1f12b9ec5a
12 changed files with 448 additions and 42 deletions

View File

@@ -17,11 +17,19 @@
*/
//==============================================================================
#include <date/date.h>
#include <ripple/basics/Log.h>
#include <ripple/basics/chrono.h>
#include <ripple/basics/contract.h>
#ifdef BEAST_ENHANCED_LOGGING
#include <ripple/beast/utility/EnhancedLogging.h>
#include <date/tz.h>
#endif
#include <boost/algorithm/string.hpp>
#include <cassert>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
@@ -316,11 +324,46 @@ Logs::format(
{
output.reserve(message.size() + partition.size() + 100);
output = to_string(std::chrono::system_clock::now());
#ifdef BEAST_ENHANCED_LOGGING
// Environment variables are used instead of config file because:
// 1. Logging starts before config parsing (needed to debug config issues)
// 2. This is a developer feature - devs can easily set env vars
// 3. Allows per-run overrides without editing config files
static const char* fmt = []() {
const char* env = std::getenv("LOG_DATE_FORMAT");
return env ? env : "%Y-%b-%d %T %Z"; // Default format
}();
// Check if we should use local time
static const bool useLocalTime = []() {
const char* env = std::getenv("LOG_DATE_LOCAL");
return env && std::strcmp(env, "1") == 0;
}();
if (useLocalTime)
{
auto now = std::chrono::system_clock::now();
auto local = date::make_zoned(date::current_zone(), now);
output = date::format(fmt, local);
}
else
{
output = date::format(fmt, std::chrono::system_clock::now());
}
#else
output = to_string(std::chrono::system_clock::now());
#endif
if (!output.empty()) // Allow setting date format to an empty string
output += " ";
output += " ";
if (!partition.empty())
{
#ifdef BEAST_ENHANCED_LOGGING
output += beast::detail::get_log_highlight_color();
#endif
output += partition + ":";
}
using namespace beast::severities;
switch (severity)
@@ -348,6 +391,10 @@ Logs::format(
break;
}
#ifdef BEAST_ENHANCED_LOGGING
output += "\033[0m";
#endif
output += message;
// Limit the maximum length of the output