aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryou <ovvv@web.de>2017-12-06 18:50:39 +0100
committerSmallJoker <SmallJoker@users.noreply.github.com>2017-12-06 18:50:39 +0100
commite049405fdc688a4d619f3ae88008bbe5ee85b569 (patch)
tree12acb7536beb3d40c69f420d1ec649d33bba6790
parent4edf08709010f721b099dfae3cab011acbdfb3ff (diff)
downloadminetest-e049405fdc688a4d619f3ae88008bbe5ee85b569.tar.gz
minetest-e049405fdc688a4d619f3ae88008bbe5ee85b569.tar.bz2
minetest-e049405fdc688a4d619f3ae88008bbe5ee85b569.zip
Add coloured logs (#4549)
The setting log_colour can be used to en-/disable or autodetect it.
-rw-r--r--builtin/settingtypes.txt7
-rw-r--r--src/defaultsettings.cpp1
-rw-r--r--src/log.h40
3 files changed, 48 insertions, 0 deletions
diff --git a/builtin/settingtypes.txt b/builtin/settingtypes.txt
index 62d695fed..24b7008a1 100644
--- a/builtin/settingtypes.txt
+++ b/builtin/settingtypes.txt
@@ -1207,6 +1207,13 @@ language (Language) enum ,be,ca,cs,da,de,en,eo,es,et,fr,he,hu,id,it,ja,jbo,ko,
# - verbose
debug_log_level (Debug log level) enum action ,none,error,warning,action,info,verbose
+# ANSI colored logs: red error log, yellow warning and grey info and verbose logs
+# Note that it doesn't work on Windows
+# "yes" always enables it,
+# "detect" enables it when printing to terminal and
+# "no" disables it
+log_color (Colored logs) enum detect yes,detect,no
+
# IPv6 support.
enable_ipv6 (IPv6) bool true
diff --git a/src/defaultsettings.cpp b/src/defaultsettings.cpp
index ff136b8e3..6691b483f 100644
--- a/src/defaultsettings.cpp
+++ b/src/defaultsettings.cpp
@@ -349,6 +349,7 @@ void set_default_settings(Settings *settings)
settings->setDefault("ignore_world_load_errors", "false");
settings->setDefault("remote_media", "");
settings->setDefault("debug_log_level", "action");
+ settings->setDefault("log_color", "detect");
settings->setDefault("emergequeue_limit_total", "256");
settings->setDefault("emergequeue_limit_diskonly", "32");
settings->setDefault("emergequeue_limit_generate", "32");
diff --git a/src/log.h b/src/log.h
index 6a1c24dec..506137739 100644
--- a/src/log.h
+++ b/src/log.h
@@ -25,6 +25,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <fstream>
#include <thread>
#include <mutex>
+#if !defined(_WIN32) // POSIX
+ #include <unistd.h>
+#endif
+#include "settings.h"
#include "irrlichttypes.h"
class ILogOutput;
@@ -106,15 +110,51 @@ public:
StreamLogOutput(std::ostream &stream) :
m_stream(stream)
{
+#if !defined(_WIN32)
+ is_tty = isatty(fileno(stdout));
+#else
+ is_tty = false;
+#endif
}
void logRaw(LogLevel lev, const std::string &line)
{
+ static const std::string use_logcolor = g_settings->get("log_color");
+
+ bool colored = use_logcolor == "detect" ? is_tty : use_logcolor == "yes";
+ if (colored)
+ switch (lev) {
+ case LL_ERROR:
+ // error is red
+ m_stream << "\033[91m";
+ break;
+ case LL_WARNING:
+ // warning is yellow
+ m_stream << "\033[93m";
+ break;
+ case LL_INFO:
+ // info is a bit dark
+ m_stream << "\033[37m";
+ break;
+ case LL_VERBOSE:
+ // verbose is darker than info
+ m_stream << "\033[2m";
+ break;
+ default:
+ // action is white
+ colored = false;
+ }
+
m_stream << line << std::endl;
+
+ if (colored)
+ // reset to white color
+ m_stream << "\033[0m";
}
private:
std::ostream &m_stream;
+ bool is_tty;
};
class FileLogOutput : public ICombinedLogOutput {