@@ -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 ;
4951static std::vector<Scope> sScopes ;
5052
5153static 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+
8191void 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
132142static 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
169190void logf (char const * module , Level level, char const * format, ...) {
0 commit comments