]> git.lizzy.rs Git - minetest.git/blobdiff - src/log.cpp
Translated using Weblate (Russian)
[minetest.git] / src / log.cpp
index 5cba8f70056a53ccd90199e9df656ef7c1f407dc..30344b4dfda86d60339a6e199b3975ed7ad5fec4 100644 (file)
@@ -34,9 +34,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <cerrno>
 #include <cstring>
 
+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 +48,8 @@ class StringBuffer : public std::streambuf {
        void push_back(char c);
 
 private:
-       std::string buffer;
+       char buffer[BUFFER_LENGTH];
+       int buffer_index;
 };
 
 
@@ -181,6 +186,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 +201,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<ILogOutput *>::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 +223,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 +251,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<threadid_t, std::string>::const_iterator it;
+       std::map<std::thread::id, std::string>::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,16 +310,32 @@ 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;
 }
 
 
@@ -326,11 +361,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;
+               }
        }
 }