Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'm having some trouble with <syslog.h> in 10.5. I'm writing a small fork based server for class and I want to use syslog() to print a bunch of debug info. However, I can't find any of the messages I issue via syslog() while using Console (I'm looking in both all messages & system.log).

Here is a code snippet of a plain C:

#include <syslog.h>
#include <stdarg.h>

int main (int argc, const char * argv[])
{
    openlog("Syslog Test", LOG_PID | LOG_NDELAY | LOG_CONS | LOG_PERROR, LOG_USER);
    setlogmask(LOG_UPTO(LOG_DEBUG));
    syslog(LOG_DEBUG, "%s", "Hello, World!");
    syslog(LOG_DEBUG, "%s", "And goodbye!");
    closelog();
    return 0;
}

And I don't see anything in "All Messages", "Console Messages" or system.log. Any advice?

share|improve this question
2  
Do you have syslog configured (/etc/syslog.conf) to accept debug entries? –  barryj May 5 '11 at 11:05
    
It accepts debug entries from the kernal. –  CyberSkull May 5 '11 at 23:03
    
Ok, so what I ended up doing for the class was just to use syslog to dump everything to the terminals I was running my server and clients from, and then save the results. I should have clarified that in the class, we were going strictly with POSIX APIs. –  CyberSkull Aug 24 '11 at 10:04
add comment

1 Answer

up vote 3 down vote accepted

Despite what you pass to setlogmask(), in its default configuration, OS X will only write messages to the system log that have a priority of LOG_NOTICE or higher.

Any of the following will solve your problem...

  1. Enable lower-priority logging in /etc/syslog.conf -- kern.* is not enough, as log messages from Xcode are not coming from the kernel
  2. Change the priority of your messages from LOG_DEBUG to LOG_NOTICE
  3. Simply use the built-in Xcode console -- the output will always appear there regardless of your syslog.conf settings

Here's what it looks like in the built-in Xcode 4 console:

Xcode console

Also, if you're developing exclusively for OS X, you might want to look into ASL (Apple System Log), which was meant to replace syslog().

share|improve this answer
    
I've heard of ASL, but I forgot to mention that for the purpose of the class I need to have my code be portable to Linux. –  CyberSkull May 6 '11 at 3:44
    
I've also been using LOG_CONS and LOG_PERROR to dump my syslogs to the terminal I am using (Xcode & Terminal). –  CyberSkull May 6 '11 at 3:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.