An open API service indexing awesome lists of open source software.

https://github.com/prashantrahul141/nhlog

An extremely simple logging library for C/C++ with colors.
https://github.com/prashantrahul141/nhlog

c cpp logging

Last synced: about 1 month ago
JSON representation

An extremely simple logging library for C/C++ with colors.

Awesome Lists containing this project

README

          

# nhlog

An extremely simple logging library for C/C++ with colors.

![demo](./meta/demo.png)

# Installation

Just include the two files `nhlog.h` and `nhlog.c` with your project files and make sure to compile and link them with your project and it just works.

# Usage

```c
/* all logging can be disabled completely at compile time by defining NHLOG_DISABLE, see nhlog.h */
// #define NHLOG_DISABLE

#include "nhlog.h"

int main() {
/* Zero initialisation needed */
TRACE("least important messages");
DEBUG("debug messages");
INFO("general info message");
WARN("warnings");
ERROR("errors");
FATAL("unrecoverable crashes");

/* Logging level can be changed at runtime using: */
nhlog_set_level(NHLOG_INFO);
TRACE("this will be NOT be logged");
WARN("however this will be logged");

/* All logging can be disabled by setting log level to NHLOG_OFF */
nhlog_set_level(NHLOG_OFF);
TRACE("this will be NOT be logged");

nhlog_set_level(NHLOG_TRACE);

/* you can log to any stream by passing it to nhlog_set_outstream */
FILE *fd = fopen("logs.txt", "w");
nhlog_set_outstream(fd);

/* you may also want to disable color output for files*/
nhlog_set_output_colors(false);

TRACE("these messages will now be logged to the file.");
DEBUG("debug message");
fclose(fd);

/* setting to null defaults to stderr */
nhlog_set_output_colors(true);
nhlog_set_outstream(NULL);

/* by default output stream if flushed after every log event, but it can be
* disabled */
nhlog_set_immediate(false);

TRACE("These messages will be now buffered by the c runtime");

return 0;
}
```

# Why?

Because every other C/C++ logging library is either

- overly complicated for small projects
- doesn't support both c and c++
- doesn't have colors
- doesn't have simple api