Release 1.0.0 is now available.
cpplogger will bring new colors to your life! Make simple console output more exciting just in a few lines of code.
Just include one log.hpp that you can find in release folder on github.
- cpplogger works in
logger::namespace
logger::errorstructure inherited fromstd::exceptionthat holds all errors and has own stack for easier tracing withTracemacro.logger::log_message_typeenum with common log types.logger::kModifierenum with modifiers that you could apply to your console output. See more about this in example section.logger::styletypedef forstd::vector<kModifier>with overloadedoperator<<.
logger::BindConsoleStyle(s, args...)(args must be instances oflogger::kModifier) creates a newlogger::stylewith namesand modifiersargs.... Returnstrueif new style was successfully created. More information about styles in example section.logger::BindLogDirectory(s)this function redefine default(project working directory) logging directory tos(smust be a valid path, doesn't matter relative or full).
ConsoleLog(s, args...)takes string as first argument (See in description below more about parsing rules), then takes variable that has overloadedoperator<<and put them instead of%vin string.logger:: kModifierin argument list is undefined behaviour. Returnstrueif everything OK with console output.Trace(x)takeslogger::erroras argument and push to error's stack current filepath, function name and line whereTracewas called. Returnslogger::error.FileLog(type, args...)takeslogger::log_message_typeas first argument. Creates folders in formatlogs/{year}/{month}/ddmmyyyy.logand outputs(in specific format) all variables provided to the function(new line for each variable).DEBUG_ONLYdisables file and console output. Type#define DEBUG_ONLYbefore(!) including cpplogger files.OS_WIN/OS_UNIXdetermines current working system.
- Windows
- MacOs
- Linux
%vVariables and modifiers are put instead of ‘%v’ in the order that they are specified in arguments list. Variable must has overloadedoperator<<.
%hputs current hour in format of two digits e.g 00,05,13%mputs current minute in format of two digits e.g 00,10,59%sputs current second in format of two digits e.g 00,10,59%ddputs day in format of two digits e.g. 01,02,12%mmputs month in format of two digits e.g. 01,02,12%yyputs year in format of two digits e.g. 19,20,21%yyyyputs day in format of four digits e.g. 2019,2020,2021
-
%FILEputs current filename (!without path) -
%FUNCputs current function name -
%LINEputs current line in source file -
%PATHputs full path to the current file including filename -
%%puts%
%.ClassName( ... %)Applylogger::styleto the string enclosed between%.ClassName(and%). Supports nested classes. Class name shouldn't contain%because of undefined behaviour. Classes without close bracket%)are undefined behaviour.
All other symbols are put as is!
#include <iostream>
#include "log.hpp"
class Class {
int x_;
public:
Class(int x) : x_(x) {}
friend std::ostream& operator <<(std::ostream& os, const Class& a) {
os << "World " << a.x_;
return os;
}
};
void FunctionThatTrowsError() {
throw Trace(logger::error("error occur"));
}
int main() {
logger::BindConsoleStyle("Foo", logger::BG_WHITE, logger::FG_RED, logger::BOLD);
logger::BindConsoleStyle("Blink", logger::SLOW_BLINK);
logger::BindLogDirectory("./boo/");
try {
FunctionThatTrowsError();
} catch (logger::error& e) {
Trace(e);
FileLog(e);
}
ConsoleLog("%FILE:%FUNC:%LINE %.Foo([%dd.%mm.%yy - %h:%m:%s]%) -> %.Blink(%v %v!%)","Hello",Class(1));
FileLog(logger::T_WARNING,Class(2),"some warning");
return 0;
}
if we compile (considering that you have ./boo folder and log.hpp file in your directory) this code with g++ main.cpp -o main -std="c++17" and then type main #./main for unix systems to the console we will get:
In ./boo folder will appear file structure with format logs/{year}/{month}/ where you can find .log file similar to this:


