Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am currently working on implementing my own UNIX shell in C. The basis of the shell is working, you could find the source code here.

Right now my next task is to implement a logging feature for the shell. So here's my question:

What are the UNIX/Linux best practices to do that? The simple way would be to open a file and write to it every time. But I was wondering if there is a function that would take care of that for me, helping me produce ... "cleaner"... code.

Thank you in advance for your help, I'm quite new to programming, but I want to do things right.

PS: If you have any general remarks about the rest of the code, they'd be greatly appreciated :-)

share|improve this question
Logging as in the script command common in many shells or writing unusual conditions into /var/log, or what? – dmckee Jan 23 '11 at 14:38
More like having a log of all the command entered in /var/log. I thought I was clear. Sorry. – rahmu Jan 23 '11 at 15:11
Then syslog is all you need. – gelraen Jan 23 '11 at 19:19
I liked your code. Clean, simple and well documented! (+1) :) – Rizo Jan 24 '11 at 14:04
up vote 4 down vote accepted

Typically command history is saved to a hidden file in the user's home directory. Other than that, you can log to stderr or syslog.

share|improve this answer

Common practice for logging is to use syslog. It's quite simple and does not require much code. If you need something else - look at logging libraries like liblogging or log4c

share|improve this answer
Thank you for your answer. I'm going with syslog, since I don't need anything extravagant and don't want to include any extra dependency. Can you tell me if syslog() is available on other Unices like BSD or Solaris? – rahmu Jan 23 '11 at 23:42
It's available on FreeBSD from libc.so and I'm 95% sure that Solaris has syslog() too. – gelraen Jan 24 '11 at 10:51
I checked yesterday at work, it is indeed available on Solaris 10. Thanks! – rahmu Jan 25 '11 at 7:22

Your Answer


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.