1 /*=--------------------------------------------------------------------------=* 2 * NetDebug.cpp -- Implementation of the BNetDebug class. 3 * 4 * Written by S.T. Mansfield (thephantom@mac.com) 5 * 6 * Remarks: 7 * * Although this would more properly be implemented as a namespace... 8 * * Do not burn the candle at both ends as it leads to the life of a 9 * hairdresser. 10 *=--------------------------------------------------------------------------=* 11 * Copyright (c) 2002, The OpenBeOS project. 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining a 14 * copy of this software and associated documentation files (the "Software"), 15 * to deal in the Software without restriction, including without limitation 16 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 17 * and/or sell copies of the Software, and to permit persons to whom the 18 * Software is furnished to do so, subject to the following conditions: 19 * 20 * The above copyright notice and this permission notice shall be included in 21 * all copies or substantial portions of the Software. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 29 * DEALINGS IN THE SOFTWARE. 30 *=--------------------------------------------------------------------------=* 31 */ 32 33 34 #include <ctype.h> 35 #include <stdio.h> 36 #include <string.h> 37 38 #include <NetDebug.h> 39 40 41 // Off by default cuz the BeBook sez so. 42 static bool g_IsEnabled = false; 43 44 45 /* Enable 46 *=--------------------------------------------------------------------------=* 47 * Purpose: 48 * Enable/disable debug message capability for your app. 49 * 50 * Input parameter: 51 * Enable : True/False to enable/disable debug message output. 52 * 53 * Remarks: 54 * This flag/setting is a per application basis, and not a per-instance 55 * occurrence as one would expect when instantiating an instance of 56 * this class. This is by design as /everything/ is static. Caveat 57 * Emptor. Needs to be dealt with in G.E. 58 */ 59 void BNetDebug::Enable( bool Enable ) 60 { 61 g_IsEnabled = Enable; 62 } 63 64 65 /* IsEnabled 66 *=--------------------------------------------------------------------------=* 67 * Purpose: 68 * Quiz the enable/disable status. 69 * 70 * Returns: 71 * True/false if enabled/disabled. 72 */ 73 bool BNetDebug::IsEnabled( void ) 74 { 75 return g_IsEnabled; 76 } 77 78 79 /* Print 80 *=--------------------------------------------------------------------------=* 81 * Purpose: 82 * If enabled, spew forth a debug message. 83 * 84 * Input parameter: 85 * msg : The message to print. 86 * 87 * Remarks: 88 * * Basically a no-op if not enabled. 89 * * We're inheriting R5 (and Nettle's) behavior, so... 90 * * The output is always "debug: msg\n" Yes, kids, you read it right; 91 * you get a newline whether you want it or not! 92 * * If msg is empty, you get "debug: \n" 93 * * Message is always printed on stderr. Redirect accordingly. 94 */ 95 void BNetDebug::Print( const char* msg ) 96 { 97 if ( g_IsEnabled ) 98 { 99 if ( msg == NULL ) 100 { 101 msg = ""; 102 } 103 104 fprintf( stderr, "debug: %s\n", msg ); 105 } 106 } 107 108 109 /* Dump 110 *=--------------------------------------------------------------------------=* 111 * Purpose: 112 * If enabled, spew forth a combination hex/ASCII dump of raw data. 113 * 114 * Input parameters: 115 * data : Data to dump. 116 * size : How many bytes of data to dump. 117 * title : Title to display in message header. 118 * 119 * Remarks: 120 * * Basically a no-op if not enabled. 121 * * We're inheriting R5 (and Nettle's) behavior, so... 122 * * The output is always "debug: msg\n" Yes, kids, you read it right; 123 * you get a newline whether you want it or not! 124 * * If msg is empty, you get "debug: \n" 125 * * Behavior is undefined if data or title is NULL. This is a 126 * possible design flaw in Nettle/R5. 127 * * Do not expect an output like this: 128 * 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF | abcdefghijklmnop 129 * 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF | abcdefghijklmnop 130 * 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF | abcdefghijklmnop 131 * 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF | abcdefghijklmnop 132 * because you ain't gettin' it. You will get a complete hex dump, 133 * /followed/ by an ASCII dump. More Glass Elevator stuff. 134 * * Nettle dumps to stdOUT, the BeBook says all output goes to stdERR, so 135 * the author chose to... 136 * * always print to stdERR. Redirect accordingly. 137 * * stderr is flushed after the dump is complete to keep things 138 * reasonably cohesive in appearance. This might be an expensive 139 * operation so use judiciously. 140 */ 141 void BNetDebug::Dump( const char* data, size_t size, const char* title ) 142 { 143 int i; 144 char line[17]; 145 146 if ( g_IsEnabled == false ) 147 { 148 return; 149 } 150 151 fprintf( stderr, "----------\n%s\n(dumping %d bytes)\n", title, size ); 152 153 memset( line, 0, sizeof( line ) ); 154 155 while ( i < size ) 156 { 157 fprintf( stderr, "%02x ", ( unsigned char )data[i] ); 158 159 line[i % 16] = ( isprint( data[i] ) ) ? data[i] : '.'; 160 161 if ( ( i + 1 ) % 16 == 0 ) 162 { 163 fprintf( stderr, " | %s\n", line ); 164 memset( line, 0, sizeof( line ) ); 165 } 166 167 i++; 168 } 169 170 if ( ( i + 1 ) % 16 != 0 ) 171 { 172 while ( i % 16 != 0 ) 173 { 174 printf( " " ); 175 i++; 176 } 177 178 fprintf( stderr, " | %s", line ); 179 } 180 181 fprintf( stderr, "\n----------\n" ); 182 fflush( stderr ); 183 } 184 185 186 /*=------------------------------------------------------------------- End -=*/ 187 188