This library provides a simple logging system, and some additional utility functionality for C++ projects. The library is built using Premake5 for C++23.
- Clone the repository and open a terminal in the project root.
The next steps depend on your preferred build system below.
- Run
premake5 vs20XXto generate a Visual Studio solution file (.sln). - Open the solution file in Visual Studio and build using MSVC.
- Run the pre-defined action
premake5 gmake-gccto generate makefiles specifically for GCC. - Navigate into
/build/[os]-gccwhere theMakefileis created. - Run
make config=[build type]where the possible options aredebug,releaseordist. - Navigate into
/bin/Sandbox/[build type]and run theSandboxexecutable.
- Run the pre-defined action
premake5 gmake-clangto generate makefiles specifically for Clang. - Navigate into
/build/[os]-clangwhere theMakefileis created. - Run
make config=[build type]where the possible options aredebug,releaseordist. - Navigate into
/bin/Sandbox/[build type]and run theSandboxexecutable.
There are additional actions for formatting with clang-format and linting through clang-tidy. These are run through:
# Run clang-format
premake5 format
# Run clang-tidy
premake5 lint
# Run clang-tidy and apply fixes
premake5 lint-fixThese commands assume clang-format and clang-tidy are installed on your system.
If you use clangd for intellisense and code completion, the provided gen-build-cmds.sh script will generate build commands for clangd. This will call premake5 gmake-clang and use bear to generate the build commands. Both clang and bear are assumed to be installed on your system.
- Premake5: This library uses Premake5 as its build configuration tool.
Ensure thatpremake5is installed on your system or copied into therootfolder.
You can download it here.
This library can be used as a git submodule in other Premake5 projects. Add it as a submodule and include the Log project definition in your premake5.lua:
include("path/to/log-lib/log-project.lua")
project("YourProject")
-- ...
includedirs({ "path/to/log-lib/log-lib/include" })
links({ "Log" })The log-project.lua file defines only the Log project with automatic path resolution, while premake5.lua is used for standalone builds including the Sandbox example.
To use the library, include the Log.h header file in your project. The logger can be accessed from anywhere in your code.
Log messages include a severity level, information about the file and line number, and the message itself. This is displayed with suitable colors corresponding to the selected severity. The logger is also configured to only log messages for specified build types.
Where logs are written is determined by adding sinks to the Logger singleton. Multiple console and/or file sinks with a specified severity range can be added to control what logs end up where. For example, this makes it possible to log everything to the console but only record the errors in a dedicated error file.
In addition to the logging functionality, there are also macros for throwing exceptions with messages. The exceptions are formatted in the same way as the log messages. Furthermore, there is basic functionality for timing code execution.
The build configuration determines which logging macros are active:
| Configuration | Define | Logging |
|---|---|---|
debug |
AE_DEBUG |
AE_LOG() and AE_LOG_BOTH() are active |
release |
AE_RELEASE |
AE_LOG_RELEASE() and AE_LOG_BOTH() are active |
dist |
AE_DIST |
All logging disabled |
Use AE_LOG() for debug-only messages, AE_LOG_RELEASE() for release-only, and AE_LOG_BOTH() for messages in both builds.
Below is some example code demonstrating basic usage.
#include "Log.h"
void Demo()
{
// Author: Rasmus Hugosson
// Date: 2025-12-06
// Description: This is a simple example of how to use this library (see /sandbox/src/Sandbox.cpp for full demo)
// Let's start by adding a sink where the logs will show up
ae::Logger::Get().AddConsoleSink("Console", ae::LogSinkConsoleKind::STDOUT, AE_TRACE); // AE_TRACE means trace or higher severity will be logged here
ae::Logger::Get().AddFileSink("Error file", "logs/errors.txt", AE_ERROR); // Only errors or fatal errors will be recorded here
// Now we can log a simple message with the following macro
AE_LOG(AE_INFO, "Hello World!");
// A special macro is provided for blank lines since printing "\n" manually can result in incorrect formatting
AE_LOG_NEWLINE();
// There are 5 log levels: Trace, Info, Warning, Error, Fatal
// The log levels are color coded and a tag is displayed before the message
AE_LOG(AE_TRACE, "This message is not important");
AE_LOG(AE_INFO, "This is an information message");
AE_LOG(AE_WARNING, "This is a warning!");
AE_LOG(AE_ERROR, "This is an error!");
AE_LOG(AE_FATAL, "This is a fatal error!");
AE_LOG_NEWLINE();
// All log messages can be formatted through std::format, like using normal std::print
AE_LOG(AE_TRACE, "The answer to life, the universe and everything is {}", 42);
AE_LOG(AE_TRACE, "{} is the value of pi", 3.14159265359);
}An extended example of how this library can be used is provided in /sandbox/src/Sandbox.cpp. See this file for further information regarding functionality.
- Windows (MSVC)
- Linux (GCC / Clang)
- Likely MacOS (not yet tested, but ANSI branch is portable)
This library is licensed under the Apache License 2.0.
See the LICENSE file in this repository for details.
