Logging.h 12 KB

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