ITEM: H8696L

Using syslogd facilities in a C program


Question:

I am using a RISC 550 running 3.2.5.  I am trying to use the syslog 
facility to log error messages into a separate log.  I have created a 
syslog file in my /etc directory and wrote a C program which opens log, 
sets log mask, performs syslog, and closes the log.  When I try this 
it does not write anything to log but does create the log.  All I seem
to receive is random return codes but no error messages to the log.  I
would like assistance in getting the syslog facility to work.  Here is
a short excerpt of my program:

  i = openlog("mysyslog", LOG_CONS | LOG_PID, LOG_USER);
  i = setlogmask(LOG_UPTO(LOG_ALERT));
  i = syslog(LOG_DEBUG, "DEBUG: Failed, iserrno = %i\\n", 111);
  i = closelog();

My /etc/syslog.conf file contains the following:

  user.debug              /tmp/mysyslog.log

Response:

Your code looks great except for the setlogmask() subroutine.  The 
LOG_UPTO function is kind of confusing as it works just the opposite of 
what you would might think.  Using LOG_UPTO(LOG_ALERT) as you have setup 
does not allow LOG_DEBUG messages to be logged to the file.  You should 
use a setlogmask() statement similar to the following:

  i = setlogmask(LOG_UPTO(LOG_DEBUG);

This should help correct the problems you are experiencing.

Here is an sample to help demonstrate the way this should work.  (Please
note that the following code is a sample only...)  

In the first run of slog (the name of my executable) I used LOG_UPTO
(LOG_ALERT).  Notice that only the LOG_ALERT message is logged to the
logfile.  In the second run I used LOG_UPTO(LOG_DEBUG).  Notice that in
this run all messages are logged.

Script command is started on Mon Mar 28 20:01:40 CST 1994.
watsone: /u/liz/samples/clang/syslog [408]> cat slog.c
\#include\
main()
{  int i;
  i = openlog("slog", LOG_CONS | LOG_PID, LOG_USER);
  printf("i=%i\\n");
  perror("openlog");

  i = setlogmask(LOG_UPTO(LOG_ALERT));
  printf("i=%i\\n");
  perror("setlogmask");

  i=syslog(LOG_DEBUG, "DEBUG: Failed, iserrno = %i\\n", 111);
  i=syslog(LOG_INFO, "INFO: Failed, iserrno = %i\\n", 111);
  i=syslog(LOG_NOTICE, "NOTICE: Failed, iserrno = %i\\n", 111);
  i=syslog(LOG_WARNING, "WARNING: Failed, iserrno = %i\\n",111);
  i=syslog(LOG_ERR, "ERR: Failed, iserrno = %i\\n", 111);
  i=syslog(LOG_CRIT, "CRIT: Failed, iserrno = %i\\n", 111);
  i=syslog(LOG_ALERT, "ALERT: Failed, iserrno = %i\\n", 111);

  printf("i=%i\\n");
  perror("syslog");

  i = closelog();
  printf("i=%i\\n");
  perror("closelog");
}watsone: /u/liz/samples/clang/syslog [409]> make slog
        cc -O  slog.c -o slog
watsone: /u/liz/samples/clang/syslog [410]> tail -1 /etc/syslog.conf
user.debug              /tmp/constat.log
watsone: /u/liz/samples/clang/syslog [411]> touch /tmp/constat.log
watsone: /u/liz/samples/clang/syslog [412]> refresh -s syslogd
0513-095 The request for subsystem refresh was completed successfully.
watsone: /u/liz/samples/clang/syslog [413]> slog
i=536942456
openlog: Error 0
i=536942376
setlogmask: Error 0
i=62
syslog: Error 0
i=62
closelog: Error 0
watsone: /u/liz/samples/clang/syslog [414]> cat /tmp/constat.log
Mar 28 20:02:22 watsone slog[18120]: ALERT: Failed, iserrno = 111 
watsone: /u/liz/samples/clang/syslog [415]> sed \\
"s/(LOG_ALERT)/(LOG_DEBUG)/" slog.c > slog.temp; mv slog.temp slog.c
watsone: /u/liz/samples/clang/syslog [416]> make slog
        cc -O  slog.c -o slog
watsone: /u/liz/samples/clang/syslog [417]> slog
i=536942456
openlog: Error 0
i=536942376
setlogmask: Error 0
i=62
syslog: Error 0
i=62
closelog: Error 0
watsone: /u/liz/samples/clang/syslog [418]> r cat
cat /tmp/constat.log
Mar 28 20:02:22 watsone slog[18120]: ALERT: Failed, iserrno = 111 
Mar 28 20:03:44 watsone slog[17955]: DEBUG: Failed, iserrno = 111 
Mar 28 20:03:44 watsone slog[17955]: INFO: Failed, iserrno = 111 
Mar 28 20:03:44 watsone slog[17955]: NOTICE: Failed, iserrno = 111 
Mar 28 20:03:44 watsone slog[17955]: WARNING: Failed, iserrno = 111 
Mar 28 20:03:44 watsone slog[17955]: ERR: Failed, iserrno = 111 
Mar 28 20:03:44 watsone slog[17955]: CRIT: Failed, iserrno = 111 
Mar 28 20:03:44 watsone slog[17955]: ALERT: Failed, iserrno = 111 
watsone: /u/liz/samples/clang/syslog [419]> \^D

Script command is complete on Mon Mar 28 20:07:48 CST 1994.


Support Line: Using syslogd facilities in a C program ITEM: H8696L
Dated: April 1994 Category: N/A
This HTML file was generated 99/06/24~13:30:46
Comments or suggestions? Contact us