Skip to content

Commit dcf321f

Browse files
authored
--log-no-color and --log-flush (#36)
1 parent b79a552 commit dcf321f

File tree

6 files changed

+49
-3
lines changed

6 files changed

+49
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Changelog
22

3+
## []
4+
5+
- Added `--log-no-color` to disable colored logging output
6+
- Added `--log-flush` to flush logging after every line
7+
38
## [4.0.3] 2025-03-12
49

510
- Building and publishing containers to GHCR

dependency/loglet/include/loglet/loglet.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ enum class Level {
187187
void uninitialize();
188188

189189
void set_level(Level level);
190+
void set_color_enable(bool enabled);
191+
void set_always_flush(bool flush);
190192
void set_module_level(char const* module, Level level);
191193
void disable_module(char const* module);
192194
bool is_module_enabled(char const* module);

dependency/loglet/loglet.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ struct Module {
4545
Level level;
4646
};
4747

48-
static Level sLevel = Level::Debug;
48+
static Level sLevel = Level::Debug;
49+
static bool sColorEnabled = true;
50+
static bool sAlwaysFlush = false;
4951
static std::vector<Scope> sScopes;
5052

5153
static std::unordered_map<char const*, Module, HashModuleName, EqualModuleName> sModules;
@@ -78,6 +80,14 @@ void set_level(Level level) {
7880
sLevel = level;
7981
}
8082

83+
void set_color_enable(bool enabled) {
84+
sColorEnabled = enabled;
85+
}
86+
87+
void set_always_flush(bool flush) {
88+
sAlwaysFlush = flush;
89+
}
90+
8191
void set_module_level(char const* module, Level level) {
8292
auto& module_ref = get_or_add_module(module);
8393
module_ref.level = level;
@@ -130,6 +140,7 @@ static char const* level_to_string(Level level) {
130140
}
131141

132142
static char const* level_to_color(Level level) {
143+
if (!sColorEnabled) return "";
133144
switch (level) {
134145
case Level::Trace: return COLOR_CYAN;
135146
case Level::Verbose: return COLOR_BLUE;
@@ -162,8 +173,18 @@ void log(char const* module, Level level, char const* message) {
162173
fflush(stdout);
163174
}
164175

165-
fprintf(file, "%s%s%s[%10s] %*s%s%s\n", start_color, level_to_string(level), buffer, module,
166-
static_cast<int>(sScopes.size() * 2), "", message, stop_color);
176+
if (sAlwaysFlush) {
177+
fflush(file);
178+
}
179+
180+
char const indent_buffer[64 + 1] =
181+
" ";
182+
auto indent_length = static_cast<int>(sScopes.size() * 2);
183+
if (indent_length > 64) {
184+
indent_length = 64;
185+
}
186+
fprintf(file, "%s%s%s[%10s] %.*s%s%s\n", start_color, level_to_string(level), buffer, module,
187+
static_cast<int>(sScopes.size() * 2), indent_buffer, message, stop_color);
167188
}
168189

169190
void logf(char const* module, Level level, char const* format, ...) {

examples/client/config.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@ struct DataTracingConfig {
158158

159159
struct LoggingConfig {
160160
loglet::Level log_level;
161+
bool color;
162+
bool flush;
161163
std::unordered_map<std::string, loglet::Level> module_levels;
162164
};
163165

examples/client/config/logging.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,26 @@ static args::ValueFlagList<std::string> gModules{
5050
"<module>=<level>",
5151
{"lm"},
5252
};
53+
static args::Flag gNoColor{
54+
gGroup,
55+
"no-color",
56+
"Disable colored output",
57+
{"log-no-color"},
58+
};
59+
static args::Flag gFlush{
60+
gGroup,
61+
"flush",
62+
"Flush log after each line",
63+
{"log-flush"},
64+
};
5365

5466
static void setup() {}
5567

5668
static void parse(Config* config) {
5769
auto& logging = config->logging;
5870
logging.log_level = loglet::Level::Info;
71+
logging.color = gNoColor ? false : true;
72+
logging.flush = gFlush;
5973

6074
if (gTrace) {
6175
logging.log_level = loglet::Level::Trace;

examples/client/main.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,8 @@ int main(int argc, char** argv) {
458458
}
459459

460460
loglet::set_level(config.logging.log_level);
461+
loglet::set_color_enable(config.logging.color);
462+
loglet::set_always_flush(config.logging.flush);
461463
for (auto const& [module, level] : config.logging.module_levels) {
462464
loglet::set_module_level(module.c_str(), level);
463465
}

0 commit comments

Comments
 (0)