Share a lightweight log library suitable for microcontrollers

Follow the + star public account and never miss exciting content

fccc6bceba99dc4ae4165bc6d4c60de1.gif

Source | Github

Arrangement | strongerHuang

Single-chip microcomputer projects often need to be debugged, and diagnosing bugs through logs is a common debugging method.

Next, I will share with you an open source lightweight log library suitable for single-chip microcomputers. There are only two files, log.c and log.h, which are very convenient to use.

2f9e93c031f53f8512b8fa048594e9e7.png

usage

The use of this log library is relatively simple, and novices are completely competent.

1 Overview

Add log.c and log.h files to your project.

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 parameters:

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

Generates a line of the given format to print to stderr:

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

2. Interface description

log_set_quiet(bool enable)

Quiet mode (no logging) can be enabled by passing to the function.

When this mode is enabled, the library will not output anything to the file, but will continue to write to the file and callback if set.truelog_set_quiet()stderr

log_set_level(int level)

You can use this function to set the current logging level. All logs below the given level will not be written. The default level is LOG_TRACE, which means nothing is ignored.log_set_level()stderrLOG_TRACE

log_add_fp(FILE *fp, int level)

One or more file pointers to which the log will be written can be provided to the library by using the function. Data output written to a file is in the following format:log_add_fp()

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

Any messages below the given value will be ignored. A value less than zero is returned if the library cannot add the file pointer.

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

One or more callback functions that are called with log data can be provided to the library by using functions. The callback function is passed a list containing numbers, strings, printf va_list and the given .log_add_callback()log_Eventlinefilenamefmtvaleveludata

log_set_lock(log_LockFn fn, void *udata)

If the log will be written from multiple threads, a locking function can be set. The function is passed a Boolean value if the lock should be acquired, or if the lock should be released and given a value.truefalseudata

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 escape codes will be used when printing.

Open source agreement

This library is a free software library, but you need the corresponding MIT license terms.

Open source address:

https://github.com/rxi/log.c

------------ END ------------

8e18ebd6e10b434b86ec4fbf8e3aced7.gif

●Column "Embedded Tools "

●Column "Embedded Development"

●Column "Keil Tutorial"

●Embedded column selected tutorials

Follow the official account and reply " Add Group " to join the technical exchange group according to the rules, and reply " 1024 " to view more content.

f3e8e1157fe9762391abc13db29a7882.jpeg

dc47b4dd0953b77e9be3fb859fc67fee.png

Click " Read the original text " to view more sharing.

Guess you like

Origin blog.csdn.net/ybhuangfugui/article/details/132726354