Debian Codegraph @

libsrc/tw/trace.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2005 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29 .TYPE Module
30 .IDENTIFICATION trace.c
31 .AUTHOR Alan Richmond [ST-ECF], Francois Ochsenbein [ESO]
32 .LANGUAGE C
33 .KEYWORDS Program Monitoring
34 .ENVIRONMENT Minimal Implementation (no control_C)
35 .COMMENTS
36 This set of routines is a tool for debugging and session log,
37 and is connected to the TRACE_... and ERR_... macros defined
38 in trace.h.
39 \begin{TeX}
40 \\
41 The initialisation is done through {\it pm\_open}
42 (specification of the log file name), and
43 {\it pm\_level} (specification of the level(s) to trace).
44 By default, {\em stderr} (file 2) is assumed as the log file, and
45 only levels 0 and 1 are logged.
46
47 The level is a user-supplied positive number, with number 0
48 reserved for {\it error messages} (always logged), and number 1
49 in principle used for {\it warnings} or debugging purposes.
50
51 Provided that {\em pm\_enter} {\em and pm\_$x$exit}
52 (or the ENTER and EXIT$x$ macros)
53 are called at the beginning and end of each C function,
54 a complete trace of the calling sequence is stacked.
55
56 If the level is 0, the text {\tt "****"} followed by the name of the
57 function calling the pm\_... routine is logged;
58 if the level is 1, the text {\tt "++++"} precedes the message.
59
60 The present functions also allow to start the tracing as soon as
61 one of the functions specified by {\em pm\_fetch} is called.
62 The {\em pm\_unfetch} function removes a function from the list
63 of functions to fetch.
64
65 The following operations can be performed with the present set of routines:
66
67 \begin{itemize}
68 \item Initialize: {\it pm\_open, pm\_level, pm\_set\_depth}
69
70 \item Define / delete function names where tracing should start:
71 {\it pm\_fetch, pm\_unfetch}
72
73 \item Log starting / ending point of a C function:
74 {\it pm\_enter} (ENTER macro),
75 {\it pm\_iexit}, {\it pm\_lexit}, {\it pm\_pexit} (EXIT$x$ macros).\\
76 The first character of the argument of the {\em ENTER} macro must be:\\
77 {\tt *} for a {\em function returning a pointer}, \\
78 {\tt +} for a {\em function returning a length or an index} (int),\\
79 {\tt .} for a {\em function returning a value} (long int),\\
80 and no prefix for a {\em function returning a status} (int).
81
82 \item Log the complete calling sequence: {\it pm\_history}
83
84 \item Trace a text or a number: {\it pm\_trace} (write a text),
85 {\em pm\_tr2}, {\it pm\_ed\_trace}, {\it pm\_ed\_tr2},
86 and {\it pm\_ed\_i}. The corresponding text is only written
87 to the log file if the tracing of the function is on.
88 Non-printable characters are logged as the hexadecimal representation
89 within braces.
90
91 \item Issue a text onto the log file: {\em pm\_log} and {em pm\_comment}.
92
93 \item close the log file: {\it pm\_close}
94 \end{itemize}
95
96 \end{TeX}
97
98 .VERSION 2.0 11-Jun-1987: Creation: extraction from original PM_TRACE.
99 .VERSION 3.0 25-May-1988: Added LOG possibility for
100 all functions, with a (-1) level.
101 .VERSION 3.1 25-Nov-1988: pm_ed_tr2 possible with NULL string...
102 .VERSION 3.2 21-May-1989: Added pm_get_level, to return current
103 maximum trace level. M. Albrecht [ESIS]
104 .VERSION 3.3 29-May-1989: Added pm_islev which checks is a defined level
105 is set or not.
106 .VERSION 3.4 29-Jun-1989: If NOTRACING defined, minimize code.
107 .VERSION 3.5 06-Oct-1989: pm_lfile returns the logfile
108 .VERSION 3.6 25-Oct-1989: Be sure there is no crash for NULL pointers...
109 .VERSION 3.7 28-Mar-1990: Definition of COMMENT_CHAR is in <ok.h>
110
111 051019 last modif
112 -----------------------------------------------------------------------------*/
113
114 #define NOTRACING /* CG. Default */
115
116 #ifdef NOTRACING
117 #define TRACING 0
118 #define SW_LEVEL 0
119 #endif
120
121 #include <ok.h> /* Defines COMMENT_CHAR */
122 #include <osdefos.h> /* Need to know the OS */
123 #include <macrogen.h>
124 #include <atype.h>
125 #include <osfile.h> /* Need to use file i/o */
126 #include <string.h>
127
128 #if SW_LEVEL
129 #define MAX_DEPTH 64 /* Maximum allowed depth */
130 #define MAX_FETCH 32 /* Maximum number of functions to fetch */
131 #else
132 #define MAX_DEPTH 16 /* Maximum allowed depth */
133 #define MAX_FETCH 8
134 #endif
135
136 #define iffct(f) if(!(isalpha(*f)) && (*f != '_'))
137
138 #define INDENT 32 /* The position to indent */
139 #define INDENTA INDENT+16
140
141 #define LEVELS 32 /* The number of levels */
142 #define LINESIZE 132 /* Maximum size of a line */
143 #define TOO_DEEP "too_deep!"
144
145 static unsigned char confidential = 0;
146 /* Specifies condifential data, NOT TO LOG */
147 static char line[LINESIZE+5];
148 static int log_file = 2; /* Descriptor for monitor file */
149 static unsigned char stream[LEVELS] = /* Monitor streams */
150 {1 ,1 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
151
152 static int call_depth=0; /* Known calling depth */
153 static char init_pgm[] = "";
154
155 static int max_depth = MAX_DEPTH; /* The max logging depth */
156 static int old_depth = MAX_DEPTH; /* The max logging depth */
157 static int stop_depth = -1; /* The depth to stop logging */
158 static char *name_ptr[MAX_DEPTH] = /* Stack of routine names */
159 {&init_pgm[0],&init_pgm[0]};
160 static unsigned char pgm_level[MAX_DEPTH] = /* Stack of program levels */
161 {0,0};
162 static char *void_string = "";
163
164
165 static unsigned char xlog = 1; /* Flag to start/stop logging */
166
167 static int nfetch = 0;
168 #if SW_LEVEL /* Fetching only for full implementation */
169 static int fetch_list[MAX_FETCH] = {0, 0};
170 static int fetch_depth[MAX_FETCH] = {0, 0};
171 static char fetch_buffer[12*MAX_FETCH] = "";
172 static int cfetch = 0;
173 #endif
174
175 static char err_depth[] = "!***Monitoring Depth is negative !\n";
176 MID_RSTATIC char xdigit[] = {'0','1','2','3','4','5','6','7','8','9',
177 'A','B','C','D','E','F'};
178 MID_RSTATIC char null_pointer[] = "<NuLL>"; /* Null Pointer */
179
180 #define FINISH goto FIN
181 #define RETURN(x) FIN: return(x)
182 #define CheckLevel(lev) if(lev >= 0) if_not( (stream[lev]) && \
183 (call_depth <= max_depth)) FINISH
184
185 #if (MID_OS_VMS||MID_OS_MSDOS)
186 #define Flush() if (log_file > 2) osdflush(log_file)
187 #else
188 #define Flush()
189 #endif
190
191 #ifdef Write
192 #undef Write
193 #endif
194 #ifdef WriteText
195 #undef WriteText
196 #endif
197 #ifdef WriteString
198 #undef WriteString
199 #endif
200 #define WriteText(t) Write(t, sizeof(t)-1)
201 #define WriteString(t) Write(t, strlen(t))
202
203 /*==========================================================================*/
204 static int Write(text, len)
205 /*+++
206 .PURPOSE Write text == Just don't write anything when log_file = 0
207 .RETURNS len
208 --------*/
209 char *text; /* IN: Text to write */
210 int len; /* IN: Length of Text */
211 {
212 if(log_file) osdwrite(log_file, text, len);
213 return(len);
214 }
215
216 /*==========================================================================*/
217 static int pm_ed_begin (level,text,length,ch)
218 /*+++
219 .PURPOSE Insert the indentation in the "line" buffer if level>1, followed by the
220 text. This function is used by other pm_... routines
221 .RETURNS the length of line after insertion
222 .REMARKS If level=0, the text "****" is added.
223 .REMARKS If level=1, the text "++++" is added.
224 .REMARKS If level<0, the text "====" is added.
225 .REMARKS Modification 28-May-1986: Insert COMMENT_CHAR comment flag.
226 --------*/
227 int level; /* IN: Module level */
228 char *text; /* IN: Text to insert */
229 int length; /* IN: Length of text */
230 char ch; /* IN: Character used for indentation */
231 {
232 int i, j;
233 char *p;
234 char flag;
235
236 i = 0; line[i++] = COMMENT_CHAR; /* Comment flag */
237 if (level>1) /* Insert indentation */
238 { for (j=call_depth; --j>=0 ; )
239 { if (i >= (LINESIZE-20)) break;
240 line[i++] = ' ';
241 line[i++] = ch ;
242 }
243 }
244 else /* Insert flags */
245 { flag = (level == 0 ? '*' : (level > 0 ? '+' : '='));
246 for (j=3; --j>=0; ) line[i++] = flag; /* Add "****" */
247 p = (call_depth < MAX_DEPTH ? name_ptr[call_depth] : TOO_DEEP);
248 if (*p)
249 { while (*p) line[i++] = *(p++); /* Move entry_name */
250 line[i++] = ':'; line[i++] = ' ';
251 while (i<INDENT) line[i++] = ' ';
252 }
253 }
254
255 for (p=text, j=length; --j>=0; ) /* Insert text */
256 { if (i >= LINESIZE) break;
257 line[i++] = *(p++);
258 }
259
260 return(i);
261 }
262
263 /*==========================================================================*/
264 static int pm__line (text, length, index)
265 /*+++
266 .DES Copies text of length l to buffer "line" starting at position index,
267 and writes the result to log_file. Used by other pm_... routines.
268 .RET Length of line (just before the '\n' character)
269 .REM Non-printable characters are replaced by ``<hexa>''.
270 If in confidential state, DON'T LOG sensible data.
271 -------*/
272 char *text; /* IN: Address of input text */
273 int length; /* IN: Length of input text */
274 int index; /* IN: Starting index within line */
275 {
276 register int l,i;
277 register char *p;
278 static char sensible_data[] = "<<You'll not know the password...>>";
279
280 l = length;
281 i = index;
282 p = text;
283
284 if (!p) p = null_pointer, l = sizeof(null_pointer)-1;
285 if(confidential) p = sensible_data, l = sizeof(sensible_data)-1;
286
287 while (--l>=0)
288 { if (i >= LINESIZE)
289 { line[i++] = '\n';
290 Write (line, i);
291 i = index;
292 }
293 if (isprint(*p)) line[i++] = *(p++);
294 else
295 { line[i++] = '<';
296 line[i++] = xdigit[((*p)&0xf0)>>4]; /* 1st hexa dig */
297 line[i++] = xdigit[*(p++)&15]; /* 2nd hexa dig */
298 line[i++] = '>';
299 }
300 }
301
302 if ( (i != index) || (length == 0) )
303 { line[i] = '\n';
304 Write (line, i+1);
305 }
306
307 return(i);
308 }
309
310 static int pm__copy (text, length, index)
311 /*+++
312 .DES Copies text of length l to buffer "line" starting at position index.
313 .RET Length of line
314 .REM Non-printable characters are replaced by ``<hexa>''.
315 If in confidential state, DON'T LOG sensible data.
316 -------*/
317 char *text; /* IN: Address of input text */
318 int length; /* IN: Length of input text */
319 int index; /* IN: Starting index within line */
320 {
321 register int l,i;
322
323 l = length;
324 i = index;
325
326 while (--l>=0)
327 { if (i >= LINESIZE) break;
328 if (isprint(*text)) line[i++] = *(text++);
329 else
330 { line[i++] = '<';
331 line[i++] = xdigit[((*text)&0xf0)>>4]; /* 1st hexa dig */
332 line[i++] = xdigit[*(text++)&15]; /* 2nd hexa dig */
333 line[i++] = '>';
334 }
335 }
336 return(i);
337 }
338
339 /*==========================================================================*/
340 static int edval (x, i)
341 /*+++
342 .PURPOSE Edit the long integer number
343 .RETURNS The new index in line
344 .REMARKS
345 --------*/
346 long int x; /* IN: Number ot edit */
347 int i; /* IN: Index in line */
348 {
349 register int k;
350 register long int j;
351 static char edbuf[12] = " ";
352
353 j = ABSOLUTE(x);
354
355 for (k = sizeof(edbuf); ;)
356 { edbuf[--k]= xdigit[j%10];
357 j /= 10;
358 if (j == 0) break;
359 }
360 if (x < 0) edbuf[--k] = '-';
361
362 return(pm__copy(&edbuf[k], sizeof(edbuf)-k, i));
363 }
364
365 /*==========================================================================*/
366 static int pm__exit (level,status,flag)
367 /*+++
368 .PURPOSE Print on the log file the status (in decimal), followed by the
369 calling routine name
370 .RETURNS 0
371 .REMARKS The text is preceded by an explicitation of the returned number.
372 --------*/
373 int level; /* IN: Module level */
374 long int status; /* IN: Completion code being returned */
375 char flag; /* IN: Kind of returned value to check */
376 {
377 register int i;
378 register char *f, *type;
379 char mismatch;
380 unsigned char old_conf;
381 static char bad[] = " **** Mismatch ENTER / EXIT **** ";
382
383 call_depth++;
384 f = (call_depth < MAX_DEPTH ? name_ptr[call_depth] : TOO_DEEP);
385 call_depth--;
386 old_conf = confidential, confidential = 0;
387
388 mismatch = 0;
389 switch(*f)
390 { case '*': /* Returns Pointer */
391 mismatch = *f - flag;
392 type = " .Ptr="; f++; break;
393 case '+': /* Returns Length */
394 type = " .Len="; f++; break;
395 case '.': /* Returns Value */
396 mismatch = *f - flag;
397 type = " .Val="; f++; break;
398 default: /* Returns Status */
399 type = " .Sta=";
400 }
401
402 f = (call_depth < MAX_DEPTH ? name_ptr[call_depth] : TOO_DEEP);
403 iffct(f) f++;
404 i = pm_ed_begin(level,type, 6, ' ');
405 i = edval(status, i);
406 if (mismatch) i = pm__copy(bad, sizeof(bad)-1, i);
407 /* Alignement in log_file changed for comments */
408 while (i<INDENTA) line[i++] = ' ';
409 if (i < LINESIZE-4)
410 line[i++] = '=', line[i++] = '=', line[i++] = '=', line[i++] = '>';
411 pm__line(f, strlen(f), i);
412 Flush();
413 /* Suppress logging if stop_depth is encountered */
414 if (call_depth == stop_depth)
415 { max_depth = old_depth;
416 for (i = LEVELS; --i > 0; ) stream[i] &= ~2;
417 }
418
419 confidential = old_conf;
420 return(0);
421 }
422
423 /*==========================================================================*/
424 static int pm_function (level,function,index)
425 /*+++
426 .PURPOSE Writes on the "line" buffer at offset index, the function name
427 followed by the level and the number of arguments
428 .RETURNS 0
429 --------*/
430 int level; /* IN: The level */
431 char *function; /* IN: EOS-terminated Function name */
432 int index; /* IN: starting offset in line */
433 {
434 register int i;
435 register long int x;
436 register char *f;
437 unsigned char old_conf;
438
439 f = function;
440 iffct(f) f++;
441
442 i = index, x = level;
443 line[i++] = '[';
444 i = edval(x, i);
445 line[i++] = ']', line[i++] = ' ';
446
447 while (*f != EOS) line[i++] = *(f++);
448 old_conf = confidential, confidential = 0;
449 pm__line(void_string, 0, i);
450 confidential = old_conf;
451
452 return(0);
453 }
454
455 /*==========================================================================*/
456 /* ARGSUSED */
457 static int pm__look (level,fct)
458 /*+++
459 .PURPOSE Check if the subroutine name should start tracing.
460 .RETURNS 0
461 .REMARKS In minial implementation, returns always 0.
462 --------*/
463 int level; /* IN: Module level */
464 char *fct; /* IN: Function name */
465 {
466 #if SW_LEVEL
467 register int i, j, l;
468 static char starting[] = "Start Tracing: ";
469
470 iffct(fct) fct++;
471 l = strlen(fct) + 1;
472 for (i=nfetch; --i >=0; )
473 if (oscomp(fct, fetch_buffer + fetch_list[i], l) == 0) break;
474
475 if (i >= 0)
476 { j = pm_ed_begin(level, starting, sizeof(starting)-1, '.');
477 pm_function(level, fct, j);
478 pm_history();
479 old_depth = pm_set_depth(call_depth +
480 ( fetch_depth[i] > 0 ? fetch_depth[i] : 0));
481 stop_depth = call_depth - 1 +
482 ( fetch_depth[i] < 0 ? fetch_depth[i] : 0);
483 for (j=LEVELS; --j > 0; ) stream[j] |= 2;
484 }
485 #endif
486
487 return(0);
488 }
489
490 /*==========================================================================
491 Public Routines
492 *==========================================================================*/
493 int pm_lfile ()
494 /*+++
495 .PURPOSE Get the currently opened log file
496 .RETURNS The file number (2 when log file not opened)
497 .REMARKS
498 --------*/
499 {
500 return(log_file);
501 }
502
503 /*==========================================================================*/
504
505 int pm_get_depth ()
506 /*+++
507 .PURPOSE Get the current calling depth
508 .RETURNS The current depth
509 .REMARKS Useful for recursive functions...
510 --------*/
511 {
512 return(call_depth);
513 }
514
515 /*==========================================================================*/
516 int pm_get_level ()
517 /*+++
518 .PURPOSE Get the current maximum trace level
519 .RETURNS The maximum trace level currently in use
520 --------*/
521 {
522 int i; /* The returned level */
523
524 for (i = LEVELS; !stream[--i]; ) ;
525
526 return(i);
527 }
528
529 /*==========================================================================*/
530 int pm_islev (lev)
531 /*+++
532 .PURPOSE Check is the specified level is on or off
533 .RETURNS 1 (level is ON) / 0 (level is OFF)
534 .REMARKS A negative argument means `all levels UNTIL |lev|'
535 --------*/
536 int lev; /* IN: Level to check */
537 {
538 int i; /* The returned level */
539
540 i = ABSOLUTE(lev);
541 if (i >= LEVELS) return(0);
542
543 if (lev >= 0) return(stream[i]); /* Positive Value */
544
545 while ((i > 0) && stream[i--]) ; /* Negative Value */
546 return(i == 0 ? 1 : 0);
547 }
548
549 /*==========================================================================
550 Initialisations
551 *==========================================================================*/
552 int pm_sensible (k)
553 /*+++
554 .PURPOSE Switch on / off the data confidentiality, i.e. don't log
555 confidential data.
556 .RETURNS The previous confidentiality state
557 .REMARKS
558 --------*/
559 int k; /* IN: Zero to switch off confidentiality, not-zero \
560 to switch on */
561 {
562 register int old_conf;
563
564 old_conf = confidential;
565 confidential = (k ? 1 : 0);
566 return(old_conf);
567 }
568
569 /*==========================================================================*/
570 int pm_set_depth (d)
571 /*+++
572 .PURPOSE Define the maximal depth for tracing: if the calling
573 depth will be larger than the specified number, tracing will be
574 stopped.
575 .RETURNS The previous maximal depth
576 .REMARKS If this routine is not called, the maximum is MAX_DEPTH, defined here.
577 --------*/
578 int d; /* IN: The maximal depth allowed */
579 {
580 register int old;
581
582 old = max_depth;
583 max_depth = MIN(MAX_DEPTH, d);
584 if (max_depth < 1) max_depth = 1;
585 return(old);
586 }
587
588 #if SW_LEVEL
589 /*==========================================================================*/
590 int pm_fetch (function, d)
591 /*+++
592 .PURPOSE Define a function name where tracing should start.
593 .RETURNS The number of existing functions to fetch.
594 .REMARKS The parameter d specifies the depth, starting from the function,
595 to trace; a negative number asks to continue tracing
596 after the return from function.
597 --------*/
598 char *function; /* IN: The function name to `trap' */
599 int d; /* IN: The maximal depth to trace */
600 {
601 static char text[] = "!***Too long fetch list\n";
602 register int l;
603
604 l = strlen(function) + 1;
605 if (((l + cfetch) > sizeof(fetch_buffer)) || (nfetch >= MAX_FETCH-1))
606 text[0] = COMMENT_CHAR, WriteText(text);
607 else
608 { fetch_list[nfetch] = cfetch, fetch_depth[nfetch++] = d;
609 oscopy(&fetch_buffer[cfetch], function, l);
610 cfetch += l;
611 }
612
613 return(nfetch);
614 }
615
616 /*==========================================================================*/
617 int pm_unfetch (function)
618 /*+++
619 .PURPOSE Suppress a function name previously defined via pm_fetch.
620 .RETURNS The number of existing functions to fetch.
621 .REMARKS
622 --------*/
623 char *function; /* IN: The function name to `trap' */
624 {
625 register int l, i, j;
626
627 l = strlen(function) + 1;
628 for (i=nfetch; --i >=0; )
629 if (oscomp(function, fetch_buffer + fetch_list[i], l) == 0) break;
630
631 if (i >= 0)
632 { for (j = nfetch; --j > i; ) fetch_list[j] -= l;
633 j = i + 1; /* Copy depth list */
634 oscopy((char *)(&fetch_depth[i]), (char *)(&fetch_depth[j]),
635 (nfetch-j)*sizeof(fetch_depth[0])); /* Copy list */
636 oscopy((char *)(&fetch_list[i]), (char *)(&fetch_list[j]),
637 (nfetch-j)*sizeof(fetch_list[0]));
638 j = fetch_list[i];
639 cfetch -= l;
640 oscopy(&fetch_buffer[j], (&fetch_buffer[j])+l, /* Copy fct list */
641 cfetch - j);
642 nfetch--;
643 }
644
645 return(nfetch);
646 }
647 #endif
648
649 /*==========================================================================*/
650 int pm_open (filename,title)
651 /*+++
652 .PURPOSE Open monitoring file. If title is NULL, then open the
653 log file for APPENDING.
654 .RETURNS File number
655 .REMARKS If this function is not called, file 2 (stderr) is assumed
656 --------*/
657 char *filename; /* IN: name of monitoring log_file (NULL \
658 for dummy) */
659 char *title; /* IN: Printed in file */
660 {
661 static char text[] = "!<<<<<Continuation>>>>>>\n";
662 register int i,l;
663
664 #if MID_OS_MSDOS
665 #define OP_MODE RECORD_MODE
666 #else
667 #define OP_MODE 0
668 #endif
669
670 if_not(filename) { log_file = 0; return(0); }
671
672 if_not(title)
673 { log_file = osdopen(filename, APPEND|OP_MODE);
674 if (log_file <= 0) log_file = osdopen(filename, WRITE|OP_MODE) ;
675 text[0] = COMMENT_CHAR, WriteText(text);
676 }
677 else log_file = osdopen(filename, WRITE|OP_MODE) ;
678 if (log_file <= 0)
679 { log_file = 2;
680 WriteText("****Program Monitoring cannot open LOG file =>");
681 WriteString(filename);
682 WriteText("****\n");
683 }
684 /* Writes the title */
685 l = (title ? strlen(title): 0);
686
687 if (l > 0)
688 { line[0] = COMMENT_CHAR; /* Comment the title... */
689 l = pm__line(title,l,1); /* Copies the title */
690 for (i=1; i<l; i++) line[i] = '-'; /* Underlining the title*/
691 Write (line,l+1); /* Write Underline */
692 line[1] = '\n';
693 Write (line,2); /* Skip a line */
694 }
695
696 Flush();
697 return ( log_file );
698 }
699
700 /*==========================================================================*/
701 int pm_level(level)
702 /*+++
703 .PURPOSE Set monitoring stream: a positive value means that the specified
704 level is traced; successive calls may log several several levels.
705 A negative value asks for a tracing of levels 1 thru |level|.
706 .RETURNS The maximal level to trace.
707 .REMARKS Levels 0 and 1 are logged if this function is not called.
708 ----*/
709 int level; /* IN: monitoring depth */
710 {
711 register int i, level_max;
712
713 /* If level >=0, only this level is logged;
714 * if level <0 , levels 0 thru -level are logged */
715 level_max = ABSOLUTE(level);
716 if(level_max >= LEVELS) level_max = LEVELS-1;
717
718 if (level >= 0) stream[level] = xlog;
719 else
720 { for (i=level_max; i> 0; i--) /* Level 0 is ALWAYS reported */
721 stream[i] = xlog;
722 }
723
724 xlog = 1;
725
726 return ( level_max );
727 }
728
729 /*==========================================================================*/
730 int pm_stop(level)
731 /*+++
732 .PURPOSE Stop monitoring stream: a positive value means that the specified
733 level is not logged; successive calls may stop logging several levels.
734 A negative value asks for stop logging of levels 1 thru |level|.
735 .RETURNS The level
736 .REMARKS
737 --------*/
738 int level; /* IN: monitoring depth */
739 {
740 xlog = 0;
741
742 return ( pm_level(level) );
743 }
744
745 /*==========================================================================*/
746 int pm_close ()
747 /*+++
748 .PURPOSE Close monitoring file.
749 .RETURNS The closed file number.
750 .REMARKS Subsequent calls to pm_... routines will be written on stderr,
751 unless a new pm_open is called.
752 --------*/
753 {
754 register int status;
755
756 status = log_file;
757 if (log_file > 2) osdclose(log_file);
758 log_file = 2; /* No more log_file */
759
760 return(status);
761 }
762
763 /*==========================================================================
764 Enter / Exit / Trace
765 *==========================================================================*/
766 int pm_enter (level, function)
767 /*+++
768 .PURPOSE Print entry-point string supplied in monitoring file (ENTER macro)
769 .RETURNS The current depth
770 .REMARKS The function name is stacked.
771 --------*/
772 int level; /* IN: Module level */
773 char *function; /* IN: text to print in monitor file */
774 {
775 register int i;
776
777 #if MID_OS_MSDOS
778 unsigned int stackavail();
779 if (stackavail() < 400) pm_ed_i(1, "Stack only: ", stackavail());
780 #endif
781
782 if (++call_depth < MAX_DEPTH)
783 name_ptr[call_depth] = function, pgm_level[call_depth] = level;
784
785 if ((stream[level]) && (call_depth <= max_depth))
786 { i = pm_ed_begin(level, NULL_PTR(char),0,'.'); /* Indents the line */
787 line[--i] = '>';
788 i++;
789 pm_function(level, function, i);
790 Flush();
791 }
792 else if (nfetch) pm__look(level, function);
793
794 return(call_depth);
795 }
796
797 /*==========================================================================*/
798 int pm_iexit (level,status)
799 /*+++
800 .PURPOSE Print on the log file the status (in decimal), followed by the
801 calling routine name
802 .RETURNS The status
803 .REMARKS The text is preceded by an explicitation of the returned number.
804 --------*/
805 int level; /* IN: Module level */
806 int status; /* IN: Completion code being returned */
807 {
808 register long int x;
809
810 if (call_depth > 0) call_depth--;
811 else err_depth[0] = COMMENT_CHAR, WriteText(err_depth);
812
813 if ((stream[level]) && (call_depth < max_depth))
814 { x = status;
815 pm__exit(level, x, EOS);
816 }
817
818 return(status);
819 }
820
821 /*==========================================================================*/
822 long int pm_lexit (level,status)
823 /*+++
824 .PURPOSE Print on the log file the status (in decimal), followed by the
825 calling routine name
826 .RETURNS The status
827 .REMARKS The text is preceded by an explicitation of the returned number.
828 --------*/
829 int level; /* IN: Module level */
830 long int status; /* IN: Completion code being returned */
831 {
832
833 if (call_depth > 0) call_depth--;
834 else err_depth[0] = COMMENT_CHAR, WriteText(err_depth);
835
836 if ((stream[level]) && (call_depth < max_depth))
837 pm__exit(level, status, '.');
838
839 return(status);
840 }
841
842 /*==========================================================================*/
843 char *pm_pexit (level,status)
844 /*+++
845 .PURPOSE Print on the log file the status (in decimal), followed by the
846 calling routine name
847 .RETURNS Status
848 .REMARKS The text is preceded by an explicitation of the returned number.
849 --------*/
850 int level; /* IN: Module level */
851 char *status; /* IN: Completion code being returned */
852 {
853 register long int x;
854
855 if (call_depth > 0) call_depth--;
856 else err_depth[0] = COMMENT_CHAR, WriteText(err_depth);
857
858 if ((stream[level]) && (call_depth < max_depth))
859 { x = (long int)status;
860 pm__exit(level, x, '*');
861 }
862
863 return(status);
864 }
865
866 /*==========================================================================*/
867 int pm_history ()
868 /*+++
869 .PURPOSE Log the complete stack
870 .RETURNS The current depth
871 ________*/
872 {
873 register int i,id;
874 static char call_text[] = "...Called from ";
875
876 for (i=call_depth-1; i>0; i--)
877 { if (i >= MAX_DEPTH) id = pm_ed_begin(0, TOO_DEEP,
878 sizeof(TOO_DEEP)-1, ' ');
879 else
880 { id = pm_ed_begin((int)pgm_level[i], call_text,
881 sizeof(call_text)-1, ' ');
882 pm_function((int)pgm_level[i], name_ptr[i], id);
883 }
884 }
885 Flush();
886 return(call_depth);
887 }
888
889 /*==========================================================================*/
890 int pm_trace (level,text)
891 /*+++++++++
892 .PURPOSE Print EOS-terminated string supplied in monitoring file,
893 with indentation
894 .RETURNS The current depth
895 --------*/
896 int level; /* IN: Module level */
897 char *text; /* IN: text to print in monitor file */
898 {
899 int l;
900
901 l = text ? strlen(text) : 0;
902
903 return(pm_tr2(level, text, l));
904 }
905
906 /*==========================================================================*/
907 int pm_tr2 (level, text, length)
908 /*+++
909 .PURPOSE Print string supplied in monitoring file, with indentation.
910 .RETURNS The current depth
911 --------*/
912 int level; /* IN: Module level */
913 char *text; /* IN: text to print in monitor file */
914 int length; /* IN: Length of text */
915 {
916 register int i;
917
918 CheckLevel(level);
919
920 i = pm_ed_begin(level,NULL_PTR(char),0,' '); /* Indent line */
921 pm__line(text, length, i);
922 Flush();
923
924 RETURN(call_depth);
925 }
926
927 /*==========================================================================*/
928 int pm_ed_trace (level,text,string)
929 /*+++++++++
930 .PURPOSE Writes on the log-file text followed by string.
931 .RETURNS The current depth
932 --------*/
933 int level; /* IN: Module level */
934 char *text; /* IN: Text to insert */
935 char *string; /* IN: EOS-terminated string to insert */
936 {
937 int l;
938
939 l = (string ? strlen(string) : 0);
940 return(pm_ed_tr2(level,text,string, l));
941 }
942
943 /*==========================================================================*/
944 int pm_ed_tr2 (level, text, string, length)
945 /*+++
946 .PURPOSE Writes on the log-file text followed by string.
947 .RETURNS The current depth
948 --------*/
949 int level; /* IN: Module level */
950 char *text; /* IN: Text to insert */
951 char *string; /* IN: String to insert */
952 int length; /* IN: length of string */
953 {
954 register int i;
955
956 CheckLevel(level);
957
958 i = pm_ed_begin(level,text,strlen(text),' ');
959 pm__line(string,length,i);
960 Flush();
961
962 RETURN(call_depth);
963 }
964
965 /*==========================================================================*/
966 int pm_ed_i (level, text, value)
967 /*++++++++++
968 .PURPOSE Writes on the log-file text followed by a number (in decimal)
969 .RETURNS The current depth
970 --------*/
971 int level; /* IN: Module level */
972 char *text; /* IN: EOS-terminated Text to insert*/
973 int value; /* IN: value to edit */
974 {
975 register int i;
976 register long int x;
977
978 CheckLevel(level);
979
980 i = pm_ed_begin(level, text, strlen(text), ' ');
981 x = value;
982 i = edval(x, i);
983 pm__line(void_string, 0, i);
984 Flush();
985
986 RETURN(call_depth);
987 }
988
989 /*==========================================================================*/
990 /* Insert text in Log File */
991 /*==========================================================================*/
992 int pm_log (string)
993 /*+++
994 .PURPOSE Print EOS-terminated string supplied in monitoring file,
995 without indentation
996 .RETURNS The current depth
997 ---------*/
998 char *string; /* IN: text to print in monitor file */
999 {
1000 register int l;
1001
1002 l = (string ? strlen(string) : 0);
1003
1004 pm__line(string, l, 0);
1005 Flush();
1006
1007 return(call_depth);
1008 }
1009
1010 /*==========================================================================*/
1011 int pm_comment (string)
1012 /*+++
1013 .PURPOSE Print EOS-terminated string supplied in monitoring file,
1014 preceded by a comment flag COMMENT_CHAR, without indentation.
1015 .RETURNS The current depth
1016 --------*/
1017 char *string; /* IN: text to print in monitor file */
1018 {
1019 register int l;
1020
1021 line[0] = COMMENT_CHAR; /* Comment flag */
1022 l = (string ? strlen(string) : 0);
1023
1024 pm__line(string, l, 1);
1025 Flush();
1026
1027 return(call_depth);
1028 }
1029