Return-Path: Delivered-To: apmail-apr-dev-archive@www.apache.org Received: (qmail 2383 invoked from network); 5 Jul 2005 16:10:51 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 5 Jul 2005 16:10:51 -0000 Received: (qmail 26858 invoked by uid 500); 5 Jul 2005 16:10:48 -0000 Delivered-To: apmail-apr-dev-archive@apr.apache.org Received: (qmail 26809 invoked by uid 500); 5 Jul 2005 16:10:47 -0000 Mailing-List: contact dev-help@apr.apache.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Id: Delivered-To: mailing list dev@apr.apache.org Received: (qmail 26786 invoked by uid 99); 5 Jul 2005 16:10:46 -0000 Received: from asf.osuosl.org (HELO asf.osuosl.org) (140.211.166.49) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 05 Jul 2005 09:10:46 -0700 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: pass (asf.osuosl.org: domain of jorton@redhat.com designates 66.187.233.31 as permitted sender) Received: from [66.187.233.31] (HELO mx1.redhat.com) (66.187.233.31) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 05 Jul 2005 09:10:46 -0700 Received: from int-mx1.corp.redhat.com (int-mx1.corp.redhat.com [172.16.52.254]) by mx1.redhat.com (8.12.11/8.12.11) with ESMTP id j65GAgFV022561 for ; Tue, 5 Jul 2005 12:10:42 -0400 Received: from radish.cambridge.redhat.com (radish.cambridge.redhat.com [172.16.18.90]) by int-mx1.corp.redhat.com (8.11.6/8.11.6) with ESMTP id j65GAYV08081 for ; Tue, 5 Jul 2005 12:10:36 -0400 Received: (from jorton@localhost) by radish.cambridge.redhat.com (8.13.4/8.13.4/Submit) id j65GAXgi018089 for dev@apr.apache.org; Tue, 5 Jul 2005 17:10:33 +0100 X-Authentication-Warning: radish.cambridge.redhat.com: jorton set sender to jorton@redhat.com using -f Date: Tue, 5 Jul 2005 17:10:33 +0100 From: Joe Orton To: dev@apr.apache.org Subject: [PATCH] vformatter formatting apr_status_t Message-ID: <20050705161033.GA17811@redhat.com> Mail-Followup-To: dev@apr.apache.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline User-Agent: Mutt/1.4.2.1i X-Virus-Checked: Checked by ClamAV on apache.org X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N 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 #if APR_HAVE_CTYPE_H #include @@ -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