Syslog command in C code

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

The above syslog command is not logged in syslog. So I tried

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

This prevents me from registering anything, and I get the following error:

 syslog: unknown facility/priority: 8049584 
+8
source share
3 answers

This line is incorrect:

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

"" must be an integer:

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

An integer must be any of these ORed constants 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:

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

Note that your syslogd configuration cannot be configured to save log level messages LOG_INFO . Try LOG_ALERT or something to debug this problem - if it works, go back to LOG_INFO and configure syslogd to save the log messages you want to keep. Adding a line, for example:

 *.* /var/log/all_messages.log 

will complete the task for rsyslogd(8) . Be sure to read the rsyslog.conf(5) man page if your system uses rsyslogd(8) . If your system uses a different syslog daemon, then check your system manpages.

+16
source

You really have to compile all the allowed and debugged warnings, i.e. gcc -Wall -g .

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

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

therefore, we pass "" because the second argument is incorrect (and the compiler warned you about this). This should be, for example, LOG_PERROR|LOG_PID or some other flags.

+7
source
 openlog("vyatta-conntrack", "", LOG_USER); 

This string is incorrect as the transmitted string as the second parameter. please check the manual page, the 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(); } 

exit:

 May 20 16:36:19 prabha-VirtualBox Logs[8114]: Start logging 
+1
source

Source: https://habr.com/ru/post/903655/


All Articles