Syslog in C

Hey, I'm pretty new to C programming in a Linux environment and was wondering if I could get some help using log files. What I want to do is have my program write data to a log file (like /var/log/motord or something like that where a new file is being created) but I'm having a surprising amount of trouble figuring out how to go about this. Could someone perhaps post some example code that would do this sort of thing?

Also, is there a way to write a program that would, while it's running, print the lines that are being added to the log fill to the command prompt? Only while the program is running though and only the lines that are actively being sent, not the entire file. This is more of an after thought but I'm not really sure where to start with that either or if it's something that's even doable.

Just as a reference as well, these programs will be running on a remote system and I plan on accessing the command prompt via telnet. This really only affects the latter situation but I figured it would be good to specify.

Any help would be great. Thanks in advance :slight_smile:

/etc/syslog.conf determines what gets written to syslog. Try man syslog.conf

I am guessing you do not want syslog() at all. This is because you want to write to another file. Do you want messages also to appear on the console?

I would like messages to appear on the console only when I am looking for them which is why I mentioned the possible program in the second paragraph of my initial post. And I did start to get the impression that syslog() wasn't going to work. I just liked the idea that it would add all of the tags to the beginning of each line, most notably the time. Would an fprintf() or something like that make more sense though?

If this is a long-lived process that you don't want to have tied to its log file, you do need to use something like syslog.

By having a direct open file descriptor on your log file, your process needs to handle log roll over directly, the log file can not be deleted or moved to another file system without your process having code to handle that. That code already exists in syslog, so you're just reinventing the wheel.

And don't ever use redirected stdout and/or stderr for log files for long-lived daemon processes. What are you going to do when this:

MyProcessd > /my/log/file.log &

fills up a disk?

About the only thing you CAN do in that situation is kill your daemon.