Logging - Steelhead-Games/CharmQuarkEngine-wiki GitHub Wiki

Logging System

The logging system is part of the Core module.

#include <Logging/Logging.h>

Log files are located in the ./Logs directory relative to the path specified in the -project_root CLI argument.

Note: Only up to 8 logs including latest.log are stored at a time; older files are deleted automatically.

Simple logs

using namespace cqe;

Core::Log::Info("Thanks for using CQE!");
Core::Log::Info("{} strings are allowed", "Format");

Logging severity

There are four severity levels: Debug, Info, Warn and Error.

Core::Log::Debug("Debug");
Core::Log::Info("Info");
Core::Log::Warn("Warn");
Core::Log::Error("Error");

Custom types

There are two ways to make a custom type loggable.

The recommended approach is to add a public std::string ToString() method to the class.

class Book
{
    unsigned m_page = 42;

public:
    std::string ToString() const
    {
        return Core::Log::Fmt("Book(Page: {})", m_page);
    }
};

ToString method can be inherited from other classes and overridden.

If, for some reason, the implementation of the method is not possible, custom type can be made loggable with appropriate std::formatter partial definition.

template <>
struct std::formatter<Book> : std::formatter<std::string>
{
    auto format(const Book& book, format_context& ctx) const
    {
        std::string result = Core::Log::Fmt("Book(Page: {})", book.page);
        return std::formatter<std::string>::format(result, ctx);
    }
};

WARNING: The presence of both the ToString method and the std::formatter partial definition will result in ambiguity during compilation.

Loggers

Loggers are specialized objects used for logical separation of systems in logs.

Core::Log::Info("Hello, world!");
// Result:
// [...] [cqe] [info] Hello, world!
const Logger RHILogger("cqe::RHI");

RHILogger.Info("Hello, world!");
// Result:
// [...] [cqe::RHI] [info] Hello, world!

Loggers can be defined in the headers of the systems they belong to; their uniqueness is not mandatory.

// MySystem.h

static const Core::Log::Logger MySystemLogger("cqe::MySystem");
// MySystem.cpp

MySystemLogger.Info("Initializing...");
// [...] [cqe::MySystem] [info] Initializing...

Initialization

Logging system initialization should be incorporated into the engine by default.

If for some reason it is not or some sort of modification is needed, note that it can be performed with Init() function call.

Core::Log::Init();

Logging system assumes that it is initialized after the file system and before calling of either of the logging functions.

Initialization should be performed before creating any parallel threads that may require logging.

⚠️ **GitHub.com Fallback** ⚠️