Fix --color command line parameter ignorance (#7173)

* Fix color command line parameter ignorance

* coloured log: Support detecting the tty on windows

* Print an error message when setting something invalid as color mode instead of silently using mode never

* Revert "coloured log: Support detecting the tty on windows"

This reverts commit 4c9fc6366487ac0e6799e181796ca594797bb6f8.
It didn't work for travis and belongs to a separate PR

* Allow adjusting the log color with an environment variable

If --color is not passed to minetest,  is used to decide on the log colorization.
Minetest settings can not be used instead of an environment variable because logs may appear before loading them.

* fix empty if body
This commit is contained in:
HybridDog 2019-03-05 08:14:33 +01:00 committed by Loïc Blot
parent e19565c170
commit ee698770b9
2 changed files with 20 additions and 9 deletions

@ -118,16 +118,16 @@ public:
m_stream(stream) m_stream(stream)
{ {
#if !defined(_WIN32) #if !defined(_WIN32)
colored = (Logger::color_mode == LOG_COLOR_ALWAYS) || is_tty = isatty(fileno(stdout));
(Logger::color_mode == LOG_COLOR_AUTO && isatty(fileno(stdout)));
#else #else
colored = Logger::color_mode == LOG_COLOR_ALWAYS; is_tty = false;
#endif #endif
} }
void logRaw(LogLevel lev, const std::string &line) void logRaw(LogLevel lev, const std::string &line)
{ {
bool colored_message = colored; bool colored_message = (Logger::color_mode == LOG_COLOR_ALWAYS) ||
(Logger::color_mode == LOG_COLOR_AUTO && is_tty);
if (colored_message) if (colored_message)
switch (lev) { switch (lev) {
case LL_ERROR: case LL_ERROR:
@ -160,7 +160,7 @@ public:
private: private:
std::ostream &m_stream; std::ostream &m_stream;
bool colored; bool is_tty;
}; };
class FileLogOutput : public ICombinedLogOutput { class FileLogOutput : public ICombinedLogOutput {

@ -407,14 +407,25 @@ static void setup_log_params(const Settings &cmd_args)
} }
// Coloured log messages (see log.h) // Coloured log messages (see log.h)
std::string color_mode;
if (cmd_args.exists("color")) { if (cmd_args.exists("color")) {
std::string mode = cmd_args.get("color"); color_mode = cmd_args.get("color");
if (mode == "auto") #if !defined(_WIN32)
} else {
char *color_mode_env = getenv("MT_LOGCOLOR");
if (color_mode_env)
color_mode = color_mode_env;
#endif
}
if (color_mode != "") {
if (color_mode == "auto")
Logger::color_mode = LOG_COLOR_AUTO; Logger::color_mode = LOG_COLOR_AUTO;
else if (mode == "always") else if (color_mode == "always")
Logger::color_mode = LOG_COLOR_ALWAYS; Logger::color_mode = LOG_COLOR_ALWAYS;
else else if (color_mode == "never")
Logger::color_mode = LOG_COLOR_NEVER; Logger::color_mode = LOG_COLOR_NEVER;
else
errorstream << "Invalid color mode: " << color_mode << std::endl;
} }
// If trace is enabled, enable logging of certain things // If trace is enabled, enable logging of certain things