APUE course source code
guowenxue
yesterday 7b55c92f8d1401a93c8fd8e342da271dce742000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*********************************************************************************
 *      Copyright:  (C) 2025 LingYun IoT System Studio
 *                  All rights reserved.
 *
 *       Filename:  syslog.c
 *    Description:  This file is syslog() example code.
 *                 
 *        Version:  1.0.0(10/28/2025)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "10/28/2025 02:15:06 PM"
 *                 
 ********************************************************************************/
 
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <libgen.h> /* basename() */
 
int main(int argc, char **argv)
{
    char      *progname = basename(argv[0]);
 
    if( daemon(0, 0) < 0)
    {
        printf("Program daemon() failure: %s\n", strerror(errno));
        return -1;
    }
    openlog("daemon", LOG_CONS | LOG_PID, 0);
    syslog(LOG_NOTICE,  "Program '%s'start running\n",  progname);
    syslog(LOG_WARNING,  "Program '%s' running with a warnning message\n",  progname );
    syslog(LOG_EMERG,  "Program '%s' running with a emergency message\n",  progname );
    while(1)
    {
        //Do Something here
        ;
    }
 
    syslog(LOG_NOTICE,  "Program '%s' stop running\n",  progname);
    closelog();
 
    return 0;
}