Skip to content

Commit 56d1136

Browse files
committed
Add %pg printf format string converter to print a pool tag:
* include/apr_lib.h, strings/apr_snprintf.c (apr_vformatter): Add %pg support. * test/testfmt.c (test_tag_fmt): New test case. Github: closes #64 git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1924665 13f79535-47bb-0310-9956-ffa450edef68
1 parent 53044c5 commit 56d1136

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

Diff for: include/apr_lib.h

+3
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ APR_DECLARE(const char *) apr_filepath_name_get(const char *pathname);
120120
* ('0' is printed if !APR_HAS_THREADS)
121121
* - %%pm takes an apr_status_t * and prints the appropriate error
122122
* string (from apr_strerror) corresponding to that error code.
123+
* - %%pg takes an apr_pool_t * and prints the pool tag,
124+
* or '(untagged)' if no pool tag is set
123125
* - %%pp takes a void * and outputs it in hex
124126
* - %%pB takes a apr_uint32_t * as bytes and outputs it's apr_strfsize
125127
* - %%pF same as above, but takes a apr_off_t *
@@ -128,6 +130,7 @@ APR_DECLARE(const char *) apr_filepath_name_get(const char *pathname);
128130
* %%pA, %%pI, %%pT, %%pp are available from APR 1.0.0 onwards (and in 0.9.x).
129131
* %%pt is only available from APR 1.2.0 onwards.
130132
* %%pm, %%pB, %%pF and %%pS are only available from APR 1.3.0 onwards.
133+
* %%pg is only available from APR 2.0.0 onwards.
131134
*
132135
* The %%p hacks are to force gcc's printf warning code to skip
133136
* over a pointer argument without complaining. This does

Diff for: strings/apr_snprintf.c

+19
Original file line numberDiff line numberDiff line change
@@ -1191,6 +1191,25 @@ APR_DECLARE(int) apr_vformatter(int (*flush_func)(apr_vformatter_buff_t *),
11911191
}
11921192
break;
11931193

1194+
/* print the tag for an apr_pool_t */
1195+
case 'g':
1196+
{
1197+
apr_pool_t *prv;
1198+
1199+
prv = va_arg(ap, apr_pool_t *);
1200+
if (prv != NULL) {
1201+
s = (char *)apr_pool_get_tag(prv);
1202+
if (!s) s = "(untagged)";
1203+
s_len = strlen(s);
1204+
}
1205+
else {
1206+
s = S_NULL;
1207+
s_len = S_NULL_LEN;
1208+
}
1209+
pad_char = ' ';
1210+
}
1211+
break;
1212+
11941213
case 'T':
11951214
#if APR_HAS_THREADS
11961215
{

Diff for: test/testfmt.c

+22
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,27 @@ static void error_fmt(abts_case *tc, void *data)
146146
ABTS_STR_EQUAL(tc, sbuf, s);
147147
}
148148

149+
#define TEST_TAG "pool_tag_fmt_tag"
150+
151+
static void pool_tag_fmt(abts_case *tc, void *data)
152+
{
153+
char sbuf[150];
154+
apr_pool_t *testp;
155+
156+
APR_ASSERT_SUCCESS(tc, "Create test pool", apr_pool_create(&testp, NULL));
157+
158+
#if !APR_POOL_DEBUG
159+
apr_snprintf(sbuf, sizeof sbuf, "tag-%pg-end", testp);
160+
ABTS_STR_EQUAL(tc, "tag-(untagged)-end", sbuf);
161+
#endif
162+
163+
apr_pool_tag(testp, TEST_TAG);
164+
apr_snprintf(sbuf, sizeof sbuf, "tag-%pg-end", testp);
165+
ABTS_STR_EQUAL(tc, "tag-" TEST_TAG "-end", sbuf);
166+
167+
apr_pool_destroy(testp);
168+
}
169+
149170
abts_suite *testfmt(abts_suite *suite)
150171
{
151172
suite = ADD_SUITE(suite)
@@ -160,6 +181,7 @@ abts_suite *testfmt(abts_suite *suite)
160181
abts_run_test(suite, uint64_t_hex_fmt, NULL);
161182
abts_run_test(suite, more_int64_fmts, NULL);
162183
abts_run_test(suite, error_fmt, NULL);
184+
abts_run_test(suite, pool_tag_fmt, NULL);
163185

164186
return suite;
165187
}

0 commit comments

Comments
 (0)