2011-12-13 63 views
6
#include<syslog.h> 
syslog(LOG_INFO, "Start logging"); 

上述syslog命令未記錄在syslog中。所以,我想,C代碼中的syslog命令

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

這無法登錄任何東西,我得到以下錯誤:

syslog: unknown facility/priority: 8049584 
+1

請參閱http://www.linuxselfhelp.com/gnu/glibc/html_chapter/libc_18.html –

回答

7

你真的應該啓用所有警告和調試,即gcc -Wall -g編譯。

再次閱讀openlog man page。它被聲明爲:

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

所以通過""作爲第二個說法是錯誤的(並且編譯器沒有警告你)。它應該是例如LOG_PERROR|LOG_PID或其他一些標誌。

14

此行是錯誤的:

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

的 「」 應該已經整數:

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

的整數應該是任何這些常量OR連起來的:

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. 

請再試一次更類似於:

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

請注意,您的syslogd配置可能未設置保留日誌級別LOG_INFO的消息。嘗試使用LOG_ALERT或其他方法來調試此問題 - 如果它有效,請退回到LOG_INFO並配置您的syslogd以保留要保留的日誌消息。添加一條線如:

*.* /var/log/all_messages.log 

將完成​​的工作。如果您的系統使用​​,請務必閱讀rsyslog.conf(5)聯機幫助頁。如果您的系統使用不同的syslog守護進程,那麼請檢查系統的聯機幫助頁以獲取詳細信息。