Difference between revisions of "UsingSyslog"
From ArmadeusWiki
SebastienR (Talk | contribs) (New page: ==Calling syslog method from your C source code== This part is quiet simple, but we need to correctly use each parameter. First step is to declare us as syslog client : <source lang="C"> ....) |
SebastienR (Talk | contribs) |
||
Line 21: | Line 21: | ||
... | ... | ||
</source> | </source> | ||
− | + | If you compile the source file: | |
<source lang="C"> | <source lang="C"> | ||
// log_syslog.c | // log_syslog.c | ||
Line 28: | Line 28: | ||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
{ | { | ||
− | + | openlog("program_name", LOG_PID, LOG_USER); | |
− | + | syslog(LOG_INFO, "%s called with %d arguments", argv[0], argc - 1); | |
− | + | closelog(); | |
− | + | return 0; | |
− | + | ||
} | } | ||
</source> | </source> | ||
Line 44: | Line 43: | ||
itsme@mycomputer:~/log_syslog$ ./log_syslog pim pam poum | itsme@mycomputer:~/log_syslog$ ./log_syslog pim pam poum | ||
</pre> | </pre> | ||
− | You will probably obtain no result in shell, but you can take a look in | + | You will probably obtain no result in shell, but you can take a look in your system log (/var/log/syslog or /var/log/messages) |
<pre class="apf"> | <pre class="apf"> | ||
itsme@mycomputer:~/log_syslog$ tail -n 2 /var/log/syslog | itsme@mycomputer:~/log_syslog$ tail -n 2 /var/log/syslog |
Revision as of 17:53, 19 September 2012
Calling syslog method from your C source code
This part is quiet simple, but we need to correctly use each parameter. First step is to declare us as syslog client :
...
#include <syslog.h>
int main(int argc, char *argv[])
{
...
openlog("program_name", LOG_PID, LOG_USER);
...
closelog();
...
}
Then, you can call syslog from your code using printf like format :
...
syslog(LOG_INFO, "%s called with %d arguments\n", argv[0], argc);
...
If you compile the source file:
// log_syslog.c
#include <syslog.h>
int main(int argc, char *argv[])
{
openlog("program_name", LOG_PID, LOG_USER);
syslog(LOG_INFO, "%s called with %d arguments", argv[0], argc - 1);
closelog();
return 0;
}
With the command :
itsme@mycomputer:~/log_syslog$ gcc log_syslog.c -o log-syslog
Then run it 2 times :
itsme@mycomputer:~/log_syslog$ ./log_syslog itsme@mycomputer:~/log_syslog$ ./log_syslog pim pam poum
You will probably obtain no result in shell, but you can take a look in your system log (/var/log/syslog or /var/log/messages)
itsme@mycomputer:~/log_syslog$ tail -n 2 /var/log/syslog Sep 19 18:46:49 mycomputer program_name[3022]: ./log_syslog called with 0 arguments Sep 19 18:46:53 mycomputer program_name[3023]: ./log_syslog called with 3 arguments
We can notice, that the lines contain program_name[PID] pattern showing us the name we have specified in openlog and the pid of the process because we ask it with LOG_PID argument.