diff --git a/common/log.cc b/common/log.cc index 36a90f7a..95a2e715 100644 --- a/common/log.cc +++ b/common/log.cc @@ -46,6 +46,7 @@ static std::ofstream logfile; namespace logging { bitflags mask = bitflags(flag::ALL) & ~bitflags(flag::VERBOSE); +bool enable_color_codes = true; void init(const fs::path &filename, const settings::common_settings &settings) { @@ -71,14 +72,17 @@ void print(flag logflag, const char *str) } fmt::text_style style; - if (string_icontains(str, "error")) { - style = fmt::fg(fmt::color::red); - } else if (string_icontains(str, "warning")) { - style = fmt::fg(fmt::terminal_color::yellow); - } else if (bitflags(logflag) & flag::PERCENT) { - style = fmt::fg(fmt::terminal_color::blue); - } else if (bitflags(logflag) & flag::STAT) { - style = fmt::fg(fmt::terminal_color::cyan); + + if (enable_color_codes) { + if (string_icontains(str, "error")) { + style = fmt::fg(fmt::color::red); + } else if (string_icontains(str, "warning")) { + style = fmt::fg(fmt::terminal_color::yellow); + } else if (bitflags(logflag) & flag::PERCENT) { + style = fmt::fg(fmt::terminal_color::blue); + } else if (bitflags(logflag) & flag::STAT) { + style = fmt::fg(fmt::terminal_color::cyan); + } } print_mutex.lock(); @@ -97,8 +101,15 @@ void print(flag logflag, const char *str) #endif } - // stdout (assume the terminal can render ANSI colors) - fmt::print(style, "{}", str); + if (enable_color_codes) { + // stdout (assume the terminal can render ANSI colors) + fmt::print(style, "{}", str); + } else { + std::cout << str; + } + + // for TB, etc... + fflush(stdout); print_mutex.unlock(); } diff --git a/common/settings.cc b/common/settings.cc index 544cab52..7e80c38f 100644 --- a/common/settings.cc +++ b/common/settings.cc @@ -185,5 +185,9 @@ void common_settings::postinitialize(int argc, const char **argv) if (noprogress.value()) { logging::mask &= ~bitflags(logging::flag::PROGRESS); } + + if (nocolor.value()) { + logging::enable_color_codes = false; + } } } // namespace settings diff --git a/include/common/log.hh b/include/common/log.hh index 5b412d18..ae8c7ceb 100644 --- a/include/common/log.hh +++ b/include/common/log.hh @@ -55,6 +55,7 @@ enum class flag : uint8_t }; extern bitflags mask; +extern bool enable_color_codes; // initialize logging subsystem void init(const fs::path &filename, const settings::common_settings &settings); diff --git a/include/common/settings.hh b/include/common/settings.hh index 0f20a1aa..ea24c477 100644 --- a/include/common/settings.hh +++ b/include/common/settings.hh @@ -851,6 +851,7 @@ public: setting_bool nopercent{this, "nopercent", false, &logging_group, "don't output percentage messages"}; setting_bool nostat{this, "nostat", false, &logging_group, "don't output statistic messages"}; setting_bool noprogress{this, "noprogress", false, &logging_group, "don't output progress messages"}; + setting_bool nocolor{this, "nocolor", false, &logging_group, "don't output color codes (for TB, etc)"}; setting_redirect quiet{this, {"quiet", "noverbose"}, {&nopercent, &nostat, &noprogress}, &logging_group, "suppress non-important messages (equivalent to -nopercent -nostat -noprogress)"}; setting_path gamedir{this, "gamedir", "", &game_group, diff --git a/light/light.cc b/light/light.cc index dbde660d..1e7238df 100644 --- a/light/light.cc +++ b/light/light.cc @@ -114,14 +114,19 @@ setting_group experimental_group{"Experimental options", 60}; void light_settings::initialize(int argc, const char **argv) { - token_parser_t p(argc - 1, argv + 1, { "command line" }); - auto remainder = parse(p); + try { + token_parser_t p(argc - 1, argv + 1, { "command line" }); + auto remainder = parse(p); - if (remainder.size() <= 0 || remainder.size() > 1) { + if (remainder.size() <= 0 || remainder.size() > 1) { + printHelp(); + } + + sourceMap = remainder[0]; + } catch (parse_exception &ex) { + logging::print(ex.what()); printHelp(); } - - sourceMap = remainder[0]; } void light_settings::postinitialize(int argc, const char **argv) diff --git a/qbsp/qbsp.cc b/qbsp/qbsp.cc index 383c6481..fd5f8f53 100644 --- a/qbsp/qbsp.cc +++ b/qbsp/qbsp.cc @@ -105,7 +105,7 @@ void qbsp_settings::initialize(int argc, const char **argv) if (remainder.size() == 2) { qbsp_options.bsp_path = remainder[1]; } - } catch (parse_exception ex) { + } catch (parse_exception &ex) { logging::print(ex.what()); printHelp(); } diff --git a/vis/vis.cc b/vis/vis.cc index c9960727..b819729c 100644 --- a/vis/vis.cc +++ b/vis/vis.cc @@ -63,14 +63,19 @@ setting_group advanced_group{"Advanced", 300}; void vis_settings::initialize(int argc, const char **argv) { - token_parser_t p(argc - 1, argv + 1, { "command line" }); - auto remainder = parse(p); + try { + token_parser_t p(argc - 1, argv + 1, { "command line" }); + auto remainder = parse(p); - if (remainder.size() <= 0 || remainder.size() > 1) { + if (remainder.size() <= 0 || remainder.size() > 1) { + printHelp(); + } + + sourceMap = DefaultExtension(remainder[0], "bsp"); + } catch (parse_exception &ex) { + logging::print(ex.what()); printHelp(); } - - sourceMap = DefaultExtension(remainder[0], "bsp"); } } // namespace settings