Syslog and custom C app

https://en.wikipedia.org/wiki/Syslog

[code language=”cpp”]#include <syslog.h>

int main()
{
setlogmask(LOG_UPTO(LOG_NOTICE)); // Returns the previous log priority mask.
openlog("translator", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL7);
syslog(LOG_WARNING, "[thread_nr1] Program started by User %d", 44);
closelog();
return 0;
}
[/code]

Compile the program above (no special linking is needed). This program will make a syslog system call.
You should then configure your syslog daemon to handle these messages. For example, I use rsyslogd and I created: /etc/rsyslog.d/example_program.conf:

[code language=”cpp”]template(name="logline" type="string" string="%TIMESTAMP% %HOSTNAME% %syslogtag%%msg%\n")

# Filter by program identifier string
if ($programname == ‘my_program’) then
{
# :msg, regex, "\\[thread_nr1\\].*" /var/log/example_program.log
action( type="omfile" file="/var/log/example_program.log" template="logline" )
}[/code]

Then simply restart the daemon, tail on your log and run the program.

[code language=”bash”]/etc/init.d/rsyslog restart && tail -F /var/log/example_program.log[/code]