diff options
author | Rainer Gerhards <rgerhards@adiscon.com> | 2013-11-29 09:44:58 +0100 |
---|---|---|
committer | Rainer Gerhards <rgerhards@adiscon.com> | 2013-11-29 09:44:58 +0100 |
commit | 89060ecec2db9799af3d5eb17a174b9a0f2bd4b2 (patch) | |
tree | b2e5c5f7c18b4f5bb7f0a6a0c5c564df9e6e627b /runtime/errmsg.c | |
parent | 1ea869ec5460806841ab4feacf24e25c288a5760 (diff) | |
download | rsyslog-89060ecec2db9799af3d5eb17a174b9a0f2bd4b2.tar.gz rsyslog-89060ecec2db9799af3d5eb17a174b9a0f2bd4b2.tar.bz2 rsyslog-89060ecec2db9799af3d5eb17a174b9a0f2bd4b2.zip |
introduce new function to emit warning and other non-error messages
also refactor the error message subsystem a bit
Diffstat (limited to 'runtime/errmsg.c')
-rw-r--r-- | runtime/errmsg.c | 96 |
1 files changed, 68 insertions, 28 deletions
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) |