Simple C++ logger uses singleton pattern to implement logging into a single file from any place in the code of your C++ code.
Use FetchContent:
include(FetchContent)
FetchContent_Declare(
scl
GIT_REPOSITORY https://github.com/AlbertRtk/simple_cpp_logger.git
GIT_TAG v.1.0.0
)
FetchContent_MakeAvailable(scl)
add_executable(main src/main.cpp)
target_link_libraries(main PRIVATE scl)
Sample code:
#include "scl_logger.h"
int main(int argc, char *argv[]) {
scl::Logger::GetLogger().Initialize("my_log_file.log");
scl::Logger::GetLogger().MakeVerbose();
LOG_INFO("Logger started...");
LOG_DEBUG("Debug message.");
LOG_WARNING("Warning message!");
LOG_ERROR("ERROR MESSAGE!!!");
return 0;
}
- Initialize the logger.
Initialize
method takes path to an output log file as input:scl::Logger::GetLogger().Initialize("my_log_file.log");
- Once initialize, you can make the logger verbose (outputs to a log file and
stdout
) or silent (outputs only to a log file):scl::Logger::GetLogger().MakeVerbose(); scl::Logger::GetLogger().MakeSilent();
- You can use predefined macros to to log messages of different level:
LOG_INFO("Logger started..."); LOG_DEBUG("Debug message."); // logged only in debug builds LOG_WARNING("Warning message!"); LOG_ERROR("ERROR MESSAGE!!!");