This is the fourth article introducing the core component of OpenWrt, libubox. Here, we will discuss the ulog module, which is widely used in the OpenWrt software ecosystem. Almost all core modules in the system utilize it to save logs, so let’s get started.The ulog module is a wrapper for the syslog programming interface. If you are not familiar with syslog, it is recommended to research it online, and I also recommend checking out this article:https://blog.csdn.net/allway2/article/details/120047890The syslog operation interfaces provided by ulog can be found in ulog.h, and the interfaces are as follows:
#include <syslog.h>
enum { ULOG_KMSG = (1 << 0), ULOG_SYSLOG = (1 << 1), ULOG_STDIO = (1 << 2)};
void ulog_open(int channels, int facility, const char *ident);
void ulog_close(void);
void ulog_threshold(int threshold);
void ulog(int priority, const char *fmt, ...);
#define ULOG_INFO(fmt, ...) ulog(LOG_INFO, fmt, ## __VA_ARGS__)
#define ULOG_NOTE(fmt, ...) ulog(LOG_NOTICE, fmt, ## __VA_ARGS__)
#define ULOG_WARN(fmt, ...) ulog(LOG_WARNING, fmt, ## __VA_ARGS__)
#define ULOG_ERR(fmt, ...) ulog(LOG_ERR, fmt, ## __VA_ARGS__)
The usage is to first open a log output file, then set the threshold for the log output level, and then you can freely output logs. If you no longer need to output logs, you can close the file at the end. The usage is relatively simple, and here is an example demonstrating two usages:
#include <stdint.h> // uint32_t
#include <stdio.h>
#include <stdlib.h> // free()
#include <string.h>
#include <libubox/ulog.h>
/*Priority Level Description
LOG_EMERG An emergency situation
LOG_ALERT High-priority problem, such as database corruption
LOG_CRIT Critical error, such as hardware failure
LOG_ERR Errors
LOG_WARNING Warning
LOG_NOTICE Special conditions requiring attention
LOG_INFO Informational messages
LOG_DEBUG Debug messages */
#define PRINT_INFO(_text) do {\
ulog_open(ULOG_STDIO, LOG_USER, NULL);\
ulog_threshold(LOG_INFO);\
ULOG_INFO(_text);\
ulog_close();\
} while(0)
void log_test() { ulog_open(ULOG_STDIO, LOG_USER, NULL); ulog_threshold(LOG_INFO);
ULOG_INFO("info : A\n"); ULOG_NOTE("notice: B\n"); ULOG_WARN("warn : C\n"); ULOG_ERR("err : D\n"); ulog_close();}
int main(int argc, char** argv) { log_test();PRINT_INFO("hello world!\n");
return 0;}
This program demonstrates the usage of the<span><span>ulog</span></span> basic API, while providing a<span><span>PRINT_INFO</span></span> macro definition that encapsulates a set of operations. The output of this program is as follows:
root@ruok:/tmp# demo-libubox
info : A
notice: B
warn : C
err : D
hello world!
The series of articles on the core component of OpenWrt libubox includes:OpenWrt Core Component: libubox (1) – Project IntroductionOpenWrt Core Component: libubox (2) – utilsOpenWrt Core Component: libubox (3) – md5