]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/log.cpp
Merge pull request #14 from corarona/master
[dragonfireclient.git] / src / log.cpp
index 3e9229e38799485c58300082c72190ef45755b8b..54442c39b1669561a5fa9b6a686395d9416d4c0a 100644 (file)
@@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "debug.h"
 #include "gettime.h"
 #include "porting.h"
+#include "settings.h"
 #include "config.h"
 #include "exceptions.h"
 #include "util/numeric.h"
@@ -251,6 +252,8 @@ const std::string Logger::getLevelLabel(LogLevel lev)
        return names[lev];
 }
 
+LogColor Logger::color_mode = LOG_COLOR_AUTO;
+
 const std::string Logger::getThreadName()
 {
        std::map<std::thread::id, std::string>::const_iterator it;
@@ -308,19 +311,108 @@ void Logger::logToOutputs(LogLevel lev, const std::string &combined,
 //// *LogOutput methods
 ////
 
-void FileLogOutput::open(const std::string &filename)
+void FileLogOutput::setFile(const std::string &filename, s64 file_size_max)
 {
-       m_stream.open(filename.c_str(), std::ios::app | std::ios::ate);
+       // Only move debug.txt if there is a valid maximum file size
+       bool is_too_large = false;
+       if (file_size_max > 0) {
+               std::ifstream ifile(filename, std::ios::binary | std::ios::ate);
+               is_too_large = ifile.tellg() > file_size_max;
+               ifile.close();
+       }
+
+       if (is_too_large) {
+               std::string filename_secondary = filename + ".1";
+               actionstream << "The log file grew too big; it is moved to " <<
+                       filename_secondary << std::endl;
+               remove(filename_secondary.c_str());
+               rename(filename.c_str(), filename_secondary.c_str());
+       }
+       m_stream.open(filename, std::ios::app | std::ios::ate);
+
        if (!m_stream.good())
                throw FileNotGoodException("Failed to open log file " +
                        filename + ": " + strerror(errno));
        m_stream << "\n\n"
-                  "-------------" << std::endl
-               << "  Separator" << std::endl
-               << "-------------\n" << std::endl;
+               "-------------" << std::endl <<
+               "  Separator" << std::endl <<
+               "-------------\n" << std::endl;
+}
+
+void StreamLogOutput::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";
+       }
 }
 
+void LogOutputBuffer::updateLogLevel()
+{
+       const std::string &conf_loglev = g_settings->get("chat_log_level");
+       LogLevel log_level = Logger::stringToLevel(conf_loglev);
+       if (log_level == LL_MAX) {
+               warningstream << "Supplied unrecognized chat_log_level; "
+                       "showing none." << std::endl;
+               log_level = LL_NONE;
+       }
+
+       m_logger.removeOutput(this);
+       m_logger.addOutputMaxLevel(this, log_level);
+}
 
+void LogOutputBuffer::logRaw(LogLevel lev, const std::string &line)
+{
+       std::string color;
+
+       if (!g_settings->getBool("disable_escape_sequences")) {
+               switch (lev) {
+               case LL_ERROR: // red
+                       color = "\x1b(c@#F00)";
+                       break;
+               case LL_WARNING: // yellow
+                       color = "\x1b(c@#EE0)";
+                       break;
+               case LL_INFO: // grey
+                       color = "\x1b(c@#BBB)";
+                       break;
+               case LL_VERBOSE: // dark grey
+                       color = "\x1b(c@#888)";
+                       break;
+               default: break;
+               }
+       }
+
+       m_buffer.push(color.append(line));
+}
 
 ////
 //// *Buffer methods