You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@apr.apache.org by Joe Orton <jo...@redhat.com> on 2005/07/05 18:10:33 UTC

[PATCH] vformatter formatting apr_status_t

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

Re: [PATCH] vformatter formatting apr_status_t

Posted by "William A. Rowe, Jr." <wr...@rowe-clan.net>.
At 02:24 PM 7/5/2005, Joe Orton wrote:
>On Tue, Jul 05, 2005 at 01:58:41PM -0500, William Rowe wrote:
>> -1 on the whole %% abuse, it's is the wrong convention.
>
>Sorry s/%%pm/%pm/ in my message, %% is and always has been an escaped %.  
>I just brainoed it after writing the doxygen string which also 
>confusingly escapes the %s.

Whoops :)  In that case, +1.



Re: [PATCH] vformatter formatting apr_status_t

Posted by Joe Orton <jo...@redhat.com>.
On Tue, Jul 05, 2005 at 01:58:41PM -0500, William Rowe wrote:
> At 11:10 AM 7/5/2005, Joe Orton wrote:
> >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.
> 
> +1 in principal.
> 
> -1 on the whole %% abuse, it's is the wrong convention.  %% is supposed
> to be a literal '%', and abusing it for subclassing a long list of
> specialized formats was wrong.  We need to drop a '%' from that,
> restoring the meaning of %% in the format string, no?

Sorry s/%%pm/%pm/ in my message, %% is and always has been an escaped %.  
I just brainoed it after writing the doxygen string which also 
confusingly escapes the %s.

joe

Re: [PATCH] vformatter formatting apr_status_t

Posted by "William A. Rowe, Jr." <wr...@rowe-clan.net>.
At 11:10 AM 7/5/2005, Joe Orton wrote:
>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.

+1 in principal.

-1 on the whole %% abuse, it's is the wrong convention.  %% is supposed
to be a literal '%', and abusing it for subclassing a long list of
specialized formats was wrong.  We need to drop a '%' from that,
restoring the meaning of %% in the format string, no?

Bill



Re: [PATCH] vformatter formatting apr_status_t

Posted by Jeff Trawick <tr...@gmail.com>.
On 7/5/05, Joe Orton <jo...@redhat.com> wrote:
> 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?

no, looks very useful

Re: [PATCH] vformatter formatting apr_status_t

Posted by Jim Jagielski <ji...@apache.org>.
On Jul 5, 2005, at 12:10 PM, Joe Orton wrote:

> 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?
>

Nope, +1