Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

65 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kqlog

header-only, easy to use, C++ logging library minimum C++17

Setup

clone the repo with --recursive, it will clone: kqlog, fmt, magic_enum, Catch2.

Platforms

  • Linux: clang 15.0.7
  • Windows: clang 15.0.7

Features


  • Header only
  • Allows user defined log types(/levels)
  • Log filtering
  • Log color
  • Custom formtting for log pattern
  • Rich formatting, by fmt
  • Thread safe
  • Backup
  • xml for .npp log files (in progress)

Pattern flags

flag meaning example
%V The log itself "your text"
%T Event type "Debug" / "Info" / "YourEvent"
%Y Year "2023"
%M Month 01-12 "06"
%m Month full name "June"
%b Month abbriviated name "Jun"
%D Day 01-31 "23"
%d Day full name "Friday"
%B Day abbriviated name "Fri"
%H Hours 00-23 "18"
%N Minutes 00-59 "42"
%S Seconds 00-59 "36"
%t Thread id "1234"
%L Caller's source line "123"
%F Caller's function name "my_func"
%s Caller's source file "my_func_impl.cpp"
%% The % sign "%"
%K Black color black
%R Red color red
%G Green color green
%y Yellow color yellow
%E Blue color blue
%g Magenta color magenta
%C Cyan oclor cyan
%W White color white

Usage example


Custom log file

by default the logger will output into ./logs.txt (filename="logs.txt", directory="./") you can change it by providing a filename and directory in the constructor e.g

kq::logger logger("myfile.txt", "myDirectory/myOutputDirectory/");

Make sure to include any / in your directory


Declare a logger with your own custom enum

enum class myTypes { /* your types here */ };
kq::logger<myTypes> loggers;

if no template argument is provided, kq::default_symbols is used as a default, containing: (macros)

  1. KQDEBUG
  2. KQINFO
  3. KQWARNING
  4. KQERROR
  5. KQCRITICAL

How to log with your logger

call .out() with an enum type and a format string with all it's argument wrapped in {} e.g

logger.out( KQINFO, { "{} is a nice {}", "Today", "day" } );
output: `[2023-02-27 06:38:11] [INFO] [main@07] Today is a nice day`

Custom pattern

by default the logger will have the following pattern:

[{%Y}-{%M}-{%D} {%H}:{%N}:{%S}] [{%T}] [{%F}@{%L}] {%V}

see Pattern Flags for reference you can change this pattern with .set_pattern() and provide a string containing the new pattern to reset to the default pattern call the function with no arguments


Rainbow

kq::logger Logger("logs.txt", "output/");
    for(int i = 0; i < 10; ++i)
    {
        Logger.set_pattern("{%R}[{%Y}-{%M}-{%D}{%y} {%H}:{%N}:{%S}] {%G}[{%T}] {%E}[{%F}@{%L}] {%V}\n");
        Logger.out(KQINFO, {"{%C} {0:5} {%g}START", i + 10});
        Logger.set_pattern("{%y}[{%Y}-{%M}-{%D}{%G} {%H}:{%N}:{%S}] {%C}[{%T}] [{%F}@{%L}] {%V}\n");
        Logger.out(KQDEBUG, {"{%g} {0:5} {%R}CONTINUE", i + 10});
        Logger.set_pattern("{%G}[{%Y}-{%M}-{%D}{%C} {%H}:{%N}:{%S}] {%E}[{%T}] [{%F}@{%L}] {%V}\n");
        Logger.out(KQCRITICAL, {"{%R} {0:5} {%E}END", i + 10});
    }

Rainbow Example


How to filter your console output

the .set_filter() function takes as parameters:
a vector containing values from the enum of choice
a bool which if true, will only print the values found in the vector, if false, will only print the values NOT found in the vector
(basically filter in/out)
example:

    kq::logger logger;
    // logger.set_filter(.....)
    logger.out( KQINFO,     { "Some info message\n" } );
    logger.out( KQDEBUG,    { "Some debug message\n" } );
    logger.out( KQCRITICAL, { "Some critical message\n" } );

with .set_filter({KQINFO}, true), we get the folloiwng console output:

[2023-03-01 20:32:43] [INFO] [main@08] Some info message

with .set_filter({KQINFO}, false), we get the following console output:

[2023-03-01 20:33:35] [DEBUG] [main@09] Some debug message
[2023-03-01 20:33:35] [CRITICAL] [main@10] Some critical message

the file output does not experience any changes


Backup

simply call .backup() it will create backup/ in the directory specified at construction
and will make a copy of the log file with the following pattern name:
{year}-{month}-{day}_{hour}:{min}:{sec}-{filename}


Other

this project was made by me and norctus
we took inspiration from other loggers like spdlog and plog
but we've added our on twist and took a more flexible and different approach on our logger

About

C++ Logging Library, cross-platform, easy to use, header only, features

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages