Cartesius00
Cartesius00

Reputation: 24414

Logging for C program

I need some clever logging library for a daemon-like C (not C++) program on Linux. It needs to be open-source. It should support logging to files, maybe sockets etc.

Or at least some good tips or tricks or best practices.

Upvotes: 2

Views: 6654

Answers (2)

amatuerCprogrammer
amatuerCprogrammer

Reputation: 73

the following is something that you could do .

I am assuming your C application runs as a deamon and hence you cannot frintf the logging messages . syslog() is certainly an option for routing you messages . syslog() is very option the location where deamons write their messages and the adminstrators parse the contents of the syslog file to reach meaningful conclusions . Also syslog has a particual logging format . However i wish the propose that you write your own logging scheme . It is often what is desireable in enterprise software and not logging into syslog . Do this

1 . in you main configuration file define a logging macro

 #define LOG_ERR log_err 

having a definition like log_err(int errno,char *logger , char *text) the api should log details to a log file .

  1. any c file which includes the main configuration file can just use the macro

    LOG_ERR(errno,"handle_submission()","Submission of job failed ")

You should also include time stamps in the logger file .

Upvotes: 1

nos
nos

Reputation: 229342

Use syslog() . This decouples your in program logging from how the logs are handled. rsyslog is used on most Linux distributions nowadays, and allows great flexibility on how the logs are processed.

Upvotes: 7

Related Questions