Skip to content

Vincent-Devine/OMLogger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OMLogger

Header-Only Version C++20 Windows License: MIT

OMLogger (One More Logger) is a single-header logging library for C++20, designed for clarity, flexibility, and ease of use.
It supports console and file outputs, verbosity filtering, color-coded messages, tag-based categorization, and more — all with zero dependencies.

png

Table of Contents

Features

  • Console output with colored logs
  • Optional file output
  • Configurable verbosity levels
  • Tag system for filtering and categorizing logs
  • Assertion macro with custom error messages
  • Lightweight and header-only

Add to your project

Add the OMLogger/Logger.hpp file to your project and include it where needed.

#include "OMLogger/Logger.hpp"

There are no external dependencies.

Usage

Basic logging

Use the macros to log your messages:

int main()
{
	OM_LOG_DEBUG("your text here");
	OM_LOG_INFO("your text here");
	OM_LOG_WARNING("your text here");
	OM_LOG_ERROR("your text here");
	OM_LOG_CRITICAL("your text here");

	OM::Logger::Logger::GetInstance()->Destroy(); // Important!
}

Verbosity Levels

Use the SetVerbosity() method to filter log messages:

int main()
{
	OM::Logger::Logger::GetInstance()->SetVerbosity(OM::Logger::VerbosityDebug | OM::Logger::VerbosityInfo);
	OM_LOG_DEBUG("Only debug and info verbosity");		// Displayed
	OM_LOG_INFO("Only debug and info verbosity");		// Displayed 
	OM_LOG_WARNING("Only debug and info verbosity");	// Not displayed
	OM_LOG_ERROR("Only debug and info verbosity");		// Not displayed
	OM_LOG_CRITICAL("Only debug and info verbosity");	// Not displayed
}

Tags

Tags: Engine, Core, Render, Input, Physics, Audio, Animation, UI, AI, Resource, Scripting, Tools, Scene
Use the macros to log and tag your messages:

int main()
{
	OM_LOG_INFO_TAG("your text here", OM::Logger::TagCore);
}

Display Settings

Settings: Date, Time, Thread, FileInfo, Verbosity, Tag
Use the SetDisplaySettings() method to configure what log information is shown:

int main()
{
	OM::Logger::Logger::GetInstance()->SetDisplaySettings(OM::Logger::DisplayTime);
	OM_LOG_INFO("Displays only date + message");

	OM::Logger::Logger::GetInstance()->SetDisplaySettings(OM::Logger::DisplayDate | OM::Logger::DisplayTime | OM::Logger::DisplayFileInfo);
	OM_LOG_INFO("Displays date + time + file info + message");
}

Result:
png

File Output

Use the OpenLogFile() and CloseLogFile() methods to enable or disable file output:

int main()
{
	// Beginning of the project
	OM::Logger::Logger* logger = OM::Logger::Logger::GetInstance();
	logger->OpenLogFile("log.txt");

	OM_LOG_DEBUG("Message written to console and log file");

	// End of the project
	logger->CloseLogFile();
}

By default, writing to the file will overwrite the existing contents of the log file. You can pass an additional parameter when calling the function to keep the existing entries instead of deleting them.

logger->OpenLogFile("log.txt", false);

Custom Data

To manually specify the file, function, and line, consider using helper functions. Make sure that the log entry corresponds to the function calling the helper, not the helper itself.

void Helper(std::string message, const char* file, int line, const char* function)
{
	OM_LOG_INFO_CUSTOM_DATA(message, OM::Logger::TagNone, file, line, function);
}
#define CUSTOM_LOG(message) Helper(message, __FILE__, __LINE__, __func__)

int main()
{
	// file, line and func are on the main and not on the Helper() function
	CUSTOM_LOG("Custom log");
}

Assertion

Use the OM_ASSERTION macro to assert your code:

int main()
{
	// If _DEBUG is defined: logs a critical message + triggers a breakpoint
	// Else: logs a critical message + aborts

	OM_ASSERTION(1 + 1 == 2, "your text here"); // Passes
	OM_ASSERTION(1 + 1 == 3, "your text here"); // Fails
}

Assertions support tags OM_ASSERTION_TAG and custom data OM_ASSERTION_CUSTOM_DATA, allowing you to categorize failures and specify the file, line, and function manually when needed.

Requirements

  • C++20
  • Windows OS (for console color support via WinAPI)

Credit

Made by: Vincent DEVINE

About

OMLogger (One More Logger) is a single-header logging library for C++20.

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages