Skip to content

ZhangzrJerry/log.hpp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

log.hpp

A minimal, thread-safe, header-only logging library in modern C++, modified from rxi/log.c.

Basic Usage

Include the Header

#include "log.hpp"

Basic Logging

log_trace("This is a TRACE message");
log_debug("Debug info: %d", value);
log_info("Application started");
log_warn("Something might be wrong...");
log_error("An error occurred: %s", error_msg);
log_fatal("Critical failure, exiting!");

Set Log Level

log_set_level(LOG_WARN); // Only WARN, ERROR, FATAL logs are printed

Enable/Disable Output

log_set_quiet(true);  // Disable output to stderr
log_set_quiet(false); // Re-enable

Add File Output

FILE* fp = fopen("app.log", "w");
log_add_fp(fp, LOG_DEBUG);

Custom Callback

void my_callback(log_Event* ev) {
    // Handle log event manually
    std::cerr << "[Custom] " << ev->fmt << std::endl;
}

log_add_callback(my_callback, nullptr, LOG_WARN);

Thread Safety

std::mutex mtx;

log_set_lock([](bool lock, void* udata) {
    std::mutex* m = static_cast<std::mutex*>(udata);
    if (lock)
        m->lock();
    else
        m->unlock();
}, &mtx);

LICENSE

This project is licensed under the MIT License. Ported and extended from rxi/log.c, which is also MIT Licensed.

About

A Simple, Header-Only C++ Logging Library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published