Debian Codegraph @

prim/tc3/libsrc/trace.c

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