8000 GitHub - sodeprecated/cpplogger: C++ Library that provides tools for fast and simple formatted logging to console and files
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

sodeprecated/cpplogger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Announcements:

Release 1.0.0

Release 1.0.0 is now available.

Welcome to cpplogger!

cpplogger will bring new colors to your life! Make simple console output more exciting just in a few lines of code.

Getting started:

Just include one log.hpp that you can find in release folder on github.

Namespace:

  • cpplogger works in logger:: namespace

Types:

  • lo 8000 gger::error structure inherited from std::exception that holds all errors and has own stack for easier tracing with Trace macro.
  • logger::log_message_type enum with common log types.
  • logger::kModifier enum with modifiers that you could apply to your console output. See more about this in example section.
  • logger::style typedef for std::vector<kModifier> with overloaded operator<<.

Functions:

  • logger::BindConsoleStyle(s, args...) (args must be instances of logger::kModifier) creates a new logger::style with name s and modifiers args.... Returns true if new style was successfully created. More information about styles in example section.
  • logger::BindLogDirectory(s) this function redefine default(project working directory) logging directory to s (s must be a valid path, doesn't matter relative or full).

Macros:

  • ConsoleLog(s, args...) takes string as first argument (See in description below more about parsing rules), then takes variable that has overloaded operator<< and put them instead of %v in string. logger:: kModifier in argument list is undefined behaviour. Returns true if everything OK with console output.
  • Trace(x) takes logger::error as argument and push to error's stack current filepath, function name and line where Trace was called. Returns logger::error.
  • FileLog(type, args...) takes logger::log_message_type as first argument. Creates folders in format logs/{year}/{month}/ddmmyyyy.log and outputs(in specific format) all variables provided to the function(new line for each variable).
  • DEBUG_ONLY disables file and console output. Type #define DEBUG_ONLY before(!) including cpplogger files.
  • OS_WIN/OS_UNIX determines current working system.

Platforms:

  • Windows
  • MacOs
  • Linux

Example:

Parsing rules:

Variables
  • %v Variables and modifiers are put instead of ‘%v’ in the order that they are specified in arguments list. Variable must has overloaded operator<<.
Date/Time
  • %h puts current hour in format of two digits e.g 00,05,13
  • %m puts current minute in format of two digits e.g 00,10,59
  • %s puts current second in format of two digits e.g 00,10,59
  • %dd puts day in format of two digits e.g. 01,02,12
  • %mm puts month in format of two digits e.g. 01,02,12
  • %yy puts year in format of two digits e.g. 19,20,21
  • %yyyy puts day in format of four digits e.g. 2019,2020,2021
Macros
  • %FILE puts current filename (!without path)

  • %FUNC puts current function name

  • %LINE puts current line in source file

  • %PATH puts full path to the current file including filename

  • %% puts %

Class
  • %.ClassName( ... %) Apply logger::style to 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!

Code example:

#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:

About

C++ Library that provides tools for fast and simple formatted logging to console and files

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

0