log.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include <map>
  18. #include <queue>
  19. #include <string>
  20. #include <fstream>
  21. #include <thread>
  22. #include <mutex>
  23. #if !defined(_WIN32) // POSIX
  24. #include <unistd.h>
  25. #endif
  26. #include "irrlichttypes.h"
  27. class ILogOutput;
  28. enum LogLevel {
  29. LL_NONE, // Special level that is always printed
  30. LL_ERROR,
  31. LL_WARNING,
  32. LL_ACTION, // In-game actions
  33. LL_INFO,
  34. LL_VERBOSE,
  35. LL_MAX,
  36. };
  37. enum LogColor {
  38. LOG_COLOR_NEVER,
  39. LOG_COLOR_ALWAYS,
  40. LOG_COLOR_AUTO,
  41. };
  42. typedef u8 LogLevelMask;
  43. #define LOGLEVEL_TO_MASKLEVEL(x) (1 << x)
  44. class Logger {
  45. public:
  46. void addOutput(ILogOutput *out);
  47. void addOutput(ILogOutput *out, LogLevel lev);
  48. void addOutputMasked(ILogOutput *out, LogLevelMask mask);
  49. void addOutputMaxLevel(ILogOutput *out, LogLevel lev);
  50. LogLevelMask removeOutput(ILogOutput *out);
  51. void setLevelSilenced(LogLevel lev, bool silenced);
  52. void registerThread(const std::string &name);
  53. void deregisterThread();
  54. void log(LogLevel lev, const std::string &text);
  55. // Logs without a prefix
  56. void logRaw(LogLevel lev, const std::string &text);
  57. void setTraceEnabled(bool enable) { m_trace_enabled = enable; }
  58. bool getTraceEnabled() { return m_trace_enabled; }
  59. static LogLevel stringToLevel(const std::string &name);
  60. static const std::string getLevelLabel(LogLevel lev);
  61. static LogColor color_mode;
  62. private:
  63. void logToOutputsRaw(LogLevel, const std::string &line);
  64. void logToOutputs(LogLevel, const std::string &combined,
  65. const std::string &time, const std::string &thread_name,
  66. const std::string &payload_text);
  67. const std::string getThreadName();
  68. std::vector<ILogOutput *> m_outputs[LL_MAX];
  69. // Should implement atomic loads and stores (even though it's only
  70. // written to when one thread has access currently).
  71. // Works on all known architectures (x86, ARM, MIPS).
  72. volatile bool m_silenced_levels[LL_MAX];
  73. std::map<std::thread::id, std::string> m_thread_names;
  74. mutable std::mutex m_mutex;
  75. bool m_trace_enabled;
  76. };
  77. class ILogOutput {
  78. public:
  79. virtual void logRaw(LogLevel, const std::string &line) = 0;
  80. virtual void log(LogLevel, const std::string &combined,
  81. const std::string &time, const std::string &thread_name,
  82. const std::string &payload_text) = 0;
  83. };
  84. class ICombinedLogOutput : public ILogOutput {
  85. public:
  86. void log(LogLevel lev, const std::string &combined,
  87. const std::string &time, const std::string &thread_name,
  88. const std::string &payload_text)
  89. {
  90. logRaw(lev, combined);
  91. }
  92. };
  93. class StreamLogOutput : public ICombinedLogOutput {
  94. public:
  95. StreamLogOutput(std::ostream &stream) :
  96. m_stream(stream)
  97. {
  98. #if !defined(_WIN32)
  99. is_tty = isatty(fileno(stdout));
  100. #else
  101. is_tty = false;
  102. #endif
  103. }
  104. void logRaw(LogLevel lev, const std::string &line);
  105. private:
  106. std::ostream &m_stream;
  107. bool is_tty;
  108. };
  109. class FileLogOutput : public ICombinedLogOutput {
  110. public:
  111. void setFile(const std::string &filename, s64 file_size_max);
  112. void logRaw(LogLevel lev, const std::string &line)
  113. {
  114. m_stream << line << std::endl;
  115. }
  116. private:
  117. std::ofstream m_stream;
  118. };
  119. class LogOutputBuffer : public ICombinedLogOutput {
  120. public:
  121. LogOutputBuffer(Logger &logger) :
  122. m_logger(logger)
  123. {
  124. updateLogLevel();
  125. };
  126. virtual ~LogOutputBuffer()
  127. {
  128. m_logger.removeOutput(this);
  129. }
  130. void updateLogLevel();
  131. void logRaw(LogLevel lev, const std::string &line);
  132. void clear()
  133. {
  134. m_buffer = std::queue<std::string>();
  135. }
  136. bool empty() const
  137. {
  138. return m_buffer.empty();
  139. }
  140. std::string get()
  141. {
  142. if (empty())
  143. return "";
  144. std::string s = m_buffer.front();
  145. m_buffer.pop();
  146. return s;
  147. }
  148. private:
  149. std::queue<std::string> m_buffer;
  150. Logger &m_logger;
  151. };
  152. extern StreamLogOutput stdout_output;
  153. extern StreamLogOutput stderr_output;
  154. extern std::ostream null_stream;
  155. extern std::ostream *dout_con_ptr;
  156. extern std::ostream *derr_con_ptr;
  157. extern std::ostream *derr_server_ptr;
  158. extern Logger g_logger;
  159. // Writes directly to all LL_NONE log outputs for g_logger with no prefix.
  160. extern std::ostream rawstream;
  161. extern std::ostream errorstream;
  162. extern std::ostream warningstream;
  163. extern std::ostream actionstream;
  164. extern std::ostream infostream;
  165. extern std::ostream verbosestream;
  166. extern std::ostream dstream;
  167. #define TRACEDO(x) do { \
  168. if (g_logger.getTraceEnabled()) { \
  169. x; \
  170. } \
  171. } while (0)
  172. #define TRACESTREAM(x) TRACEDO(verbosestream x)
  173. #define dout_con (*dout_con_ptr)
  174. #define derr_con (*derr_con_ptr)