X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Flog.cpp;h=3c61414e901c57e1626104d3c0e0df55b79be347;hb=48f885e3102cb1c4ebb3f5fddf2d7e70d95c0522;hp=5cba8f70056a53ccd90199e9df656ef7c1f407dc;hpb=688556a5d1910ead2cf06a87af6908110fa9d035;p=dragonfireclient.git diff --git a/src/log.cpp b/src/log.cpp index 5cba8f700..3c61414e9 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -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" @@ -34,9 +35,13 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include +const int BUFFER_LENGTH = 256; + class StringBuffer : public std::streambuf { public: - StringBuffer() {} + StringBuffer() { + buffer_index = 0; + } int overflow(int c); virtual void flush(const std::string &buf) = 0; @@ -44,7 +49,8 @@ class StringBuffer : public std::streambuf { void push_back(char c); private: - std::string buffer; + char buffer[BUFFER_LENGTH]; + int buffer_index; }; @@ -91,16 +97,7 @@ LogBuffer verbose_buf(g_logger, LL_VERBOSE); std::ostream *dout_con_ptr = &null_stream; std::ostream *derr_con_ptr = &verbosestream; -// Server -std::ostream *dout_server_ptr = &infostream; -std::ostream *derr_server_ptr = &errorstream; - -#ifndef SERVER -// Client -std::ostream *dout_client_ptr = &infostream; -std::ostream *derr_client_ptr = &errorstream; -#endif - +// Common streams std::ostream rawstream(&raw_buf); std::ostream dstream(&none_buf); std::ostream errorstream(&error_buf); @@ -181,6 +178,14 @@ void Logger::addOutput(ILogOutput *out, LogLevel lev) m_outputs[lev].push_back(out); } +void Logger::addOutputMasked(ILogOutput *out, LogLevelMask mask) +{ + for (size_t i = 0; i < LL_MAX; i++) { + if (mask & LOGLEVEL_TO_MASKLEVEL(i)) + m_outputs[i].push_back(out); + } +} + void Logger::addOutputMaxLevel(ILogOutput *out, LogLevel lev) { assert(lev < LL_MAX); @@ -188,15 +193,19 @@ void Logger::addOutputMaxLevel(ILogOutput *out, LogLevel lev) m_outputs[i].push_back(out); } -void Logger::removeOutput(ILogOutput *out) +LogLevelMask Logger::removeOutput(ILogOutput *out) { + LogLevelMask ret_mask = 0; for (size_t i = 0; i < LL_MAX; i++) { std::vector::iterator it; it = std::find(m_outputs[i].begin(), m_outputs[i].end(), out); - if (it != m_outputs[i].end()) + if (it != m_outputs[i].end()) { + ret_mask |= LOGLEVEL_TO_MASKLEVEL(i); m_outputs[i].erase(it); + } } + return ret_mask; } void Logger::setLevelSilenced(LogLevel lev, bool silenced) @@ -206,14 +215,14 @@ void Logger::setLevelSilenced(LogLevel lev, bool silenced) void Logger::registerThread(const std::string &name) { - threadid_t id = thr_get_current_thread_id(); + std::thread::id id = std::this_thread::get_id(); MutexAutoLock lock(m_mutex); m_thread_names[id] = name; } void Logger::deregisterThread() { - threadid_t id = thr_get_current_thread_id(); + std::thread::id id = std::this_thread::get_id(); MutexAutoLock lock(m_mutex); m_thread_names.erase(id); } @@ -234,11 +243,13 @@ const std::string Logger::getLevelLabel(LogLevel lev) return names[lev]; } +LogColor Logger::color_mode = LOG_COLOR_AUTO; + const std::string Logger::getThreadName() { - std::map::const_iterator it; + std::map::const_iterator it; - threadid_t id = thr_get_current_thread_id(); + std::thread::id id = std::this_thread::get_id(); it = m_thread_names.find(id); if (it != m_thread_names.end()) return it->second; @@ -291,19 +302,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 @@ -326,11 +426,15 @@ std::streamsize StringBuffer::xsputn(const char *s, std::streamsize n) void StringBuffer::push_back(char c) { if (c == '\n' || c == '\r') { - if (!buffer.empty()) - flush(buffer); - buffer.clear(); + if (buffer_index) + flush(std::string(buffer, buffer_index)); + buffer_index = 0; } else { - buffer.push_back(c); + buffer[buffer_index++] = c; + if (buffer_index >= BUFFER_LENGTH) { + flush(std::string(buffer, buffer_index)); + buffer_index = 0; + } } }