#include <syslog.h>
void log_msg(char *name, int priority, const char *format)
{
va_list vl;
openlog(name, 0, LOG_DAEMON);
va_start(vl, format);
vsyslog(log_class[priority], format, vl);
va_end(vl);
closelog();
}
I use this code to log all message when running my program in openwrt backfire 10.03. but I can't find any log under /var/log
.
it some thing wrong in my code ! knowing that I don't find the config file of syslog demons in /etc/syslog.conf
If you want to print to the syslog (/var/log/syslog
) you don't actually need to use openlog()
or closelog()
. You can see this in the documentation for openlog from the GNU C library:
You don't have to use openlog. If you call syslog without having called openlog, syslog just opens the connection implicitly and uses defaults for the information in ident and options
but you do need to pass the variable argument list (...
) matching the format, otherwise vsyslog()
won't have anything to print. Here's a working example:
#include <stdarg.h>
#include <syslog.h>
void log_msg(int priority, const char *format, ...)
{
va_list vl;
va_start(vl, format);
vsyslog(priority, format, vl);
va_end(vl);
}
int main(void)
{
log_msg(LOG_USER|LOG_DEBUG, "%d variable %s\n", 2, "arguments");
return 0;
}
Then you can see the message in the syslog:
~/sandbox$ cat /var/log/syslog
...
Feb 27 09:03:04 a.out: 2 variable arguments
I'm not sure what your priority is, but note the proper values are found in the man page for vsyslog()
. It's facility and the level OR'd together.
If you want to use openlog()
/closelog()
then you'll still find your messages in /var/log/syslog
, but then they'll be appended with your name you opened. For example:
openlog(name, LOG_CONS, LOG_DEBUG);
va_start(vl, format);
vsyslog(priority, format, vl);
va_end(vl);
closelog();
(This is assuming you still pass char * name
to the log_msg function. You'd call it as such:
log_msg("hello", LOG_USER|LOG_DEBUG, "%d variable %s\n", 2, "arguments");
Then in the syslog
you'd now see:
Feb 27 09:35:16 hello: 2 variable arguments
Note that the generic program name a.out
has now changed to the name you passed to openlog()
("hello"
)