Giter Site home page Giter Site logo

log.c's Introduction

log.c

A simple logging library implemented in C99

screenshot

Usage

log.c and log.h should be dropped into an existing project and compiled along with it. The library provides 6 function-like macros for logging:

log_trace(const char *fmt, ...);
log_debug(const char *fmt, ...);
log_info(const char *fmt, ...);
log_warn(const char *fmt, ...);
log_error(const char *fmt, ...);
log_fatal(const char *fmt, ...);

Each function takes a printf format string followed by additional arguments:

log_trace("Hello %s", "world")

Resulting in a line with the given format printed to stderr:

20:18:26 TRACE src/main.c:11: Hello world

log_set_quiet(bool enable)

Quiet-mode can be enabled by passing true to the log_set_quiet() function. While this mode is enabled the library will not output anything to stderr, but will continue to write to files and callbacks if any are set.

log_set_level(int level)

The current logging level can be set by using the log_set_level() function. All logs below the given level will not be written to stderr. By default the level is LOG_TRACE, such that nothing is ignored.

log_add_fp(FILE *fp, int level)

One or more file pointers where the log will be written can be provided to the library by using the log_add_fp() function. The data written to the file output is of the following format:

2047-03-11 20:18:26 TRACE src/main.c:11: Hello world

Any messages below the given level are ignored. If the library failed to add a file pointer a value less-than-zero is returned.

log_add_callback(log_LogFn fn, void *udata, int level)

One or more callback functions which are called with the log data can be provided to the library by using the log_add_callback() function. A callback function is passed a log_Event structure containing the line number, filename, fmt string, va printf va_list, level and the given udata.

log_set_lock(log_LockFn fn, void *udata)

If the log will be written to from multiple threads a lock function can be set. The function is passed the boolean true if the lock should be acquired or false if the lock should be released and the given udata value.

const char* log_level_string(int level)

Returns the name of the given log level as a string.

LOG_USE_COLOR

If the library is compiled with -DLOG_USE_COLOR ANSI color escape codes will be used when printing.

License

This library is free software; you can redistribute it and/or modify it under the terms of the MIT license. See LICENSE for details.

log.c's People

Contributors

rxi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

log.c's Issues

Maximum log file size reached

Hello,
I wanted to ask what happens when log file reaches maximum size of fatfs (2^32 - 1).
FatFS just stops writing into file and in the library I cannot find if it's possible to set maximum size of the log file, reaching which it would start overwriting from the begging of the file
or this functionality is not foreseen or needs yet to be implemented?
thank you

Logs to syslog()

Is there any way to send the longs through syslog for background services?

Log level enumaration

Hi,

Maybe you consider changing log level names inside enum in log.h (LOG_INFO, LOG_ERROR etc.). Some of the decleration also used in linux syslog utility and that leads confisuon. For example, LOG_INFO also defined in syslog and has value 6 despite your utility's LOG_INFO has number 2.

Best regards,
Said.

Lock before changing level

Should the lock be locked before changing level, for the case where the log level is configurable during runtime?

How to redirect messages to text file?

I want to write logging messages to a text file, here is what i have done.

  FILE *fp_test = fopen("logging.txt","w");
  log_add_fp(fp_test,"INFO");
  log_info("%s is good man","rxi");
  fclose(fp_test);

But i got an empty file logging.txt.
How to achieve this?

Convert internal lock-management to pthread

You can't use an internal locking-unlocking system, because you should disable interruptions during lock or unlock execution (it's not possible with CPU in user-mode). So you can include pthread and use a mutex. Let me know for a pull-request.

More Explanation/ Documentation

I'm trying to implement it to my project but I'm facing difficulties due to the improper documentation and the functions that are not explained in it.

Copyright year

Apparently you changed the copyright year from 2017 to 2020 but the year of first publication should be set out. It is a common misconception that the current year should always be given.

The general rule is that the year to include in a copyright notice is the year of first publication of the work. First publication is when the work is made available to the public without restriction. This includes selling a book or leaving free copies of it in a public place or posting content on your blog. Publication does not include, for example, circulating copies internally to co-workers or sending copies of a book to a publisher.

For compilations or derivative works which incorporate previously published content, the year of first publication of the compilation or derivative work is sufficient.

Source: Which Year Should You Use in Your Copyright Notice?

Best regards, Michael

NULL safety

log_trace("%s", NULL);
output is (null) but according to standard C library usage states NULL argument behaivour is undefined. So passing NULL to functions like printf() could produce Segmentation Fault.
It would be nice to be able to log NULLs without worries.

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.