I have created my own C library which my co-workers intend to use. In this wrapper, I intend to use syslog
and depending on an input parameter, I wish to switch between LOCAL0
and LOCAL1
.
The easiest way I found is to do openlog()
with LOCAL0
or LOCAL1
, depending on an input param, and then do syslog()
and `closelog().
I have all 3 in the same wrapper API (something simliar to below):
void syslog_wrap_api(int flag, const char *msg)
{
setlogmask(LOG_UPTO (LOG_INFO));
if(flag == 0)
openlog("myapplog",LOG_NDELAY,LOG_LOCAL0);
else
openlog("myapplog",LOG_NDELAY,LOG_LOCAL1);
syslog(LOG_INFO,"%s",msg);
closelog()
}
My question is, can this API cause problems under stress (performance issues)?
This is, in my opinion, the wrong way to go about it, but the documentation is confusing.
Whether you have problems under stress is going to be be determined by the implementation of openlog
in your C library. It might well be problematic. More to the point, in a multithreaded environment, one thread may issue the syslog
after another thread issues the openlog
, meaning the wrong priority is used.
The key to how to do this right is in the man page:
The facility argument establishes a default to be used if none is specified in subsequent calls to
syslog()
. Values for option and facility are given below ... Thepriority
argument is formed by OR-ing thefacility
and thelevel
values (explained below).
The clue as to how to exploit this is here:
The use of
openlog()
is optional; it will automatically be called bysyslog()
if necessary, in which caseident
will default toNULL
.
If you don't use openlog
, there must be some way of expressing the facility
. The answer is that you can specify the facility as zero to openlog
(or not use it at all), and OR in the facility to the priority
argument of syslog
.
So, something like:
syslog(LOG_INFO | (flag?LOG_LOCAL1:LOG_LOCAL0),"%s",msg);