1 /*=--------------------------------------------------------------------------=*
2 * NetDebug.cpp -- Implementation of the BNetDebug class.
4 * Written by S.T. Mansfield (thephantom@mac.com)
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
10 *=--------------------------------------------------------------------------=*
11 * Copyright (c) 2002, The OpenBeOS project.
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:
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
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 *=--------------------------------------------------------------------------=*
39 #include <SupportDefs.h>
42 // Off by default cuz the BeBook sez so.
43 static bool g_NetDebugEnabled
= false;
47 *=--------------------------------------------------------------------------=*
49 * Enable/disable debug message capability for your app.
52 * Enable : True/False to enable/disable debug message output.
55 * This flag/setting is a per application basis, and not a per-instance
56 * occurrence as one would expect when instantiating an instance of
57 * this class. This is by design as /everything/ is static. Caveat
58 * Emptor. Needs to be dealt with in G.E.
60 void BNetDebug::Enable( bool Enable
)
62 g_NetDebugEnabled
= Enable
;
67 *=--------------------------------------------------------------------------=*
69 * Quiz the enable/disable status.
72 * True/false if enabled/disabled.
74 bool BNetDebug::IsEnabled( void )
76 return g_NetDebugEnabled
;
81 *=--------------------------------------------------------------------------=*
83 * If enabled, spew forth a debug message.
86 * msg : The message to print.
89 * * Basically a no-op if not enabled.
90 * * We're inheriting R5 (and Nettle's) behavior, so...
91 * * The output is always "debug: msg\n" Yes, kids, you read it right;
92 * you get a newline whether you want it or not!
93 * * If msg is empty, you get "debug: \n"
94 * * Message is always printed on stderr. Redirect accordingly.
96 void BNetDebug::Print( const char* msg
)
98 if ( !g_NetDebugEnabled
)
104 fprintf( stderr
, "debug: %s\n", msg
);
109 *=--------------------------------------------------------------------------=*
111 * If enabled, spew forth a combination hex/ASCII dump of raw data.
114 * data : Data to dump.
115 * size : How many bytes of data to dump.
116 * title : Title to display in message header.
119 * * Basically a no-op if not enabled.
120 * * We're inheriting R5 (and Nettle's) behavior, so...
121 * * The output is always "debug: msg\n" Yes, kids, you read it right;
122 * you get a newline whether you want it or not!
123 * * If msg is empty, you get "debug: \n"
124 * * Behavior is undefined if data or title is NULL. This is a
125 * possible design flaw in Nettle/R5.
126 * * Do not expect an output like this:
127 * 00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF | abcdefghijklmnop
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 * because you ain't gettin' it. You will get a complete hex dump,
132 * /followed/ by an ASCII dump. More Glass Elevator stuff.
133 * * Nettle dumps to stdOUT, the BeBook says all output goes to stdERR, so
134 * the author chose to...
135 * * always print to stdERR. Redirect accordingly.
136 * * stderr is flushed after the dump is complete to keep things
137 * reasonably cohesive in appearance. This might be an expensive
138 * operation so use judiciously.
140 void BNetDebug::Dump(const char* data
, size_t size
, const char* title
)
143 if ( ! g_NetDebugEnabled
)
146 fprintf( stderr
, "----------\n%s\n(dumping %ld bytes)\n",
147 title
? title
: "(untitled)", size
);
150 fprintf(stderr
, "NULL data!\n");
153 char text
[96]; // only 3*16 + 3 + 16 max by line needed
154 uint8
*byte
= (uint8
*) data
;
157 for ( i
= 0; i
< size
; i
+= 16 ) {
160 for ( j
= i
; j
< i
+ 16 ; j
++ ) {
162 sprintf(ptr
, "%02x ", byte
[j
]);
171 for (j
= i
; j
< size
&& j
< i
+ 16;j
++) {
172 if ( byte
[j
] >= 0x20 && byte
[j
] < 0x7e )
181 fprintf(stderr
, text
);
184 fprintf( stderr
, "----------\n" );
189 /*=------------------------------------------------------------------- End -=*/