1 /*******************************************************************************
 2  *  Copyright (c) 2009, Arthur Benilov
 3  *
 4  * This library is free software; you can redistribute it and/or
 5  * modify it under the terms of the GNU Lesser General Public
 6  * License as published by the Free Software Foundation; either
 7  * version 2 of the License, or (at your option) any later version.
 8  *
 9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  ******************************************************************************/
18 
19 /*
20     This example demonstrates syslog client initialization, usage, and
21     logMsg() messages redirection.
22 */
23 
24 #include <logLib.h>
25 #include "syslog.h"
26 
27 /**
28  * Entry point
29  */
30 void syslog_example ( ) {
31 
32     /* Before any syslog message will be sent, we have to initialize the library.
33      * This will create a separate task called 'tSyslog' that handles
34      * syslog messages asynchronously (just like logMsg() does).
35      *
36      * Be sure you have defined SYSLOG_HOST and SYSLOG_PORT environment variables,
37      * elsewhere an error message will be printed in console.
38      */
39     syslogInit();
40 
41     /* Since now we can send a message to syslog server using syslog() function.
42      * The format of the function is the same as for printf().
43      */
44     syslog(LOG_USER, LOG_INFO, "Message from %s function", __FUNCTION__);
45 
46     /* It may whant to redirect all the messages generated by logMsg() functions
47      * to a syslog server. Since redirection activated via
48      * logFdAdd() and others does not work with UDP sockets, a spectial driver
49      * will be installed. This driver creates a virtual file /syslog writing
50      * to which will transfer the data to syslog server.
51      *
52      * Here we activate the redirection function. We have to specify a facility
53      * and severity that will be assigned to any logMsg() message redirected.
54      */
55     syslogRedirectEnable(LOG_USER, LOG_INFO);
56 
57     /* Now, if we call logMsg() function it will print a message as usual but
58      * copy it as well to a syslog server. This will work as well with any
59      * other currently running tasks using logMsg().
60      */
61     logMsg("Another message from %s\n", __FUNCTION__);
62 
63     /* Note, that you have to specify 'end of line' character '\n'
64      * at the end of a string, since syslog driver assembles all
65      * non-terminated strings to a single one, and sends it to syslog
66      * server only if '\n' character detected or message length is
67      * bigger than 1024 characters (a limit of UDP message).
68      *
69      * So, this will generate a single syslog message:
70      */
71     logMsg("Third ");
72     logMsg("message\n");
73 
74     /* In order to disable logMsg() redirection, have to call
75      * the fillowing function:
76      */
77     syslogRedirectDisable();
78 
79     logMsg("This message will not be sent to syslog server.\n");
80 }
81 
82 /* ------------------------------------------------------ */
83 /* End of file */