home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / k / ksh48.zip / sh / trace.c < prev    next >
C/C++ Source or Header  |  1992-05-03  |  3KB  |  166 lines

  1. /* NAME:
  2.  *      trace.c - a simple trace facility
  3.  *
  4.  * SYNOPSIS:
  5.  *      TRACE(level, (fmt [, ...]));
  6.  *
  7.  * DESCRIPTION:
  8.  *      This module provides a simple trace facility via
  9.  *      a call to checkpoint() which opens a log file and writes
  10.  *      an entry and closes the log (so that process crashes
  11.  *      won't destroy the log :-).  checkpoint() takes options
  12.  *      like printf().  If Trace_log is not initialized then
  13.  *      stderr is used.
  14.  *
  15.  *      The header file trace.h defines a macro TRACE() which
  16.  *      is useful in that it allows checkpoint to be called
  17.  *      based on the value of Trace_level, and the calls can be
  18.  *      eliminated by undefining USE_TRACE.
  19.  *
  20.  * RETURN VALUE:
  21.  *      None.
  22.  *
  23.  * FILES:
  24.  *      None.
  25.  *
  26.  * SEE ALSO:
  27.  *
  28.  *
  29.  * BUGS:
  30.  *
  31.  *
  32.  * AMENDED:
  33.  *      91/11/22  22:53:56  (sjg)
  34.  *
  35.  * RELEASED:
  36.  *      91/11/22  22:54:17  v1.2
  37.  *
  38.  *      @(#)Copyright (c) 1990 Simon J. Gerraty.
  39.  */
  40. #ifdef USE_TRACE
  41.  
  42. #ifndef lint
  43. static char  sccs_id[] = "@(#)trace.c     1.2  91/11/22  22:53:56  (sjg)";
  44. #endif
  45.  
  46. /* include files */
  47. #include <stdio.h>
  48. #ifdef __STDC__
  49. # include <stdlib.h>
  50. #endif
  51.  
  52. #define EXTERN
  53. #include "trace.h"
  54. #undef EXTERN
  55.  
  56. /* some useful #defines */
  57. #ifndef ENTRY
  58. # define ENTRY
  59. # define LOCAL static
  60. # define BOOLEAN int
  61. #endif
  62. #ifndef TRUE
  63. # define TRUE  1
  64. # define FALSE 0
  65. #endif
  66. #ifndef ARGS
  67. # if defined(__STDC__) || defined(PROTO)
  68. #   define ARGS(p) p
  69. # else
  70. #   define ARGS(p) ()
  71. # endif
  72. #endif
  73.  
  74.  
  75. /* NAME:
  76.  *      checkpoint - write a logfile entry
  77.  *
  78.  * SYNOPSIS:
  79.  *      checkpoint(fmt, ...)
  80.  *
  81.  * DESCRIPTION:
  82.  *      This function takes a variable number or args
  83.  *      like the printf(3S) family of functions.
  84.  *
  85.  * RETURN VALUE:
  86.  *      None
  87.  */
  88. extern char * _CDECL strdup    ARGS((char *s));
  89.  
  90. #ifdef __STDC__
  91. # include <stdarg.h>
  92.  
  93. ENTRY void _CDECL
  94. checkpoint(fmt)
  95.     char *fmt;
  96. {
  97.   int c;
  98.   va_list arg_ptr;
  99.   FILE *fp;
  100.   register char *rcp;
  101.   char  *mode;
  102.   static  setup;
  103.  
  104.   va_start(arg_ptr, fmt);
  105. #else  /* __STDC__ */
  106. # include <varargs.h>
  107.  
  108. ENTRY void _CDECL
  109. checkpoint(va_alist)
  110.   va_dcl
  111. {
  112.   extern char *getenv    ARGS((char *var));
  113.   char *fmt;
  114.   int c;
  115.   va_list arg_ptr;
  116.   FILE *fp;
  117.   register char *rcp;
  118.   char  *mode;
  119.   static  setup;
  120.  
  121.   va_start(arg_ptr);
  122.   fmt = va_arg(arg_ptr, char *);
  123. #endif /* __STDC__ */
  124.  
  125.   /* 42 is a "magic" number */
  126.   if (setup == 42)
  127.     mode = "a";
  128.   else
  129.   {
  130.     if (Trace_level == 0 && (rcp = getenv("TRACE_LEVEL")))
  131.       Trace_level = atoi(rcp);
  132.     if (Trace_log == NULL || *Trace_log == '\0')
  133.     {
  134.       if (rcp = getenv("TRACE_LOG"))
  135.     Trace_log = strdup(rcp);
  136.       else
  137.     Trace_log = NULL;
  138.     }
  139.     setup = 42;
  140.     mode= "w";
  141.   }
  142.   if (Trace_log)
  143.     fp = fopen(Trace_log, mode);
  144.   else
  145.     fp = stderr;
  146.   if (fp != (FILE *)NULL)
  147.   {
  148.     vfprintf(fp, fmt, arg_ptr);
  149.     fputc('\n', fp);
  150.     if (fp == stderr)
  151.       fflush(fp);
  152.     else
  153.       fclose(fp);
  154.   }
  155. }
  156.  
  157. #endif /* USE_TRACE */
  158.  
  159. /* This lot (for GNU-Emacs) goes at the end of the file. */
  160. /*
  161.  * Local Variables:
  162.  * version-control:t
  163.  * comment-column:40
  164.  * End:
  165.  */
  166.