de http://stackoverflow.com/questions/6508461/logging-library-for-c
#ifndef LOG_H
#define LOG_H
void log(const char* tag, const char* message);
#endif /* LOG_H */
#include "log.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void log(const char* tag, const char* message) {
time_t now;
time(&now);
printf("%s [%s]: %s\n", ctime(&now), tag, message);
}
man syslog
#include <syslog.h>
void openlog(const char *ident, int option, int facility);
void syslog(int priority, const char *format, ...);
void closelog(void);
Ejemplo sencillo de uso: hello_syslog.c
#include <stdio.h>
#include <unistd.h>
#include <syslog.h>
int main(void) {
openlog("slog", LOG_PID|LOG_CONS, LOG_USER);
syslog(LOG_INFO, "A different kind of Hello world ... ");
closelog();
return 0;
}
To log into a different file you'll need to specify the file in the syslog configuration.
In my folder /etc/rsyslog.d there are two files:
20-ufw.conf and 50-default.conf
I added a file:
sudo nano /etc/rsyslog.d/30-mycustomname.conf
# Log QSD Centro generated log messages to file
if $programname == 'centro' then /var/log/centro.log
# Uncomment the following to stop logging anything that matches the last rule.
& ~
Then I check that the file /var/log/centro.log does not exist
sudo rm -f /var/log/centro.log
Then I restart the service
sudo service rsyslog restart