OSDN Git Service

2013.10.24
[uclinux-h8/uClinux-dist.git] / user / rsyslog / errmsg.c
1 /* The errmsg object.
2  *
3  * Module begun 2008-03-05 by Rainer Gerhards, based on some code
4  * from syslogd.c
5  *
6  * Copyright 2008 Rainer Gerhards and Adiscon GmbH.
7  *
8  * This file is part of rsyslog.
9  *
10  * Rsyslog is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * Rsyslog is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Rsyslog.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * A copy of the GPL can be found in the file "COPYING" in this distribution.
24  */
25
26 #include "config.h"
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <errno.h>
31 #include <assert.h>
32
33 #include "rsyslog.h"
34 #include "syslogd.h"
35 #include "obj.h"
36 #include "errmsg.h"
37 #include "sysvar.h"
38 #include "srUtils.h"
39 #include "stringbuf.h"
40
41 /* static data */
42 DEFobjStaticHelpers
43
44
45 /* ------------------------------ methods ------------------------------ */
46
47
48 /* TODO: restructure this code some time. Especially look if we need
49  * to check errno and, if so, how to do that in a clean way.
50  */
51 static void __attribute__((format(printf, 2, 3)))
52 LogError(int __attribute__((unused)) iErrCode, char *fmt, ... )
53 {
54         va_list ap;
55         char buf[1024];
56         char msg[1024];
57         char errStr[1024];
58         size_t lenBuf;
59         
60         BEGINfunc
61         assert(fmt != NULL);
62         /* Format parameters */
63         va_start(ap, fmt);
64         lenBuf = vsnprintf(buf, sizeof(buf), fmt, ap);
65         if(lenBuf >= sizeof(buf)) {
66                 /* if our buffer was too small, we simply truncate. */
67                 lenBuf--;
68         }
69         va_end(ap);
70         
71         /* Log the error now */
72         buf[sizeof(buf)/sizeof(char) - 1] = '\0'; /* just to be on the safe side... */
73
74         dbgprintf("Called LogError, msg: %s\n", buf);
75
76         if (errno == 0) {
77                 snprintf(msg, sizeof(msg), "%s", buf);
78         } else {
79                 rs_strerror_r(errno, errStr, sizeof(errStr));
80                 snprintf(msg, sizeof(msg), "%s: %s", buf, errStr);
81         }
82         msg[sizeof(msg)/sizeof(char) - 1] = '\0'; /* just to be on the safe side... */
83         errno = 0;
84         logmsgInternal(LOG_SYSLOG|LOG_ERR, msg, ADDDATE);
85
86         ENDfunc
87 }
88
89
90 /* queryInterface function
91  * rgerhards, 2008-03-05
92  */
93 BEGINobjQueryInterface(errmsg)
94 CODESTARTobjQueryInterface(errmsg)
95         if(pIf->ifVersion != errmsgCURR_IF_VERSION) { /* check for current version, increment on each change */
96                 ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED);
97         }
98
99         /* ok, we have the right interface, so let's fill it
100          * Please note that we may also do some backwards-compatibility
101          * work here (if we can support an older interface version - that,
102          * of course, also affects the "if" above).
103          */
104         pIf->LogError = LogError;
105 finalize_it:
106 ENDobjQueryInterface(errmsg)
107
108
109 /* Initialize the errmsg class. Must be called as the very first method
110  * before anything else is called inside this class.
111  * rgerhards, 2008-02-19
112  */
113 BEGINAbstractObjClassInit(errmsg, 1, OBJ_IS_CORE_MODULE) /* class, version */
114         /* request objects we use */
115
116         /* set our own handlers */
117 ENDObjClassInit(errmsg)
118
119 /* vi:set ai:
120  */