diff options
Diffstat (limited to 'src/log.cpp')
-rw-r--r-- | src/log.cpp | 110 |
1 files changed, 60 insertions, 50 deletions
diff --git a/src/log.cpp b/src/log.cpp index 0fd4dc01c..54442c39b 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -37,10 +37,11 @@ with this program; if not, write to the Free Software Foundation, Inc., const int BUFFER_LENGTH = 256; -class StringBuffer : public std::streambuf -{ +class StringBuffer : public std::streambuf { public: - StringBuffer() { buffer_index = 0; } + StringBuffer() { + buffer_index = 0; + } int overflow(int c); virtual void flush(const std::string &buf) = 0; @@ -52,10 +53,13 @@ private: int buffer_index; }; -class LogBuffer : public StringBuffer -{ + +class LogBuffer : public StringBuffer { public: - LogBuffer(Logger &logger, LogLevel lev) : logger(logger), level(lev) {} + LogBuffer(Logger &logger, LogLevel lev) : + logger(logger), + level(lev) + {} void flush(const std::string &buffer); @@ -64,8 +68,8 @@ private: LogLevel level; }; -class RawLogBuffer : public StringBuffer -{ + +class RawLogBuffer : public StringBuffer { public: void flush(const std::string &buffer); }; @@ -115,28 +119,33 @@ std::ostream verbosestream(&verbose_buf); #ifdef __ANDROID__ static unsigned int g_level_to_android[] = { - ANDROID_LOG_INFO, // LL_NONE - // ANDROID_LOG_FATAL, - ANDROID_LOG_ERROR, // LL_ERROR - ANDROID_LOG_WARN, // LL_WARNING - ANDROID_LOG_WARN, // LL_ACTION - // ANDROID_LOG_INFO, - ANDROID_LOG_DEBUG, // LL_INFO - ANDROID_LOG_VERBOSE, // LL_VERBOSE + ANDROID_LOG_INFO, // LL_NONE + //ANDROID_LOG_FATAL, + ANDROID_LOG_ERROR, // LL_ERROR + ANDROID_LOG_WARN, // LL_WARNING + ANDROID_LOG_WARN, // LL_ACTION + //ANDROID_LOG_INFO, + ANDROID_LOG_DEBUG, // LL_INFO + ANDROID_LOG_VERBOSE, // LL_VERBOSE }; -class AndroidSystemLogOutput : public ICombinedLogOutput -{ -public: - AndroidSystemLogOutput() { g_logger.addOutput(this); } - ~AndroidSystemLogOutput() { g_logger.removeOutput(this); } - void logRaw(LogLevel lev, const std::string &line) - { - STATIC_ASSERT(ARRLEN(g_level_to_android) == LL_MAX, +class AndroidSystemLogOutput : public ICombinedLogOutput { + public: + AndroidSystemLogOutput() + { + g_logger.addOutput(this); + } + ~AndroidSystemLogOutput() + { + g_logger.removeOutput(this); + } + void logRaw(LogLevel lev, const std::string &line) + { + STATIC_ASSERT(ARRLEN(g_level_to_android) == LL_MAX, mismatch_between_android_and_internal_loglevels); - __android_log_print(g_level_to_android[lev], PROJECT_NAME_C, "%s", - line.c_str()); - } + __android_log_print(g_level_to_android[lev], + PROJECT_NAME_C, "%s", line.c_str()); + } }; AndroidSystemLogOutput g_android_log_output; @@ -145,6 +154,7 @@ AndroidSystemLogOutput g_android_log_output; /////////////////////////////////////////////////////////////////////////////// + //// //// Logger //// @@ -229,15 +239,16 @@ void Logger::deregisterThread() const std::string Logger::getLevelLabel(LogLevel lev) { static const std::string names[] = { - "", - "ERROR", - "WARNING", - "ACTION", - "INFO", - "VERBOSE", + "", + "ERROR", + "WARNING", + "ACTION", + "INFO", + "VERBOSE", }; assert(lev < LL_MAX && lev >= 0); - STATIC_ASSERT(ARRLEN(names) == LL_MAX, mismatch_between_loglevel_names_and_enum); + STATIC_ASSERT(ARRLEN(names) == LL_MAX, + mismatch_between_loglevel_names_and_enum); return names[lev]; } @@ -287,14 +298,15 @@ void Logger::logToOutputsRaw(LogLevel lev, const std::string &line) } void Logger::logToOutputs(LogLevel lev, const std::string &combined, - const std::string &time, const std::string &thread_name, - const std::string &payload_text) + const std::string &time, const std::string &thread_name, + const std::string &payload_text) { MutexAutoLock lock(m_mutex); for (size_t i = 0; i != m_outputs[lev].size(); i++) m_outputs[lev][i]->log(lev, combined, time, thread_name, payload_text); } + //// //// *LogOutput methods //// @@ -311,28 +323,26 @@ void FileLogOutput::setFile(const std::string &filename, s64 file_size_max) 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; + 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)); + 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); + (Logger::color_mode == LOG_COLOR_AUTO && is_tty); if (colored_message) { switch (lev) { case LL_ERROR: @@ -371,8 +381,7 @@ void LogOutputBuffer::updateLogLevel() LogLevel log_level = Logger::stringToLevel(conf_loglev); if (log_level == LL_MAX) { warningstream << "Supplied unrecognized chat_log_level; " - "showing none." - << std::endl; + "showing none." << std::endl; log_level = LL_NONE; } @@ -398,8 +407,7 @@ void LogOutputBuffer::logRaw(LogLevel lev, const std::string &line) case LL_VERBOSE: // dark grey color = "\x1b(c@#888)"; break; - default: - break; + default: break; } } @@ -416,6 +424,7 @@ int StringBuffer::overflow(int c) return c; } + std::streamsize StringBuffer::xsputn(const char *s, std::streamsize n) { for (int i = 0; i < n; ++i) @@ -438,6 +447,7 @@ void StringBuffer::push_back(char c) } } + void LogBuffer::flush(const std::string &buffer) { logger.log(level, buffer); |