Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

syslog command in C code

Tags:

c

syslog

#include<syslog.h>
syslog(LOG_INFO, "Start logging");

The above syslog command is not logging in the syslog. So I tried,

openlog("Logs", "", LOG_USER);
syslog(LOG_INFO, "Start logging");
closelog();

This fails to log anything and I get the following error:

syslog: unknown facility/priority: 8049584
like image 390
user1060517 Avatar asked Dec 13 '11 06:12

user1060517


3 Answers

You really should compile with all warnings enabled and debugging, i.e. gcc -Wall -g.

Read again the openlog man page. It is declared as:

 void openlog(const char *ident, int option, int facility);

so passing "" as the second argument is wrong (and the compiler did warn you about that). It should be e.g. LOG_PERROR|LOG_PID or some other flags.

like image 130
Basile Starynkevitch Avatar answered Oct 20 '22 01:10

Basile Starynkevitch


This line is wrong:

openlog("vyatta-conntrack", "", LOG_USER);

The "" should have been an integer:

void openlog(const char *ident, int option, int facility);

The integer should be any of these constants ORed together:

   LOG_CONS       Write directly to system console if there is
                  an error while sending to system logger.

   LOG_NDELAY     Open the connection immediately (normally, the
                  connection is opened when the first message is
                  logged).

   LOG_NOWAIT     Don't wait for child processes that may have
                  been created while logging the message.  (The
                  GNU C library does not create a child process,
                  so this option has no effect on Linux.)

   LOG_ODELAY     The converse of LOG_NDELAY; opening of the
                  connection is delayed until syslog() is
                  called.  (This is the default, and need not be
                  specified.)

   LOG_PERROR     (Not in POSIX.1-2001.)  Print to stderr as
                  well.

   LOG_PID        Include PID with each message.

Try again with something more like:

openlog("vyatta-conntrack", LOG_PID, LOG_USER);

Note that your syslogd configuration might not be set up to keep messages of log level LOG_INFO. Try LOG_ALERT or something for debugging this problem -- if it works, then drop back to LOG_INFO and configure your syslogd to keep the log messages that you want to keep. Adding a line like:

*.* /var/log/all_messages.log

will do the job for rsyslogd(8). Be sure to read the rsyslog.conf(5) manpage if your system uses rsyslogd(8). If your system uses a different syslog daemon, then check your system's manpages for details.

like image 21
sarnold Avatar answered Oct 20 '22 00:10

sarnold


openlog("vyatta-conntrack", "", LOG_USER);

This line is wrong as your passing string as second parameter. please check man page, second parameter is of type int.

try this:

    #include <syslog.h>
      int main() {
      openlog("Logs", LOG_PID, LOG_USER);
      syslog(LOG_INFO, "Start logging");
      closelog();
    }

output:

May 20 16:36:19 prabha-VirtualBox Logs[8114]: Start logging
like image 3
Prabhaskar Avatar answered Oct 20 '22 01:10

Prabhaskar