--- l2_channel.c 2001/09/13 12:50:26 1.18
+++ l2_channel.c 2001/09/24 15:36:23 1.19
@@ -51,6 +51,9 @@
ch->downstream = NULL;
memset(&ch->context, 0, sizeof(l2_context_t));
memcpy(&ch->handler, h, sizeof(l2_handler_t));
+ ch->rvErrorInfo = L2_OK;
+ ch->szErrorInfo[0] = '\0';
+ ch->szError[0] = '\0';
/* (optionally) perform create operation in handler */
if (ch->handler.create != NULL) {
@@ -229,6 +232,70 @@
return rv;
}
+l2_result_t l2_channel_errorinfo(l2_channel_t *ch, l2_result_t rv, const char *fmt, ...)
+{
+ va_list ap;
+
+ /* argument sanity check */
+ if (ch == NULL || rv == L2_OK || fmt == NULL)
+ return L2_ERR_ARG;
+
+ /* remember error information */
+ va_start(ap, fmt);
+ l2_util_vsprintf(ch->szErrorInfo, sizeof(ch->szErrorInfo), fmt, ap);
+ ch->rvErrorInfo = rv;
+ va_end(ap);
+
+ return L2_OK;
+}
+
+char *l2_channel_strerror(l2_channel_t *ch, l2_result_t rv)
+{
+ char *sz;
+ char *cpBuf;
+ int nBuf;
+ int n;
+
+ /* argument sanity check */
+ if (ch == NULL)
+ return NULL;
+
+ /* start at begin of buffer */
+ cpBuf = ch->szError;
+ nBuf = sizeof(ch->szError);
+
+ /* translate result value into corresponding string */
+ if (rv == L2_OK) sz = "everything ok";
+ else if (rv == L2_ERR_ARG) sz = "invalid argument";
+ else if (rv == L2_ERR_USE) sz = "invalid use";
+ else if (rv == L2_ERR_MEM) sz = "no more memory available";
+ else if (rv == L2_ERR_SYS) sz = "operating system error";
+ else if (rv == L2_ERR_IO) sz = "input/output error";
+ else if (rv == L2_ERR_FMT) sz = "formatting error";
+ else if (rv == L2_ERR_INT) sz = "internal error";
+ else sz = "unknown error";
+ n = l2_util_sprintf(cpBuf, nBuf, "%s", sz);
+ cpBuf += n;
+ nBuf -= n;
+
+ /* optionally annotate with error information */
+ if (rv == ch->rvErrorInfo && ch->szErrorInfo[0] != '\0') {
+ n = l2_util_sprintf(cpBuf, nBuf, "; %s", ch->szErrorInfo);
+ cpBuf += n;
+ nBuf -= n;
+ }
+
+ /* optionally annotate with operating system error information */
+ if (rv == L2_ERR_SYS) {
+ n = l2_util_sprintf(cpBuf, nBuf, "; %s (%d)", strerror(errno), errno);
+ cpBuf += n;
+ nBuf -= n;
+ }
+
+ /* return pointer to internal buffer */
+ return ch->szError;
+}
+
/* stack channel on top of another channel */
l2_result_t l2_channel_stack(l2_channel_t *ch, l2_channel_t *chTop)
{
|