Skip to content

CPlusPlus API

Aleksander Płomiński edited this page Jun 22, 2025 · 1 revision

Documentation for the cpplogger class

General Description

The cpplogger class provides a simple logging mechanism for C++. It allows logging messages to both a file and the console. The logger supports different log levels such as info, warning, and error.

Constructor and Destructor

cpplogger(bool console_enabled, std::string log_file)

Initializes the logger by opening or creating a log file and writing an initialization message.

Parameters:

  • console_enabled: A boolean flag indicating whether logs should also be printed to the console.

  • log_file: The path to the log file where logs will be stored.

Example:

cpplogger logger(true, "app.log");

~cpplogger()

Closes the log file when the logger instance is destroyed.


Methods

void log(std::string msg)

Logs a general message with a timestamp.

Parameters:

  • msg: The log message.

Example:

logger.log("Application started");

void info(std::string msg, int level)

Logs an informational message with a specified importance level.

Parameters:

  • msg: The log message.

  • level: The importance level of the message.

Example:

logger.info("Initialized component", 1);

void warning(std::string msg, int level)

Logs a warning message with a specified importance level.

Parameters:

  • msg: The warning message.

  • level: The importance level of the warning.

Example:

logger.warning("Low disk space", 2);

void error(std::string msg, int level)

Logs an error message with a specified importance level.

Parameters:

  • msg: The error message.

  • level: The importance level of the error.

Example:

logger.error("Failed to connect to database", 3);

Notes

  • If console_enabled is set to true, logs will be printed to std::cout.

  • All log messages include a timestamp formatted as [ DD:MM:YY HH:MM:SS ].

  • The log file is opened in append mode (std::ios::app), ensuring that new logs are added at the end of the file.