log.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. colored = (Logger::color_mode == LOG_COLOR_ALWAYS) ||
  100. (Logger::color_mode == LOG_COLOR_AUTO && isatty(fileno(stdout)));
  101. #else
  102. colored = Logger::color_mode == LOG_COLOR_ALWAYS;
  103. #endif
  104. }
  105. void logRaw(LogLevel lev, const std::string &line)
  106. {
  107. bool colored_message = colored;
  108. if (colored_message)
  109. switch (lev) {
  110. case LL_ERROR:
  111. // error is red
  112. m_stream << "\033[91m";
  113. break;
  114. case LL_WARNING:
  115. // warning is yellow
  116. m_stream << "\033[93m";
  117. break;
  118. case LL_INFO:
  119. // info is a bit dark
  120. m_stream << "\033[37m";
  121. break;
  122. case LL_VERBOSE:
  123. // verbose is darker than info
  124. m_stream << "\033[2m";
  125. break;
  126. default:
  127. // action is white
  128. colored_message = false;
  129. }
  130. m_stream << line << std::endl;
  131. if (colored_message)
  132. // reset to white color
  133. m_stream << "\033[0m";
  134. }
  135. private:
  136. std::ostream &m_stream;
  137. bool colored;
  138. };
  139. class FileLogOutput : public ICombinedLogOutput {
  140. public:
  141. void open(const std::string &filename);
  142. void logRaw(LogLevel lev, const std::string &line)
  143. {
  144. m_stream << line << std::endl;
  145. }
  146. private:
  147. std::ofstream m_stream;
  148. };
  149. class LogOutputBuffer : public ICombinedLogOutput {
  150. public:
  151. LogOutputBuffer(Logger &logger, LogLevel lev) :
  152. m_logger(logger)
  153. {
  154. m_logger.addOutput(this, lev);
  155. }
  156. ~LogOutputBuffer()
  157. {
  158. m_logger.removeOutput(this);
  159. }
  160. void logRaw(LogLevel lev, const std::string &line)
  161. {
  162. m_buffer.push(line);
  163. }
  164. bool empty()
  165. {
  166. return m_buffer.empty();
  167. }
  168. std::string get()
  169. {
  170. if (empty())
  171. return "";
  172. std::string s = m_buffer.front();
  173. m_buffer.pop();
  174. return s;
  175. }
  176. private:
  177. std::queue<std::string> m_buffer;
  178. Logger &m_logger;
  179. };
  180. extern StreamLogOutput stdout_output;
  181. extern StreamLogOutput stderr_output;
  182. extern std::ostream null_stream;
  183. extern std::ostream *dout_con_ptr;
  184. extern std::ostream *derr_con_ptr;
  185. extern std::ostream *dout_server_ptr;
  186. extern std::ostream *derr_server_ptr;
  187. #ifndef SERVER
  188. extern std::ostream *dout_client_ptr;
  189. extern std::ostream *derr_client_ptr;
  190. #endif
  191. extern Logger g_logger;
  192. // Writes directly to all LL_NONE log outputs for g_logger with no prefix.
  193. extern std::ostream rawstream;
  194. extern std::ostream errorstream;
  195. extern std::ostream warningstream;
  196. extern std::ostream actionstream;
  197. extern std::ostream infostream;
  198. extern std::ostream verbosestream;
  199. extern std::ostream dstream;
  200. #define TRACEDO(x) do { \
  201. if (g_logger.getTraceEnabled()) { \
  202. x; \
  203. } \
  204. } while (0)
  205. #define TRACESTREAM(x) TRACEDO(verbosestream x)
  206. #define dout_con (*dout_con_ptr)
  207. #define derr_con (*derr_con_ptr)
  208. #define dout_server (*dout_server_ptr)
  209. #ifndef SERVER
  210. #define dout_client (*dout_client_ptr)
  211. #endif