The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

107 lines
5.4 KiB

2 months ago
  1. L3++: Lightweight Logging Library for C++
  2. =====
  3. L3++ is a self-contained, single-header, cross-platform logging library for C++.
  4. The main goals for this library are simplicity, modularity and ease of use.
  5. This library is released under the MIT License.
  6. Copyright (C) 2015 Gereon Kremer
  7. Concepts
  8. =====
  9. L3++ is based on the following conceptual components:
  10. * RecordInfo: A record info stores auxiliary information of a log message like the filename, line number and function name where the log message was emitted.
  11. * Logger: A logger categorizes log messages, usually according to logical components or modules in the source code.
  12. * Sink: A sink represents a logging output, for example the terminal or a log file.
  13. * Formatter: A formatter is associated with a sink and converts a log message into an actual string.
  14. Log levels
  15. -----
  16. The following log levels exist (from `l3pp::LogLevel`):
  17. * ALL
  18. * TRACE
  19. * DEBUG
  20. * INFO
  21. * WARN
  22. * ERR
  23. * FATAL
  24. * OFF
  25. Hierarchical Loggers
  26. -----
  27. Loggers are hierarchically structured strings like `"app.module.submodule"`. In this example, `submodule` is considered a sublogger of `module` and `app` the parent of `module`. A sublogger implicitly inherits all sinks and the log level of the parent, unless explicitly configured otherwise.
  28. The hierarchical tree of loggers always contains the root logger at the top. The root logger itself does not have a parent, nor a name. It can be accessed via `l3pp::Logging::getRootLogger()`.
  29. Each logger is assigned a log level, or is configured to inherit the log level of the parent (with the special log level `l3pp::LogLevel::INHERIT`). Note that the root logger cannot inherit a log level. Any log entry with a lower level is filtered out and will not be logged.
  30. A logger can also be assigned one or more sinks. By default, a logger will log to both the sinks of its parent, as well as its own sinks. Should a logger only use it own sinks, it should be set to non-additive (using `Logger::setAdditive(false)`).
  31. Sinks
  32. -----
  33. A sink provides an output for loggers. Loggers may define multiple sinks, and sinks may be shared between loggers. Sinks are associated with a formatter and a log level. The log level specifies the minimum level of a message for it to be output (independent of the log level of a logger), and by default permits all log messages. A formatter formats the log messages before being output. By default, a simple formatter is used which prints the log level, message and a newline, but other formatters can be specified.
  34. Formatters
  35. -----
  36. A formatter shapes a log message before being sent to its final destination. A non-configurable simple formatter exists, as well as a template-based formatter. The latter specifies the format of a message by means of its template arguments, see `l3pp::makeTemplateFormatter`.
  37. Basic Usage
  38. =====
  39. A logger object can be accessed via `l3pp::Logger::getLogger()` or `l3pp::Logger::getRootLogger()`. By default, the root logger does not output anywhere. Therefore, a sink should be added. An initial configuration may look like this:
  40. l3pp::Logger::initialize();
  41. l3pp::SinkPtr sink = log4carl::StreamSink::create(std::clog);
  42. l3pp::Logger::getRootLogger()->addSink(sink);
  43. l3pp::Logger::getRootLogger()->setLevel(log4carl::LogLevel::INFO);
  44. In this demo, a single sink is created that passes log messages to the standard logging stream `std::clog`. All messages must have at least level `LVL_INFO` before being printed.
  45. The actual logging is performed using a handful of macros.
  46. These macros
  47. Considerations
  48. =====
  49. Performance
  50. -----
  51. While the use of hierarchical loggers and multiple sinks with associated formatters gives a lot of flexibility, it comes at a certain price. As the configuration is done at runtime (and may even change at runtime), the question whether a certain message is printed can only be answered at runtime. Therefore, every message, whether you will ever see it or not, has to pass through the logger and cost runtime.
  52. To mitigate this, we suggest the following:
  53. Create a preprocessor flag (like `ENABLE_LOGGING`) and define your own set of logging macros.
  54. If this flag is defined, make your macros forward to the `L3PP_LOG_*` macros.
  55. If this flag is not defined, make your macros do nothing.
  56. Multiple usages in the same project
  57. -----
  58. Assume you have an application that uses L3++ for logging as well as some other library that also uses L3++.
  59. L3++ will play nicely in this scenario (partly, it was designed for this case).
  60. However, you should take care of a few things:
  61. * Colliding loggers: Prefix your loggers with some unique prefix.
  62. * Colliding macros: If you implement the aforementioned `ENABLE_LOGGING` macro, prefix your macros with your project name. Otherwise, these macros will collide.
  63. Implementation Details
  64. =====
  65. Sinks
  66. -----
  67. A sink is a class that provides some `log` method. Any class that inherits from `l3pp::Sink` can be used.
  68. As of now, two implementations are available:
  69. * FileSink: Writes to a output file.
  70. * StreamSink: Writes to any given `std::ostream`, for example to `std::cout`.
  71. Formatters
  72. -----
  73. A formatter is a functor that given a log entry, provides a formatted string. The base class `Formatter` provides some very simple formatting, whereas `TemplateFormatter` provides more control over the shape. Internally, a `TemplateFormatter` streams its arguments to a stream before constructing the string. The special types `FieldStr` and `TimeStr` can be used to format particular attributes of a log entry.