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 :-)

closed as primarily opinion-based by Michael Homer, cas, garethTheRed, Jakuje, Scott May 1 '16 at 8:46

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the 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

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.


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

  • 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

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