1 year ago
#307404
Michael
C syslog Wrapper
I am trying to use syslog
and a custom syslog.conf
to create a logger for multiple modules. I have created a logging struct that allows a module to create a logger with custom settings
struct module_logger {
char* logger_name;
int32_t logger_options;
int32_t logger_facility_code;
int32_t log_level;
int32_t log_mask;
};
When a module initialized the logger they struct values must be passed in. I have two functions logger_log
and logger_log_at_level
void logger_log(struct module_logger* logger, char* message) {
(void)setlogmask(logger->log_mask);
openlog(logger->logger_name, logger->logger_options, logger->logger_facility_code);
syslog(logger->log_level, "%s", message);
closelog();
}
void logger_log_at_level(struct module_logger* logger, char* message, int32_t log_level) {
(void)setlogmask(log_level);
openlog(logger->logger_name, logger->logger_options, logger->logger_facility_code);
syslog(log_level, "%s", message);
closelog();
}
My syslog.config is very simple, take each log and put them into their own file
# /etc/syslog.conf Configuration file for busybox's syslogd utility
user.emerg /var/log/emerg
user.alert /var/log/alert
user.crit /var/log/crit
user.err /var/log/err
user.warning /var/log/warning
user.notice /var/log/notice
user.info /var/log/info
user.debug /var/log/debug
I wrote the following test code
struct module_logger logger_emerg;
struct module_logger logger_alert;
struct module_logger logger_crit;
struct module_logger logger_err;
struct module_logger logger_warning;
struct module_logger logger_notice;
struct module_logger logger_info;
struct module_logger logger_debug;
int main(void) {
logger_init(&logger_emerg, "logger emerg", LOGGER_DEFAULT_OPTIONS, LOG_USER, LOG_EMERG, LOG_EMERG);
logger_init(&logger_alert, "logger alert", LOGGER_DEFAULT_OPTIONS, LOG_USER, LOG_ALERT, LOG_ALERT);
logger_init(&logger_crit, "logger crit", LOGGER_DEFAULT_OPTIONS, LOG_USER, LOG_CRIT, LOG_CRIT);
logger_init(&logger_err, "logger err", LOGGER_DEFAULT_OPTIONS, LOG_USER, LOG_ERR, LOG_ERR);
logger_init(&logger_warning, "logger warning", LOGGER_DEFAULT_OPTIONS, LOG_USER, LOG_WARNING, LOG_WARNING);
logger_init(&logger_notice, "logger notice", LOGGER_DEFAULT_OPTIONS, LOG_USER, LOG_NOTICE, LOG_NOTICE);
logger_init(&logger_info, "logger info", LOGGER_DEFAULT_OPTIONS, LOG_USER, LOG_INFO, LOG_INFO);
logger_init(&logger_debug, "logger debug", LOGGER_DEFAULT_OPTIONS, LOG_USER, LOG_DEBUG, LOG_DEBUG);
while(1) {
logger_log(&logger_emerg, "emerg message");
logger_log(&logger_alert, "alert message");
logger_log(&logger_crit, "crit message");
logger_log(&logger_err, "err message");
logger_log(&logger_warning, "warning message");
logger_log(&logger_notice, "notice message");
logger_log(&logger_info, "info message");
logger_log(&logger_debug, "debug message");
logger_log_at_level(&logger_debug, "debug at emerg", LOG_EMERG);
sleep(5);
}
}
When I view my /var/log
folder there are 8 log files, one for each level. But there is an issue. All the files have the same contents. They only contain the messages from
Mar 24 10:25:27 imx6ull14x14evk user.emerg logger emerg[164]: emerg message
Mar 24 10:25:27 imx6ull14x14evk user.emerg logger debug[164]: debug at emerg
My other messages are seemingly lost. I believe the error is in my logging functions but I am not sure what I am missing.
Edit: Logger init as requested
void logger_init(struct module_logger* logger, char* name, int32_t options, int32_t facility_code, int32_t log_level, int32_t log_mask) {
logger->logger_name = name;
logger->logger_options = options;
logger->logger_facility_code = facility_code;
logger->log_level = log_level;
logger->log_mask = log_mask;
}
Edit 2: The error is definitely in my log function. Printing out the last log mask I see
last level 255
logger emerg[278]: emerg message
last level 255
last level 1
last level 2
last level 3
last level 4
last level 5
last level 6
last level 7
Edit 3: If I change the log mask to setlogmask(LOG_UPTO(logger->log_mask));
Then I get better results. In the emerg
log only emergency logs appear. In my debug log I get all messages. I want all 8 files to only get their respective messages.
c
syslog
busybox
0 Answers
Your Answer