]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/log.h
Update settingtypes.txt for new languages
[dragonfireclient.git] / src / log.h
index c017d127e6c21d9305c3c545b728117fa3e8d3ed..6350d8a8659b0ff0d555326d525b513e406c1105 100644 (file)
--- a/src/log.h
+++ b/src/log.h
@@ -17,15 +17,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef LOG_HEADER
-#define LOG_HEADER
+#pragma once
 
 #include <map>
 #include <queue>
 #include <string>
 #include <fstream>
+#include <thread>
 #include <mutex>
-#include "threads.h"
+#if !defined(_WIN32)  // POSIX
+       #include <unistd.h>
+#endif
 #include "irrlichttypes.h"
 
 class ILogOutput;
@@ -40,6 +42,12 @@ enum LogLevel {
        LL_MAX,
 };
 
+enum LogColor {
+       LOG_COLOR_NEVER,
+       LOG_COLOR_ALWAYS,
+       LOG_COLOR_AUTO,
+};
+
 typedef u8 LogLevelMask;
 #define LOGLEVEL_TO_MASKLEVEL(x) (1 << x)
 
@@ -65,6 +73,8 @@ class Logger {
        static LogLevel stringToLevel(const std::string &name);
        static const std::string getLevelLabel(LogLevel lev);
 
+       static LogColor color_mode;
+
 private:
        void logToOutputsRaw(LogLevel, const std::string &line);
        void logToOutputs(LogLevel, const std::string &combined,
@@ -79,7 +89,7 @@ class Logger {
        // written to when one thread has access currently).
        // Works on all known architectures (x86, ARM, MIPS).
        volatile bool m_silenced_levels[LL_MAX];
-       std::map<threadid_t, std::string> m_thread_names;
+       std::map<std::thread::id, std::string> m_thread_names;
        mutable std::mutex m_mutex;
        bool m_trace_enabled;
 };
@@ -107,20 +117,55 @@ class StreamLogOutput : public ICombinedLogOutput {
        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)
        {
+               bool colored_message = (Logger::color_mode == LOG_COLOR_ALWAYS) ||
+                       (Logger::color_mode == LOG_COLOR_AUTO && is_tty);
+               if (colored_message)
+                       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_message = false;
+                       }
+
                m_stream << line << std::endl;
+
+               if (colored_message)
+                       // reset to white color
+                       m_stream << "\033[0m";
        }
 
 private:
        std::ostream &m_stream;
+       bool is_tty;
 };
 
 class FileLogOutput : public ICombinedLogOutput {
 public:
-       void open(const std::string &filename);
+       void setFile(const std::string &filename, s64 file_size_max);
 
        void logRaw(LogLevel lev, const std::string &line)
        {
@@ -206,12 +251,7 @@ extern std::ostream dstream;
 #define dout_con (*dout_con_ptr)
 #define derr_con (*derr_con_ptr)
 #define dout_server (*dout_server_ptr)
-#define derr_server (*derr_server_ptr)
 
 #ifndef SERVER
        #define dout_client (*dout_client_ptr)
-       #define derr_client (*derr_client_ptr)
-#endif
-
-
 #endif