1/* 2 * debug.c : small functions to help SVN developers 3 * 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 12 * 13 * http://www.apache.org/licenses/LICENSE-2.0 14 * 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 20 * under the License. 21 * ==================================================================== 22 */ 23 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. */ 27#include <stdarg.h> 28#include <assert.h> 29 30#include <apr_pools.h> 31#include <apr_strings.h> 32#include "svn_types.h" 33#include "svn_string.h" 34 35#ifndef SVN_DBG__PROTOTYPES 36#define SVN_DBG__PROTOTYPES 37#endif 38#include "private/svn_debug.h" 39 40 41#define DBG_FLAG "DBG: " 42 43/* This will be tweaked by the preamble code. */ 44static const char *debug_file = NULL; 45static long debug_line = 0; 46static FILE * volatile debug_output = NULL; 47 48 49static svn_boolean_t 50quiet_mode(void) 51{ 52 return getenv("SVN_DBG_QUIET") != NULL; 53} 54 55 56void 57svn_dbg__preamble(const char *file, long line, FILE *output) 58{ 59 debug_output = output; 60 61 if (output != NULL && !quiet_mode()) 62 { 63 /* Quick and dirty basename() code. */ 64 const char *slash = strrchr(file, '/'); 65 66 if (slash == NULL) 67 slash = strrchr(file, '\\'); 68 if (slash) 69 debug_file = slash + 1; 70 else 71 debug_file = file; 72 } 73 debug_line = line; 74} 75 76 77/* Print a formatted string using format FMT and argument-list AP, 78 * prefixing each line of output with a debug header. */ 79static void 80debug_vprintf(const char *fmt, va_list ap) 81{ 82 FILE *output = debug_output; 83 char prefix[80], buffer[1000]; 84 char *s = buffer; 85 int n; 86 87 if (output == NULL || quiet_mode()) 88 return; 89 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); 95 do 96 { 97 char *newline = strchr(s, '\n'); 98 if (newline) 99 *newline = '\0'; 100 101 fputs(prefix, output); 102 fputs(s, output); 103 fputc('\n', output); 104 105 if (! newline) 106 break; 107 s = newline + 1; 108 } 109 while (*s); /* print another line, except after a final newline */ 110} 111 112 113void 114svn_dbg__printf(const char *fmt, ...) 115{ 116 va_list ap; 117 118 va_start(ap, fmt); 119 debug_vprintf(fmt, ap); 120 va_end(ap); 121} 122 123 124void 125svn_dbg__print_props(apr_hash_t *props, 126 const char *header_fmt, 127 ...) 128{ 129/* We only build this code if SVN_DEBUG is defined. */ 130#ifdef SVN_DEBUG 131 132 apr_hash_index_t *hi; 133 va_list ap; 134 135 va_start(ap, header_fmt); 136 debug_vprintf(header_fmt, ap); 137 va_end(ap); 138 139 if (props == NULL) 140 { 141 svn_dbg__printf(" (null)\n"); 142 return; 143 } 144 145 for (hi = apr_hash_first(apr_hash_pool_get(props), props); hi; 146 hi = apr_hash_next(hi)) 147 { 148 const char *name = svn__apr_hash_index_key(hi); 149 svn_string_t *val = svn__apr_hash_index_val(hi); 150 151 svn_dbg__printf(" '%s' -> '%s'\n", name, val->data); 152 } 153#endif /* SVN_DEBUG */ 154} 155 156