From 1ea869ec5460806841ab4feacf24e25c288a5760 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Fri, 29 Nov 2013 09:16:03 +0100 Subject: use const keyword at (some) appropriate places --- runtime/errmsg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/errmsg.c') diff --git a/runtime/errmsg.c b/runtime/errmsg.c index dcb5b185..55d013ea 100644 --- a/runtime/errmsg.c +++ b/runtime/errmsg.c @@ -57,7 +57,7 @@ DEFobjStaticHelpers * rgerhards, 2008-06-27 */ static void __attribute__((format(printf, 3, 4))) -LogError(int iErrno, int iErrCode, char *fmt, ... ) +LogError(int iErrno, int iErrCode, const char *fmt, ... ) { va_list ap; char buf[1024]; -- cgit v1.2.3 From 89060ecec2db9799af3d5eb17a174b9a0f2bd4b2 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Fri, 29 Nov 2013 09:44:58 +0100 Subject: introduce new function to emit warning and other non-error messages also refactor the error message subsystem a bit --- runtime/errmsg.c | 96 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 28 deletions(-) (limited to 'runtime/errmsg.c') diff --git a/runtime/errmsg.c b/runtime/errmsg.c index 55d013ea..5a3fca9b 100644 --- a/runtime/errmsg.c +++ b/runtime/errmsg.c @@ -56,51 +56,90 @@ DEFobjStaticHelpers * maps to a specific error event). * rgerhards, 2008-06-27 */ -static void __attribute__((format(printf, 3, 4))) -LogError(int iErrno, int iErrCode, const char *fmt, ... ) +static void +doLogMsg(const int iErrno, const int iErrCode, const int severity, const char *msg) { - va_list ap; - char buf[1024]; - char msg[1024]; + char buf[2048]; char errStr[1024]; - size_t lenBuf; - - BEGINfunc - assert(fmt != NULL); - /* Format parameters */ - va_start(ap, fmt); - lenBuf = vsnprintf(buf, sizeof(buf), fmt, ap); - if(lenBuf >= sizeof(buf)) { - /* if our buffer was too small, we simply truncate. */ - lenBuf--; - } - va_end(ap); - /* Log the error now */ - buf[sizeof(buf)/sizeof(char) - 1] = '\0'; /* just to be on the safe side... */ - - dbgprintf("Called LogError, msg: %s\n", buf); + dbgprintf("Called LogMsg, msg: %s\n", msg); if(iErrno != 0) { rs_strerror_r(iErrno, errStr, sizeof(errStr)); if(iErrCode == NO_ERRCODE || iErrCode == RS_RET_ERR) { - snprintf(msg, sizeof(msg), "%s: %s", buf, errStr); + snprintf(buf, sizeof(buf), "%s: %s", msg, errStr); } else { - snprintf(msg, sizeof(msg), "%s: %s [try http://www.rsyslog.com/e/%d ]", buf, errStr, iErrCode * -1); + snprintf(buf, sizeof(buf), "%s: %s [try http://www.rsyslog.com/e/%d ]", msg, errStr, iErrCode * -1); } } else { if(iErrCode == NO_ERRCODE || iErrCode == RS_RET_ERR) { - snprintf(msg, sizeof(msg), "%s", buf); + snprintf(buf, sizeof(buf), "%s", msg); } else { - snprintf(msg, sizeof(msg), "%s [try http://www.rsyslog.com/e/%d ]", buf, iErrCode * -1); + snprintf(buf, sizeof(buf), "%s [try http://www.rsyslog.com/e/%d ]", msg, iErrCode * -1); } } - msg[sizeof(msg)/sizeof(char) - 1] = '\0'; /* just to be on the safe side... */ + buf[sizeof(buf)/sizeof(char) - 1] = '\0'; /* just to be on the safe side... */ errno = 0; - glblErrLogger(iErrCode, (uchar*)msg); + glblErrLogger(severity, iErrCode, (uchar*)buf); +} + +/* We now receive three parameters: one is the internal error code + * which will also become the error message number, the second is + * errno - if it is non-zero, the corresponding error message is included + * in the text and finally the message text itself. Note that it is not + * 100% clean to use the internal errcode, as it may be reached from + * multiple actual error causes. However, it is much better than having + * no error code at all (and in most cases, a single internal error code + * maps to a specific error event). + * rgerhards, 2008-06-27 + */ +static void __attribute__((format(printf, 3, 4))) +LogError(const int iErrno, const int iErrCode, const char *fmt, ... ) +{ + va_list ap; + char buf[2048]; + size_t lenBuf; + + va_start(ap, fmt); + lenBuf = vsnprintf(buf, sizeof(buf), fmt, ap); + if(lenBuf >= sizeof(buf)) { + /* if our buffer was too small, we simply truncate. */ + lenBuf--; + } + va_end(ap); + buf[sizeof(buf)/sizeof(char) - 1] = '\0'; /* just to be on the safe side... */ + + doLogMsg(iErrno, iErrCode, LOG_ERR, buf); +} - ENDfunc +/* We now receive three parameters: one is the internal error code + * which will also become the error message number, the second is + * errno - if it is non-zero, the corresponding error message is included + * in the text and finally the message text itself. Note that it is not + * 100% clean to use the internal errcode, as it may be reached from + * multiple actual error causes. However, it is much better than having + * no error code at all (and in most cases, a single internal error code + * maps to a specific error event). + * rgerhards, 2008-06-27 + */ +static void __attribute__((format(printf, 4, 5))) +LogMsg(const int iErrno, const int iErrCode, const int severity, const char *fmt, ... ) +{ + va_list ap; + char buf[2048]; + size_t lenBuf; + + va_start(ap, fmt); + lenBuf = vsnprintf(buf, sizeof(buf), fmt, ap); + if(lenBuf >= sizeof(buf)) { + /* if our buffer was too small, we simply truncate. */ + lenBuf--; + } + va_end(ap); + buf[sizeof(buf)/sizeof(char) - 1] = '\0'; /* just to be on the safe side... */ + + doLogMsg(iErrno, iErrCode, severity, buf); } @@ -119,6 +158,7 @@ CODESTARTobjQueryInterface(errmsg) * of course, also affects the "if" above). */ pIf->LogError = LogError; + pIf->LogMsg = LogMsg; finalize_it: ENDobjQueryInterface(errmsg) -- cgit v1.2.3 From a42da69a72699081a2e75c737137c7a49c0fe893 Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Fri, 29 Nov 2013 09:49:42 +0100 Subject: nit: update copyright dates --- runtime/errmsg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'runtime/errmsg.c') diff --git a/runtime/errmsg.c b/runtime/errmsg.c index 5a3fca9b..b3941431 100644 --- a/runtime/errmsg.c +++ b/runtime/errmsg.c @@ -7,7 +7,7 @@ * to take further case, as the code now boils to be either my own or, a few lines, * of the original BSD-licenses sysklogd code. rgerhards, 2012-01-16 * - * Copyright 2008-2012 Adiscon GmbH. + * Copyright 2008-2013 Adiscon GmbH. * * This file is part of the rsyslog runtime library. * -- cgit v1.2.3