swasun / LibErrorInterceptor

Portable library in C99 to handle errors, format errors in stacktrace and do logging

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Description

A lightweight and cross-plateform library to handle stacktrace and logging in C99.

Examples

All examples are in examples directory.

Stacktrace

./bin/<debug|release>/examples/stacktrace_example
#include <ei/ei.h>

#include <stdlib.h>

bool foo(void *parameter) {
    if (!parameter) {
        ei_stacktrace_push_msg("Specified parameter is invalid");
        return false;
    }

    return true;
}

bool bar(void *parameter) {
    if (!foo(parameter)) {
        ei_stacktrace_push_msg("foo failed :(");
        return false;
    }

    return true;
}

/**
 * output:
 * bar failed ! (thread 140371337471744)
 * Caused by: Specified parameter is invalid
 *    at foo (stacktrace_example.c:7)
 *    at bar (stacktrace_example.c:16)
 *    at main (stacktrace_example.c:31)
 */
int main() {
    void *arg;

    arg = NULL;

    ei_init();

    if (!bar(arg)) {
        ei_stacktrace_push_msg("bar failed !");
    }

    ei_stacktrace_print();

    ei_uninit();

    return EXIT_SUCCESS;
}

Logger

./bin/<debug|release>/examples/logger_example
#include <ei/ei.h>

#include <stdlib.h>

int main() {
    ei_init();

    ei_logger_trace("Loading library...");

    ei_logger_debug("Variable value is %d", 58);

    ei_logger_info("User %s is now connected", "username");

    ei_logger_warn("Loading time is consequently longer");

    ei_logger_error("Invalid password");

    ei_logger_fatal("Out of memory");

    ei_uninit();

    return EXIT_SUCCESS;
}

Output:

logger example

Build

Linux

Debug mode

./build_debug.sh

Release mode

./build_release.sh

Clean

./clean.sh

Install

./build_release.sh && sudo ./install.sh

Windows

Debug mode

build_debug.bat

Release mode

build_release.bat

Clean

clean.bat

Install

build_release.bat && install.bat

Cross-plateform

Tested on:

  • Windows x86
  • Windows 64
  • Ubuntu 14.04
  • Ubuntu 16.04

About

Portable library in C99 to handle errors, format errors in stacktrace and do logging

License:Apache License 2.0


Languages

Language:C 80.3%Language:C++ 8.0%Language:CMake 5.8%Language:Batchfile 5.2%Language:Shell 0.7%