summaryrefslogtreecommitdiff
path: root/TAO/tao/debug.cpp
blob: 5f27724798847ddc358f37990a615480c53f47be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// @ (#)debug.cpp	1.3 95/10/02
// Copyright 1994-1995 by Sun Microsystems Inc.
// All Rights Reserved
//
// ORB:		Simple debug/trace support
//
// THREADING NOTE: the global values here  (debug_{level,filter,stream)
// are assumed to be modified "safely", e.g. in the main thread as
// part of process initialization.  They are treated as immutable
// values through all of this debuging package.
//
// XXX on Windows, make it always use OutputDebugString () instead of stdio

#include "tao/corba.h"

#if !defined (ACE_HAS_PTHREADS)		// _POSIX_THREAD_SAFE_FUNCTIONS implied
#define	flockfile(f)
#define funlockfile(f)
#endif /* ACE_HAS_PTHREADS */

u_int TAO_Export TAO_debug_level	= 0;
char * TAO_Export TAO_debug_filter	= "l";

static FILE *debug_stream = stderr;

void use_debug_stream_to_get_rid_of_warning (void) {ACE_UNUSED_ARG (debug_stream);}
// Dummy function to get rid of "'debug_stream' defined but not used" warning

// The rest of this file is not needed without -DDEBUG, and unless the
// vfprintf () call is available it can't work.
//
// NOTE:  some older platforms have "_doprnt" that provides much the
// same functionality ... this could be modified to use that routine
// where it's available.

#if	defined (DEBUG) && defined (HAVE_VPRINTF)

// Support for prefixing debug messages with process ID and, if
// threaded, the thread ID.  This lets messages from different sources
// be safely disentangled, even though they're interspersed in the
// output stream.
static pid_t my_pid;

#if defined (unix) || defined (VXWORKS)

#if defined (ACE_HAS_PTHREADS)

// Use POSIX initialization support to initialize just once.

static pthread_once_t debug_init = PTHREAD_ONCE_INIT;

#define setup() pthread_once (&debug_init, setup_once)

static void
setup_once ()
{
  my_pid = ACE_OS::getpid ();
}

static void
emit_prefix (FILE *stream)
{
  pthread_t self = pthread_self ();

  ACE_OS::fprintf (stream, "p%ld t%ld:  ", (long) my_pid, (long) self);
}

// !defined (ACE_HAS_PTHREADS)
#else	

// Without threads, guard initialization so it can be repeated,
// and don't emit the thread ID in the messages.

static void
setup (void)
{
  if (my_pid == 0) 
    {
      my_pid = ACE_OS::getpid ();
      // other setup goes here
    }
}

#define	emit_prefix(stream) fprintf (stream, "p%ld:  ", (long) my_pid)
#endif	/* !ACE_HAS_PTHREADS */

#elif	defined (_WIN32)

// Not all implementations of Win32 have threads, but in any case this
// code doesn't yet support Win32 threads.

static void
setup (void)
{
  if (my_pid == 0) 
    my_pid = GetCurrentProcessId ();
  // other setup goes here
}

#define	emit_prefix(stream) fprintf (stream, "p%ld:  ", my_pid)

#else

#	error "unknown OS platform"
#endif /* OS-specific initialization */

void TAO_Export
dmsg_filter (const char *_FAR categories,
	     const char *_FAR fmt,
	     ...)
{
  const char *cp = 0;

  if (!categories || !TAO_debug_filter)
    return;
  else if (*TAO_debug_filter != '*') 
    {		// filter with "*" --> all pass
      for (cp = categories; *cp; cp++)
	if (strchr (TAO_debug_filter, *cp) != 0)
	  break;

      if (!*cp)
	return;
    }

  va_list ap;

  setup ();
  flockfile (debug_stream);
  emit_prefix (debug_stream);

  switch (*cp) 
    {			// standard categories
    case 'l':		ACE_OS::fprintf (debug_stream, " (LEAK) "); break;
    }

  va_start (ap, fmt);
  vfprintf (debug_stream, fmt, ap);
  va_end (ap);
  if (strchr (fmt, '\n') == 0)
    ACE_OS::fprintf (debug_stream, "\n");
  funlockfile (debug_stream);

#if defined (_WIN32)
  OutputDebugString ("called dmsg_filter\n");	// experimental
#endif /* _WIN32 */
}

void TAO_Export
dmsg_filter (u_int level, 
	     const char *_FAR fmt, 
	     ...)
{
  if (level > TAO_debug_level)
    return;

  va_list ap;

  setup ();
  flockfile (debug_stream);
  emit_prefix (debug_stream);
  va_start (ap, fmt);
  vfprintf (debug_stream, fmt, ap);
  va_end (ap);
  if (strchr (fmt, '\n') == 0)
    ACE_OS::fprintf (debug_stream, "\n");
  funlockfile (debug_stream);

#if defined (_WIN32)
  OutputDebugString ("called dmsg_filter\n");	// experimental
#endif /* _WIN32 */
}

void TAO_Export
dmsg_v (const char *_FAR fmt, 
	...)
{
  va_list ap;

  setup ();
  flockfile (debug_stream);
  emit_prefix (debug_stream);
  va_start (ap, fmt);
  vfprintf (debug_stream, fmt, ap);
  va_end (ap);
  if (strchr (fmt, '\n') == 0)
    ACE_OS::fprintf (debug_stream, "\n");
  funlockfile (debug_stream);

#if defined (_WIN32)
  OutputDebugString ("called dmsg_v\n");	// experimental
#endif /* _WIN32 */
}

void TAO_Export
_dmsg_x (CORBA::Environment _FAR	&env,
	 const char *_FAR info)
{
  const CORBA::Exception	*ex = env.exception ();

  setup ();
  flockfile (debug_stream);
  emit_prefix (debug_stream);
  ACE_OS::fprintf (debug_stream, "exception '%s' at '%s'\n", ex->id (), info);
  if (env.exception_type () == CORBA::SYSTEM_EXCEPTION) 
    {
      CORBA::SystemException *sysex = (CORBA::SystemException *) ex;

      emit_prefix (debug_stream);
      ACE_OS::fprintf (debug_stream, "minor %#lx, completion %#lx\n",
		       sysex->minor (), (long) sysex->completion ());
    }
  funlockfile (debug_stream);

#if defined (_WIN32)
  OutputDebugString ("called _dmsg_x\n");	// experimental
#endif /* _WIN32 */
}

void TAO_Export
dmsg_opaque (char *_FAR label,
	     u_char *_FAR buffer,
	     u_long len)
{
  setup ();
  flockfile (debug_stream);
  emit_prefix (debug_stream);
  ACE_OS::fprintf (debug_stream, "%s ", label);

  if (len == 0 || !buffer)
    ACE_OS::fprintf (debug_stream, " (empty)");
  else if (len > UINT_MAX)
    ACE_OS::fprintf (debug_stream, "Oversized opaque data:  %ld bytes", len);
  else 
    {
      u_int i;

      for (i = 0; i < len; i++)
	if (!isprint (buffer [i]))
	  break;
	
      if (i < len) 
	{
	  if (len >= 20)
	    ACE_OS::fprintf (debug_stream, "%ld bytes binary data", len);
	  else 
	    {
	      ACE_OS::fprintf (debug_stream, "binary data {%2X", buffer [0]);
	      for (i = 1; i < len; i++)
		ACE_OS::fprintf (debug_stream, ", %2x", buffer [i]);
	      ACE_OS::fprintf (debug_stream, "}");
	    }
	} 
      else 
	{
	  if (len >= 50)
	    ACE_OS::fprintf (debug_stream, "%ld bytes string data", len);
	  else
	    ACE_OS::fprintf (debug_stream, "string data { \"%.*s\" }",
 (int) len, buffer);
	}
    }
  ACE_OS::fprintf (debug_stream, "\n");
  funlockfile (debug_stream);

#if defined (_WIN32)
  OutputDebugString ("called dmsg_opaque\n");	// experimental
#endif /* _WIN32 */
}

void TAO_Export
dmsg_opaque_full (char *_FAR label,
		  const u_char *_FAR buffer,
		  u_long len)
{
  setup ();
  flockfile (debug_stream);

  emit_prefix (debug_stream);
  ACE_OS::fprintf (debug_stream, "%s ", label);

  if (len == 0 || !buffer)
    ACE_OS::fprintf (debug_stream, " (empty)");
  else 
    {
      u_int i;

      for (i = 0; i < len; i++)
	{
	  if (i == 0)
	    ACE_OS::fprintf (debug_stream, "\nhex:   ");
	  else if ((i % 32) == 0)
	    ACE_OS::fprintf (debug_stream, "\n       ");
	  else if ((i % 4) == 0)
	    ACE_OS::fprintf (debug_stream, " ");
	  ACE_OS::fprintf (debug_stream, "%02x", buffer[i]);
	}

      for (i = 0; i < len; i++)
	{
	  if (i == 0)
	    ACE_OS::fprintf (debug_stream, "\nchars: ");
	  else if ((i % 32) == 0)
	    ACE_OS::fprintf (debug_stream, "\n       ");
	  else if ((i % 4) == 0)
	    ACE_OS::fprintf (debug_stream, " ");
	  ACE_OS::fprintf (debug_stream, "%c ",
			   (isprint (buffer[i]) ? buffer[i] : '?'));

	}

      ACE_OS::fprintf (debug_stream, "\n");
    }
  ACE_OS::fprintf (debug_stream, "\n");
  funlockfile (debug_stream);

#if defined (_WIN32)
  OutputDebugString ("called dmsg_opaque_full\n");	// experimental
#endif /* _WIN32 */
}

#endif /* DEBUG && HAVE_VPRINTF */