apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joe Orton <jor...@redhat.com>
Subject [PATCH] vformatter formatting apr_status_t
Date Tue, 05 Jul 2005 16:10:33 GMT
Since 1.2.0 already adds one of these, why not another...

glibc has a %m format string which is which expands to strerror(); this 
adds an equivalent %%pm which takes a pointer to an apr_status_t and 
prints the appropriate error string for that error code.

This is really nice to use on error paths, where you can collapse the 
typical "apr_psprintf + apr_strerror into a stack buffer" sequence into 
a single apr_psprintf call with no stack buffer needed.

Objections?

Index: strings/apr_snprintf.c
===================================================================
--- strings/apr_snprintf.c	(revision 209291)
+++ strings/apr_snprintf.c	(working copy)
@@ -21,6 +21,7 @@
 #include "apr_strings.h"
 #include "apr_network_io.h"
 #include "apr_portable.h"
+#include "apr_errno.h"
 #include <math.h>
 #if APR_HAVE_CTYPE_H
 #include <ctype.h>
@@ -1166,6 +1167,24 @@
                 }
                 break;
 
+                /* print the error for an apr_status_t */
+                case 'm':
+                {
+                    apr_status_t *mrv;
+
+                    mrv = va_arg(ap, apr_status_t *);
+                    if (mrv != NULL) {
+                        s = apr_strerror(*mrv, num_buf, NUM_BUF_SIZE-1);
+                        s_len = strlen(s);
+                    }
+                    else {
+                        s = S_NULL;
+                        s_len = S_NULL_LEN;
+                    }
+                    pad_char = ' ';
+                }
+                break;
+
                 case 'T':
 #if APR_HAS_THREADS
                 {
Index: test/testfmt.c
===================================================================
--- test/testfmt.c	(revision 209291)
+++ test/testfmt.c	(working copy)
@@ -117,6 +117,24 @@
     ABTS_STR_EQUAL(tc, buf, "-314159265358979323");
 }
 
+static void error_fmt(abts_case *tc, void *data)
+{
+    char ebuf[150], sbuf[150], *s;
+    apr_status_t rv;
+
+    rv = APR_SUCCESS;
+    apr_strerror(rv, ebuf, sizeof ebuf);
+    apr_snprintf(sbuf, sizeof sbuf, "%pm", &rv);
+    ABTS_STR_EQUAL(tc, sbuf, ebuf);
+
+    rv = APR_ENOTIMPL;
+    s = apr_pstrcat(p, "foo-",
+                    apr_strerror(rv, ebuf, sizeof ebuf),
+                    "-bar", NULL);
+    apr_snprintf(sbuf, sizeof sbuf, "foo-%pm-bar", &rv);
+    ABTS_STR_EQUAL(tc, sbuf, s);
+}
+
 abts_suite *testfmt(abts_suite *suite)
 {
     suite = ADD_SUITE(suite)
@@ -129,6 +147,7 @@
     abts_run_test(suite, uint64_t_fmt, NULL);
     abts_run_test(suite, uint64_t_hex_fmt, NULL);
     abts_run_test(suite, more_int64_fmts, NULL);
+    abts_run_test(suite, error_fmt, NULL);
 
     return suite;
 }
Index: include/apr_lib.h
===================================================================
--- include/apr_lib.h	(revision 209291)
+++ include/apr_lib.h	(working copy)
@@ -118,6 +118,8 @@
  *      ('0' is printed if !APR_HAS_THREADS)
  * %%pt takes an apr_os_thread_t * and prints it in hexadecimal
  *      ('0' is printed if !APR_HAS_THREADS)
+ * %%pm takes an apr_status_t * and prints the appropriate error
+ *      string (from apr_strerror) corresponding to that error code.
  * %%pp takes a void * and outputs it in hex
  *
  * The %%p hacks are to force gcc's printf warning code to skip

Mime
View raw message