Logging.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #pragma once
  2. #include <functional>
  3. #include <source_location>
  4. #include "Array.h"
  5. #include "Text.h"
  6. namespace Framework
  7. {
  8. class Datei;
  9. namespace Logging
  10. {
  11. /**
  12. * logs a message with level LogLevel::trace
  13. *
  14. * \param location the location of the call
  15. * \return a stream to write the message to
  16. */
  17. DLLEXPORT FlushingOStream trace(
  18. std::source_location location = std::source_location::current());
  19. /**
  20. * logs a message with level LogLevel::debug
  21. *
  22. * \param location the location of the call
  23. * \return a stream to write the message to
  24. */
  25. DLLEXPORT FlushingOStream debug(
  26. std::source_location location = std::source_location::current());
  27. /**
  28. * logs a message with level LogLevel::info
  29. *
  30. * \param location the location of the call
  31. * \return a stream to write the message to
  32. */
  33. DLLEXPORT FlushingOStream info(
  34. std::source_location location = std::source_location::current());
  35. /**
  36. * logs a message with level LogLevel::warning
  37. *
  38. * \param location the location of the call
  39. * \return a stream to write the message to
  40. */
  41. DLLEXPORT FlushingOStream warning(
  42. std::source_location location = std::source_location::current());
  43. /**
  44. * logs a message with level LogLevel::error
  45. *
  46. * \param location the location of the call
  47. * \return a stream to write the message to
  48. */
  49. DLLEXPORT FlushingOStream error(
  50. std::source_location location = std::source_location::current());
  51. /**
  52. * provides access to the logging handler
  53. *
  54. * \return a reference to the logging handler without increasing the
  55. * reference count
  56. */
  57. class LoggingHandler;
  58. DLLEXPORT LoggingHandler* zLoggingHandler();
  59. enum class LogLevel
  60. {
  61. Trace,
  62. Debug,
  63. Info,
  64. Warning,
  65. Error
  66. };
  67. /**
  68. * Formats a log message before it is written to a LoggingChannel.
  69. */
  70. class LoggingFormat : public Framework::ReferenceCounter
  71. {
  72. public:
  73. DLLEXPORT LoggingFormat();
  74. DLLEXPORT virtual ~LoggingFormat();
  75. /**
  76. * formats a log message.
  77. *
  78. * \param msg the message to format
  79. * \param level the used log level
  80. * \param location the location of the call
  81. * \return the formatted message
  82. */
  83. virtual Framework::Text formatMessage(const Framework::Text& msg,
  84. LogLevel level,
  85. const std::source_location& location) const
  86. = 0;
  87. };
  88. enum class Color
  89. {
  90. Black,
  91. RED,
  92. GREEN,
  93. YELLOW,
  94. Blue,
  95. Magenta,
  96. CYAN,
  97. WHITE,
  98. LIGHT_Black,
  99. LIGHT_RED,
  100. LIGHT_GREEN,
  101. LIGHT_YELLOW,
  102. LIGHT_Blue,
  103. LIGHT_Magenta,
  104. LIGHT_CYAN,
  105. LIGHT_WHITE,
  106. };
  107. /**
  108. * Builds a custom LoggingFormat.
  109. */
  110. class LoggingFormatBuilder
  111. {
  112. private:
  113. std::function<LoggingFormat*(LoggingFormat*)> formatTransformer;
  114. DLLEXPORT LoggingFormatBuilder(
  115. std::function<LoggingFormat*(LoggingFormat*)>
  116. formatTransformer);
  117. public:
  118. /**
  119. * creates a new format builder without any formatting.
  120. *
  121. */
  122. DLLEXPORT LoggingFormatBuilder();
  123. /**
  124. * adds a date to the format.
  125. *
  126. * \param format the format of the date y for year, m for month, d
  127. * for day, h for hour, i for minute, s for second example: "y-m-d
  128. * h:i:s"
  129. * \param postBlank true if a blank should be inserted after this
  130. * value
  131. *
  132. * \return the next builder
  133. */
  134. DLLEXPORT LoggingFormatBuilder datetime(
  135. Framework::Text format, bool postBlank = true) const;
  136. /**
  137. * adds the name of the file where the log message comes from.
  138. *
  139. * \param postBlank true if a blank should be inserted after this
  140. * value
  141. *
  142. * \return the next builder
  143. */
  144. DLLEXPORT LoggingFormatBuilder fileName(
  145. bool postBlank = true) const;
  146. /**
  147. * adds the line of the file where the log message comes from.
  148. *
  149. * \param postBlank true if a blank should be inserted after this
  150. * value
  151. *
  152. * \return the next builder
  153. */
  154. DLLEXPORT LoggingFormatBuilder fileLine(
  155. bool postBlank = true) const;
  156. /**
  157. * adds the name of the function where the log message comes from.
  158. *
  159. * \param postBlank true if a blank should be inserted after this
  160. * value
  161. *
  162. * \return the next builder
  163. */
  164. DLLEXPORT LoggingFormatBuilder functionName(
  165. bool postBlank = true) const;
  166. /**
  167. * adds the log level of the message.
  168. *
  169. * \param postBlank true if a blank should be inserted after this
  170. * value
  171. *
  172. * \return the next builder
  173. */
  174. DLLEXPORT LoggingFormatBuilder level(bool postBlank = true) const;
  175. /**
  176. * sets the color of the mesage after this call
  177. *
  178. * \param color the color
  179. * \return the next builder
  180. */
  181. DLLEXPORT LoggingFormatBuilder color(Color color) const;
  182. /**
  183. * sets the color of the mesage after this call if a specific log
  184. * level is used
  185. *
  186. * \param level the log level
  187. * \param color the color
  188. * \return the next builder
  189. */
  190. DLLEXPORT LoggingFormatBuilder color(
  191. LogLevel level, Color color) const;
  192. /**
  193. * sets the background color of the mesage after this call
  194. *
  195. * \param color the color
  196. * \return the next builder
  197. */
  198. DLLEXPORT LoggingFormatBuilder backgroundColor(Color color) const;
  199. /**
  200. * sets the background color of the mesage after this call if a
  201. * specific log level is used
  202. *
  203. * \param level the log level
  204. * \param color the color
  205. * \return the next builder
  206. */
  207. DLLEXPORT LoggingFormatBuilder backgroundColor(
  208. LogLevel level, Color color) const;
  209. /**
  210. * adds a custom text to the log message.
  211. *
  212. * \param text the text to add
  213. * \return the next builder
  214. */
  215. DLLEXPORT LoggingFormatBuilder text(Framework::Text text) const;
  216. /**
  217. * creates the logging format.
  218. *
  219. * \return the logging format
  220. */
  221. DLLEXPORT LoggingFormat* build() const;
  222. };
  223. /**
  224. * writes log messages to a specific output.
  225. */
  226. class LoggingChannel : public ReferenceCounter
  227. {
  228. private:
  229. LoggingFormat* format;
  230. public:
  231. DLLEXPORT LoggingChannel();
  232. DLLEXPORT virtual ~LoggingChannel();
  233. /**
  234. * sets the format of the log messages
  235. *
  236. * \param format the format
  237. */
  238. DLLEXPORT void setFormat(LoggingFormat* format);
  239. /**
  240. * writes a log message to this channel.
  241. *
  242. * \param msg the message
  243. * \param level the log level
  244. * \param location the location of the call
  245. */
  246. DLLEXPORT void writeMessage(const Text& msg,
  247. LogLevel level,
  248. const std::source_location& location) const;
  249. protected:
  250. /**
  251. * will be called to write the message to the channel.
  252. *
  253. * \param msg the message
  254. */
  255. virtual void writeMessage(const Text& message) const = 0;
  256. };
  257. /**
  258. * logs messages to the std::cout.
  259. */
  260. class ConsoleLoggingChannel : public LoggingChannel
  261. {
  262. public:
  263. /**
  264. * creates a new console logging channel.
  265. *
  266. */
  267. DLLEXPORT ConsoleLoggingChannel();
  268. protected:
  269. /**
  270. * writes a message to the std::cout.
  271. *
  272. * \param msg the message
  273. * \param level the log level
  274. * \param location the location of the call
  275. */
  276. DLLEXPORT virtual void writeMessage(const Text& msg) const override;
  277. };
  278. /**
  279. * logs messages to a file.
  280. */
  281. class FileLoggingChannel : public LoggingChannel
  282. {
  283. private:
  284. Datei* file;
  285. public:
  286. /**
  287. * creates a new file logging channel.
  288. *
  289. * \param filePath the path to the file. If the file already exists
  290. * the messages will be appended to the file.
  291. */
  292. DLLEXPORT FileLoggingChannel(Text filePath);
  293. DLLEXPORT ~FileLoggingChannel();
  294. protected:
  295. /**
  296. * writes a message to the file.
  297. *
  298. * \param msg the message
  299. */
  300. DLLEXPORT virtual void writeMessage(const Text& msg) const override;
  301. };
  302. /**
  303. * used to configure channels where logged messages should be written
  304. * to.
  305. */
  306. class LoggingHandler : public ReferenceCounter
  307. {
  308. private:
  309. RCArray<LoggingChannel>** channels;
  310. public:
  311. DLLEXPORT LoggingHandler();
  312. DLLEXPORT ~LoggingHandler();
  313. /**
  314. * adds a channel for a specific log level.
  315. *
  316. * \param level the log level
  317. * \param channel the channel to add
  318. */
  319. DLLEXPORT void addChannel(LogLevel level, LoggingChannel* channel);
  320. /**
  321. * removes a channel for a specific log level.
  322. *
  323. * \param level the log level
  324. * \param channel the channel to remove without increasing the
  325. * reference count
  326. */
  327. DLLEXPORT void removeLoggingChannel(
  328. LogLevel level, LoggingChannel* zChannel);
  329. /**
  330. * adds a channel to all log levels.
  331. *
  332. * \param channel the channel to add
  333. */
  334. DLLEXPORT void addChannel(LoggingChannel* channel);
  335. /**
  336. * removes a channel from all log levels.
  337. *
  338. * \param channel the channel to remove without increasing the
  339. * reference count
  340. */
  341. DLLEXPORT void removeLoggingChannel(LoggingChannel* zChannel);
  342. /**
  343. * logs a message to all configured channels of a specific log
  344. * level.
  345. *
  346. * \param level the log level
  347. * \param message the message
  348. */
  349. DLLEXPORT void log(LogLevel level,
  350. std::source_location& location,
  351. const Text message) const;
  352. };
  353. } // namespace Logging
  354. } // namespace Framework