Logging.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. */
  256. DLLEXPORT virtual void writeMessage(const Text& msg) const override;
  257. };
  258. /**
  259. * logs messages to OutputDebugString.
  260. */
  261. class OutputDebugStringLoggingChannel : public LoggingChannel
  262. {
  263. public:
  264. /**
  265. * creates a new cout logging channel.
  266. *
  267. */
  268. DLLEXPORT OutputDebugStringLoggingChannel();
  269. protected:
  270. /**
  271. * writes a message to OutputDebugString.
  272. *
  273. * \param msg the message
  274. */
  275. DLLEXPORT virtual void writeMessage(const Text& msg) const override;
  276. };
  277. /**
  278. * logs messages to the std::cout.
  279. */
  280. class ConsoleHandlerLoggingChannel : public LoggingChannel
  281. {
  282. private:
  283. ConsoleHandler* consolehandler;
  284. public:
  285. /**
  286. * creates a new console logging channel.
  287. *
  288. */
  289. DLLEXPORT ConsoleHandlerLoggingChannel(
  290. ConsoleHandler* consolehandler);
  291. DLLEXPORT ~ConsoleHandlerLoggingChannel();
  292. protected:
  293. /**
  294. * writes a message to the given ConsoleHandler.
  295. *
  296. * \param msg the message
  297. */
  298. DLLEXPORT virtual void writeMessage(const Text& msg) const override;
  299. };
  300. /**
  301. * logs messages to a file.
  302. */
  303. class FileLoggingChannel : public LoggingChannel
  304. {
  305. private:
  306. Datei* file;
  307. public:
  308. /**
  309. * creates a new file logging channel.
  310. *
  311. * \param filePath the path to the file. If the file already exists
  312. * the messages will be appended to the file.
  313. */
  314. DLLEXPORT FileLoggingChannel(Text filePath);
  315. DLLEXPORT FileLoggingChannel(Datei* file);
  316. DLLEXPORT ~FileLoggingChannel();
  317. protected:
  318. /**
  319. * writes a message to the file.
  320. *
  321. * \param msg the message
  322. */
  323. DLLEXPORT virtual void writeMessage(const Text& msg) const override;
  324. };
  325. /**
  326. * used to configure channels where logged messages should be written
  327. * to.
  328. */
  329. class LoggingHandler : public ReferenceCounter
  330. {
  331. private:
  332. RCArray<LoggingChannel>** channels;
  333. public:
  334. DLLEXPORT LoggingHandler();
  335. DLLEXPORT ~LoggingHandler();
  336. /**
  337. * adds a channel for a specific log level.
  338. *
  339. * \param level the log level
  340. * \param channel the channel to add
  341. */
  342. DLLEXPORT void addChannel(LogLevel level, LoggingChannel* channel);
  343. /**
  344. * removes a channel for a specific log level.
  345. *
  346. * \param level the log level
  347. * \param channel the channel to remove without increasing the
  348. * reference count
  349. */
  350. DLLEXPORT void removeLoggingChannel(
  351. LogLevel level, LoggingChannel* zChannel);
  352. /**
  353. * adds a channel to all log levels.
  354. *
  355. * \param channel the channel to add
  356. */
  357. DLLEXPORT void addChannel(LoggingChannel* channel);
  358. /**
  359. * removes a channel from all log levels.
  360. *
  361. * \param channel the channel to remove without increasing the
  362. * reference count
  363. */
  364. DLLEXPORT void removeLoggingChannel(LoggingChannel* zChannel);
  365. /**
  366. * logs a message to all configured channels of a specific log
  367. * level.
  368. *
  369. * \param level the log level
  370. * \param message the message
  371. */
  372. DLLEXPORT void log(LogLevel level,
  373. std::source_location& location,
  374. const Text message) const;
  375. };
  376. } // namespace Logging
  377. } // namespace Framework