2 * debug.c : small functions to help SVN developers
4 * ====================================================================
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
21 * ====================================================================
24 /* These functions are only available to SVN developers and should never
25 be used in release code. One of the reasons to avoid this code in release
26 builds is that this code is not thread-safe. */
30 #include <apr_pools.h>
31 #include <apr_strings.h>
32 #include "svn_types.h"
33 #include "svn_string.h"
35 #ifndef SVN_DBG__PROTOTYPES
36 #define SVN_DBG__PROTOTYPES
38 #include "private/svn_debug.h"
41 #define DBG_FLAG "DBG: "
43 /* This will be tweaked by the preamble code. */
44 static const char *debug_file = NULL;
45 static long debug_line = 0;
46 static FILE * volatile debug_output = NULL;
52 return getenv("SVN_DBG_QUIET") != NULL;
57 svn_dbg__preamble(const char *file, long line, FILE *output)
59 debug_output = output;
61 if (output != NULL && !quiet_mode())
63 /* Quick and dirty basename() code. */
64 const char *slash = strrchr(file, '/');
67 slash = strrchr(file, '\\');
69 debug_file = slash + 1;
77 /* Print a formatted string using format FMT and argument-list AP,
78 * prefixing each line of output with a debug header. */
80 debug_vprintf(const char *fmt, va_list ap)
82 FILE *output = debug_output;
83 char prefix[80], buffer[1000];
87 if (output == NULL || quiet_mode())
90 n = apr_snprintf(prefix, sizeof(prefix), DBG_FLAG "%s:%4ld: ",
91 debug_file, debug_line);
92 assert(n < sizeof(prefix) - 1);
93 n = apr_vsnprintf(buffer, sizeof(buffer), fmt, ap);
94 assert(n < sizeof(buffer) - 1);
97 char *newline = strchr(s, '\n');
101 fputs(prefix, output);
109 while (*s); /* print another line, except after a final newline */
114 svn_dbg__printf(const char *fmt, ...)
119 debug_vprintf(fmt, ap);
125 svn_dbg__print_props(apr_hash_t *props,
126 const char *header_fmt,
129 /* We only build this code if SVN_DEBUG is defined. */
132 apr_hash_index_t *hi;
135 va_start(ap, header_fmt);
136 debug_vprintf(header_fmt, ap);
141 svn_dbg__printf(" (null)\n");
145 for (hi = apr_hash_first(apr_hash_pool_get(props), props); hi;
146 hi = apr_hash_next(hi))
148 const char *name = svn__apr_hash_index_key(hi);
149 svn_string_t *val = svn__apr_hash_index_val(hi);
151 svn_dbg__printf(" '%s' -> '%s'\n", name, val->data);
153 #endif /* SVN_DEBUG */