123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- #pragma once
- #include <functional>
- #include <source_location>
- #include "Array.h"
- #include "Console.h"
- #include "Text.h"
- namespace Framework
- {
- class Datei;
- namespace Logging
- {
- /**
- * logs a message with level LogLevel::trace
- *
- * \param location the location of the call
- * \return a stream to write the message to
- */
- DLLEXPORT FlushingOStream trace(
- std::source_location location = std::source_location::current());
- /**
- * logs a message with level LogLevel::debug
- *
- * \param location the location of the call
- * \return a stream to write the message to
- */
- DLLEXPORT FlushingOStream debug(
- std::source_location location = std::source_location::current());
- /**
- * logs a message with level LogLevel::info
- *
- * \param location the location of the call
- * \return a stream to write the message to
- */
- DLLEXPORT FlushingOStream info(
- std::source_location location = std::source_location::current());
- /**
- * logs a message with level LogLevel::warning
- *
- * \param location the location of the call
- * \return a stream to write the message to
- */
- DLLEXPORT FlushingOStream warning(
- std::source_location location = std::source_location::current());
- /**
- * logs a message with level LogLevel::error
- *
- * \param location the location of the call
- * \return a stream to write the message to
- */
- DLLEXPORT FlushingOStream error(
- std::source_location location = std::source_location::current());
- /**
- * provides access to the logging handler
- *
- * \return a reference to the logging handler without increasing the
- * reference count
- */
- class LoggingHandler;
- DLLEXPORT LoggingHandler* zLoggingHandler();
- enum class LogLevel
- {
- Trace,
- Debug,
- Info,
- Warning,
- Error
- };
- /**
- * Formats a log message before it is written to a LoggingChannel.
- */
- class LoggingFormat : public Framework::ReferenceCounter
- {
- public:
- DLLEXPORT LoggingFormat();
- DLLEXPORT virtual ~LoggingFormat();
- /**
- * formats a log message.
- *
- * \param msg the message to format
- * \param level the used log level
- * \param location the location of the call
- * \return the formatted message
- */
- virtual Framework::Text formatMessage(const Framework::Text& msg,
- LogLevel level,
- const std::source_location& location) const
- = 0;
- };
- /**
- * Builds a custom LoggingFormat.
- */
- class LoggingFormatBuilder
- {
- private:
- std::function<LoggingFormat*(LoggingFormat*)> formatTransformer;
- DLLEXPORT LoggingFormatBuilder(
- std::function<LoggingFormat*(LoggingFormat*)>
- formatTransformer);
- public:
- /**
- * creates a new format builder without any formatting.
- *
- */
- DLLEXPORT LoggingFormatBuilder();
- /**
- * adds a date to the format.
- *
- * \param format the format of the date y for year, m for month, d
- * for day, h for hour, i for minute, s for second example: "y-m-d
- * h:i:s"
- * \param postBlank true if a blank should be inserted after this
- * value
- *
- * \return the next builder
- */
- DLLEXPORT LoggingFormatBuilder datetime(
- Framework::Text format, bool postBlank = true) const;
- /**
- * adds the name of the file where the log message comes from.
- *
- * \param postBlank true if a blank should be inserted after this
- * value
- *
- * \return the next builder
- */
- DLLEXPORT LoggingFormatBuilder fileName(
- bool postBlank = true) const;
- /**
- * adds the line of the file where the log message comes from.
- *
- * \param postBlank true if a blank should be inserted after this
- * value
- *
- * \return the next builder
- */
- DLLEXPORT LoggingFormatBuilder fileLine(
- bool postBlank = true) const;
- /**
- * adds the name of the function where the log message comes from.
- *
- * \param postBlank true if a blank should be inserted after this
- * value
- *
- * \return the next builder
- */
- DLLEXPORT LoggingFormatBuilder functionName(
- bool postBlank = true) const;
- /**
- * adds the log level of the message.
- *
- * \param postBlank true if a blank should be inserted after this
- * value
- *
- * \return the next builder
- */
- DLLEXPORT LoggingFormatBuilder level(bool postBlank = true) const;
- /**
- * sets the color of the mesage after this call
- *
- * \param color the color
- * \return the next builder
- */
- DLLEXPORT LoggingFormatBuilder color(Color color) const;
- /**
- * sets the color of the mesage after this call if a specific log
- * level is used
- *
- * \param level the log level
- * \param color the color
- * \return the next builder
- */
- DLLEXPORT LoggingFormatBuilder color(
- LogLevel level, Color color) const;
- /**
- * sets the background color of the mesage after this call
- *
- * \param color the color
- * \return the next builder
- */
- DLLEXPORT LoggingFormatBuilder backgroundColor(Color color) const;
- /**
- * sets the background color of the mesage after this call if a
- * specific log level is used
- *
- * \param level the log level
- * \param color the color
- * \return the next builder
- */
- DLLEXPORT LoggingFormatBuilder backgroundColor(
- LogLevel level, Color color) const;
- /**
- * adds a custom text to the log message.
- *
- * \param text the text to add
- * \return the next builder
- */
- DLLEXPORT LoggingFormatBuilder text(Framework::Text text) const;
- /**
- * creates the logging format.
- *
- * \return the logging format
- */
- DLLEXPORT LoggingFormat* build() const;
- };
- /**
- * writes log messages to a specific output.
- */
- class LoggingChannel : public ReferenceCounter
- {
- private:
- LoggingFormat* format;
- public:
- DLLEXPORT LoggingChannel();
- DLLEXPORT virtual ~LoggingChannel();
- /**
- * sets the format of the log messages
- *
- * \param format the format
- */
- DLLEXPORT void setFormat(LoggingFormat* format);
- /**
- * writes a log message to this channel.
- *
- * \param msg the message
- * \param level the log level
- * \param location the location of the call
- */
- DLLEXPORT void writeMessage(const Text& msg,
- LogLevel level,
- const std::source_location& location) const;
- protected:
- /**
- * will be called to write the message to the channel.
- *
- * \param msg the message
- */
- virtual void writeMessage(const Text& message) const = 0;
- };
- /**
- * logs messages to the std::cout.
- */
- class CoutLoggingChannel : public LoggingChannel
- {
- public:
- /**
- * creates a new cout logging channel.
- *
- */
- DLLEXPORT CoutLoggingChannel();
- protected:
- /**
- * writes a message to the std::cout.
- *
- * \param msg the message
- */
- DLLEXPORT virtual void writeMessage(const Text& msg) const override;
- };
- /**
- * logs messages to OutputDebugString.
- */
- class OutputDebugStringLoggingChannel : public LoggingChannel
- {
- public:
- /**
- * creates a new cout logging channel.
- *
- */
- DLLEXPORT OutputDebugStringLoggingChannel();
- protected:
- /**
- * writes a message to OutputDebugString.
- *
- * \param msg the message
- */
- DLLEXPORT virtual void writeMessage(const Text& msg) const override;
- };
- /**
- * logs messages to the std::cout.
- */
- class ConsoleHandlerLoggingChannel : public LoggingChannel
- {
- private:
- ConsoleHandler* consolehandler;
- public:
- /**
- * creates a new console logging channel.
- *
- */
- DLLEXPORT ConsoleHandlerLoggingChannel(
- ConsoleHandler* consolehandler);
- DLLEXPORT ~ConsoleHandlerLoggingChannel();
- protected:
- /**
- * writes a message to the given ConsoleHandler.
- *
- * \param msg the message
- */
- DLLEXPORT virtual void writeMessage(const Text& msg) const override;
- };
- /**
- * logs messages to a file.
- */
- class FileLoggingChannel : public LoggingChannel
- {
- private:
- Datei* file;
- public:
- /**
- * creates a new file logging channel.
- *
- * \param filePath the path to the file. If the file already exists
- * the messages will be appended to the file.
- */
- DLLEXPORT FileLoggingChannel(Text filePath);
- DLLEXPORT FileLoggingChannel(Datei* file);
- DLLEXPORT ~FileLoggingChannel();
- protected:
- /**
- * writes a message to the file.
- *
- * \param msg the message
- */
- DLLEXPORT virtual void writeMessage(const Text& msg) const override;
- };
- /**
- * used to configure channels where logged messages should be written
- * to.
- */
- class LoggingHandler : public ReferenceCounter
- {
- private:
- RCArray<LoggingChannel>** channels;
- public:
- DLLEXPORT LoggingHandler();
- DLLEXPORT ~LoggingHandler();
- /**
- * adds a channel for a specific log level.
- *
- * \param level the log level
- * \param channel the channel to add
- */
- DLLEXPORT void addChannel(LogLevel level, LoggingChannel* channel);
- /**
- * removes a channel for a specific log level.
- *
- * \param level the log level
- * \param channel the channel to remove without increasing the
- * reference count
- */
- DLLEXPORT void removeLoggingChannel(
- LogLevel level, LoggingChannel* zChannel);
- /**
- * adds a channel to all log levels.
- *
- * \param channel the channel to add
- */
- DLLEXPORT void addChannel(LoggingChannel* channel);
- /**
- * removes a channel from all log levels.
- *
- * \param channel the channel to remove without increasing the
- * reference count
- */
- DLLEXPORT void removeLoggingChannel(LoggingChannel* zChannel);
- /**
- * logs a message to all configured channels of a specific log
- * level.
- *
- * \param level the log level
- * \param message the message
- */
- DLLEXPORT void log(LogLevel level,
- std::source_location& location,
- const Text message) const;
- };
- } // namespace Logging
- } // namespace Framework
|