1
0
mirror of https://github.com/luanti-org/luanti.git synced 2025-10-29 06:35:20 +01:00

Make logging timestamps configurable (#16581)

This commit is contained in:
sfan5
2025-10-17 00:18:06 +02:00
committed by GitHub
parent 6b0e1e9b67
commit a049174f12
4 changed files with 89 additions and 38 deletions

View File

@@ -220,7 +220,7 @@ const char *Logger::getLevelLabel(LogLevel lev)
LogColor Logger::color_mode = LOG_COLOR_AUTO;
const std::string &Logger::getThreadName()
inline const std::string &Logger::getThreadName()
{
std::thread::id id = std::this_thread::get_id();
@@ -237,6 +237,22 @@ const std::string &Logger::getThreadName()
return fallback_name;
}
LogTimestamp Logger::timestamp_mode = LOG_TIMESTAMP_WALL;
inline std::string Logger::getLogTimestamp()
{
const static u64 begin_of_time = porting::getTimeMs();
if (timestamp_mode == LOG_TIMESTAMP_NONE)
return std::string();
if (timestamp_mode == LOG_TIMESTAMP_WALL)
return getTimestamp();
// LOG_TIMESTAMP_RELATIVE
float rel = (porting::getTimeMs() - begin_of_time) / 1000.0f;
char s[24];
snprintf(s, sizeof(s), "[% 8.2f]", rel);
return s;
}
void Logger::log(LogLevel lev, std::string_view text)
{
if (isLevelSilenced(lev))
@@ -244,10 +260,12 @@ void Logger::log(LogLevel lev, std::string_view text)
const std::string &thread_name = getThreadName();
const char *label = getLevelLabel(lev);
const std::string timestamp = getTimestamp();
const std::string timestamp = getLogTimestamp();
std::string line = timestamp;
line.append(": ").append(label).append("[").append(thread_name)
if (!line.empty())
line.append(": ");
line.append(label).append("[").append(thread_name)
.append("]: ").append(text);
logToOutputs(lev, line, timestamp, thread_name, text);

View File

@@ -28,12 +28,18 @@ enum LogLevel {
LL_MAX,
};
enum LogColor {
enum LogColor : u8 {
LOG_COLOR_NEVER,
LOG_COLOR_ALWAYS,
LOG_COLOR_AUTO,
};
enum LogTimestamp : u8 {
LOG_TIMESTAMP_NONE,
LOG_TIMESTAMP_WALL,
LOG_TIMESTAMP_RELATIVE
};
typedef u8 LogLevelMask;
#define LOGLEVEL_TO_MASKLEVEL(x) (1 << x)
@@ -65,6 +71,7 @@ public:
}
static LogColor color_mode;
static LogTimestamp timestamp_mode;
private:
void logToOutputsRaw(LogLevel, std::string_view line);
@@ -73,12 +80,13 @@ private:
std::string_view payload_text);
const std::string &getThreadName();
static std::string getLogTimestamp();
mutable std::mutex m_mutex;
std::vector<ILogOutput *> m_outputs[LL_MAX];
std::atomic<bool> m_has_outputs[LL_MAX];
std::atomic<bool> m_silenced_levels[LL_MAX];
std::map<std::thread::id, std::string> m_thread_names;
mutable std::mutex m_mutex;
};
class ILogOutput {

View File

@@ -71,6 +71,7 @@ extern "C" {
#define ENV_NO_COLOR "NO_COLOR"
#define ENV_CLICOLOR "CLICOLOR"
#define ENV_CLICOLOR_FORCE "CLICOLOR_FORCE"
#define ENV_LOG_TIMESTAMP "LOG_TIMESTAMP"
typedef std::map<std::string, ValueSpec> OptionList;
@@ -293,7 +294,7 @@ static void get_env_opts(Settings &args)
// CLICOLOR != 0: ANSI colors are supported (auto-detection, this is the default)
// CLICOLOR == 0: ANSI colors are NOT supported
const char *clicolor = std::getenv(ENV_CLICOLOR);
if (clicolor && std::string(clicolor) == "0") {
if (clicolor && std::string_view(clicolor) == "0") {
args.set("color", "never");
}
// NO_COLOR only specifies that no color is allowed.
@@ -304,10 +305,15 @@ static void get_env_opts(Settings &args)
}
// CLICOLOR_FORCE is another option, which should turn on colors "no matter what".
const char *clicolor_force = std::getenv(ENV_CLICOLOR_FORCE);
if (clicolor_force && std::string(clicolor_force) != "0") {
if (clicolor_force && std::string_view(clicolor_force) != "0") {
// should ALWAYS have colors, so we ignore tty (no "auto")
args.set("color", "always");
}
// No standard, Luanti-specific
const char *log_ts = std::getenv(ENV_LOG_TIMESTAMP);
if (log_ts)
args.set("log-timestamp", log_ts);
}
static bool get_cmdline_opts(int argc, char *argv[], Settings *cmd_args)
@@ -355,8 +361,10 @@ static void set_allowed_options(OptionList *allowed_options)
"'name' lists names, 'both' lists both)"))));
allowed_options->insert(std::make_pair("quiet", ValueSpec(VALUETYPE_FLAG,
_("Print only errors to console"))));
allowed_options->insert(std::make_pair("color", ValueSpec(VALUETYPE_STRING,
_("Coloured logs ('always', 'never' or 'auto'), defaults to 'auto'"))));
allowed_options->emplace("color", ValueSpec(VALUETYPE_STRING,
_("Coloured logs ('always', 'never' or 'auto'), default: 'auto'")));
allowed_options->emplace("log-timestamp", ValueSpec(VALUETYPE_STRING,
_("Timestamped logs ('wall', 'relative' or 'none'), default: 'wall'")));
allowed_options->insert(std::make_pair("info", ValueSpec(VALUETYPE_FLAG,
_("Print more information to console"))));
allowed_options->insert(std::make_pair("verbose", ValueSpec(VALUETYPE_FLAG,
@@ -524,11 +532,9 @@ static bool setup_log_params(const Settings &cmd_args)
g_logger.addOutputMaxLevel(&stderr_output, LL_ERROR);
}
// Coloured log messages (see log.h)
// Message color
std::string color_mode;
if (cmd_args.exists("color")) {
color_mode = cmd_args.get("color");
}
cmd_args.getNoEx("color", color_mode);
if (!color_mode.empty()) {
if (color_mode == "auto") {
Logger::color_mode = LOG_COLOR_AUTO;
@@ -542,6 +548,22 @@ static bool setup_log_params(const Settings &cmd_args)
}
}
// Timestamp
std::string ts_mode;
cmd_args.getNoEx("log-timestamp", ts_mode);
if (!ts_mode.empty()) {
if (ts_mode == "wall") {
Logger::timestamp_mode = LOG_TIMESTAMP_WALL;
} else if (ts_mode == "relative") {
Logger::timestamp_mode = LOG_TIMESTAMP_RELATIVE;
} else if (ts_mode == "none") {
Logger::timestamp_mode = LOG_TIMESTAMP_NONE;
} else {
errorstream << "Invalid timestamp mode: " << ts_mode << std::endl;
return false;
}
}
// In certain cases, output info level on stderr
if (cmd_args.getFlag("info") || cmd_args.getFlag("verbose") ||
cmd_args.getFlag("trace") || cmd_args.getFlag("speedtests"))