Giter Club home page Giter Club logo

clog's Introduction

clog

Log macros and output management in C

Building

clog is a header only library with an implementation define.

To use it, simply copy clog.h to your project, or add it to your include path.

Then include it in a single source file like so:

#define CLOG_IMPLEMENTATION
#include <clog.h>

You may also install it using cmake, like so:

cmake path/to/source
sudo make install

This will install both CMake and pkg-config configuration files.

Usage

You can either use the default log macros like so:

#define CLOG_USE_DEFAULTS
#define CLOG_IMPLEMENTATION
#include <clog.h>

void callback_func(clog_color_t color, const char * message, void * userData) {
    // Do whatever
}

int main(int argc, char** argv)
{
    clog_init();

    clog_add_file("allruns.log", true);
    clog_add_file("lastrun.log", false);
    
    clog_add_callback(callback_func, NULL);

    CLogVerb("This will be verbose");
    CLogInfo("This is informational");
    CLogWarn("This is a warning");
    CLogError("This is an error");

    // CLogFatal("This is a fatal error, it will exit(1)");

    clog_term();

    return 0;
}

Or you can define your own log macros like so:

#define CLOG_IMPLEMENTATION
#include <clog.h>

#define LogLevel1(M, ...)                                                 \
    do {                                                                  \
        clog_log(CLOG_COLOR_RED, "[LVL1](%s:%d) " M "\n",                 \
            CLOG_FILENAME, __LINE__, ##__VA_ARGS__);                      \
    } while (0)

#define LogLevel2(M, ...)                                                 \
    do {                                                                  \
        clog_log(CLOG_COLOR_GREEN, "[LVL2](%s:%d) " M "\n",               \
            CLOG_FILENAME, __LINE__, ##__VA_ARGS__);                      \
    } while (0)

#define LogLevel3(M, ...)                                                 \
    do {                                                                  \
        clog_log(CLOG_COLOR_BLUE, "[LVL3](%s:%d) " M "\n",                \
            CLOG_FILENAME, __LINE__, ##__VA_ARGS__);                      \
    } while (0)

int main(int argc, char** argv)
{
    clog_init();

    LogLevel1("This is level 1");
    LogLevel2("This is level 2");
    LogLevel3("This is level 3");

    clog_term();

    return 0;
}

Several helper macros exist for controlling when log statements are processed:

#define CLOG_USE_DEFAULTS
#define CLOG_IMPLEMENTATION
#include <clog.h>

int main(int argc, char** argv)
{
    clog_init();

    for (int i = 0; i < 100; ++i) {
        CLogOnce(CLogInfo("This will only print once"));
        CLogEvery(10, CLogInfo("This will only print ten times"));
        CLogWhen(i % 2 == 0, CLogInfo,"This will print fifty times"));
    }

    clog_term();

    return 0;
}

In order to reduce the length of __FILE__, you can define the base of your source path like so:

#define CLOG_SOURCE_PATH "/path/to/source/"

This is made easier using CMake:

TARGET_COMPILE_DEFINITIONS(
    MyTarget
    PRIVATE
        CLOG_SOURCE_PATH="${CMAKE_SOURCE_DIR}/"
)

There are three sane limits that can be overriden if needed:

#if !defined(CLOG_MAX_LOG_MESSAGE_LENGTH)
    #define CLOG_MAX_LOG_MESSAGE_LENGTH 200
#endif

#if !defined(CLOG_MAX_LOG_FILES)
    #define CLOG_MAX_LOG_FILES 10
#endif

#if !defined(CLOG_MAX_LOG_CALLBACKS)
    #define CLOG_MAX_LOG_CALLBACKS 10
#endif

clog's People

Contributors

whobrokethebuild avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.