home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / APPS / lout2.lzh / LOUT2 / z27.c < prev    next >
Text File  |  1994-01-23  |  10KB  |  202 lines

  1. /*@z27.c:Debug Service:Debug flags@*******************************************/
  2. /*                                                                           */
  3. /*  LOUT: A HIGH-LEVEL LANGUAGE FOR DOCUMENT FORMATTING (VERSION 2.05)       */
  4. /*  COPYRIGHT (C) 1993 Jeffrey H. Kingston                                   */
  5. /*                                                                           */
  6. /*  Jeffrey H. Kingston (jeff@cs.su.oz.au)                                   */
  7. /*  Basser Department of Computer Science                                    */
  8. /*  The University of Sydney 2006                                            */
  9. /*  AUSTRALIA                                                                */
  10. /*                                                                           */
  11. /*  This program is free software; you can redistribute it and/or modify     */
  12. /*  it under the terms of the GNU General Public License as published by     */
  13. /*  the Free Software Foundation; either version 1, or (at your option)      */
  14. /*  any later version.                                                       */
  15. /*                                                                           */
  16. /*  This program is distributed in the hope that it will be useful,          */
  17. /*  but WITHOUT ANY WARRANTY; without even the implied warranty of           */
  18. /*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            */
  19. /*  GNU General Public License for more details.                             */
  20. /*                                                                           */
  21. /*  You should have received a copy of the GNU General Public License        */
  22. /*  along with this program; if not, write to the Free Software              */
  23. /*  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
  24. /*                                                                           */
  25. /*  FILE:         z27.c                                                      */
  26. /*  MODULE:       Debug Service                                              */
  27. /*  EXTERNS:      dbg[], DebugInit(), Debug()                                */
  28. /*                ProfileOn(), ProfileOff(), ProfilePrint()                  */
  29. /*                                                                           */
  30. /*****************************************************************************/
  31. #include "externs"
  32.  
  33. #if DEBUG_ON
  34. struct dbs  dbg[] = {
  35.     "zz",    0, 0, 0,        /* - unused -                */
  36.     "sp",    0, 0, 0,        /* Supervise                 */
  37.     "la",    0, 0, 0,        /* Lexical Analyser          */
  38.     "fs",    0, 0, 0,        /* File Service              */
  39.     "ts",    0, 0, 0,        /* Token Service             */
  40.     "rd",    0, 0, 0,        /* Read Definitions          */
  41.     "op",    0, 0, 0,        /* Object Parser             */
  42.     "os",    0, 0, 0,        /* Object Service            */
  43.     "om",    0, 0, 0,        /* Object Manifest           */
  44.     "ce",    0, 0, 0,        /* Closure Expansion         */
  45.     "cr",    0, 0, 0,        /* Cross References          */
  46.     "ss",    0, 0, 0,        /* Style Service             */
  47.     "sf",    0, 0, 0,        /* Size Finder               */
  48.     "ob",    0, 0, 0,        /* Object Breaking           */
  49.     "of",    0, 0, 0,        /* Object Filling            */
  50.     "sc",    0, 0, 0,        /* Size Constraints          */
  51.     "sa",    0, 0, 0,        /* Size Adjustments          */
  52.     "gw",    0, 0, 0,        /* Gap Widths                */
  53.     "gt",    0, 0, 0,        /* Galley Transfer           */
  54.     "ga",    0, 0, 0,        /* Galley Attaching          */
  55.     "gf",    0, 0, 0,        /* Galley Flusher            */
  56.     "gm",    0, 0, 0,        /* Galley Maker              */
  57.     "gs",    0, 0, 0,        /* Galley Service            */
  58.     "gp",    0, 0, 0,        /* Galley Printer            */
  59.     "ps",    0, 0, 0,        /* Print Service             */
  60.     "oe",    0, 0, 0,        /* Object Echo               */
  61.     "es",    0, 0, 0,        /* Echo Service              */
  62.     "zz",    0, 0, 0,        /* Debug Service (unused)    */
  63.     "yy",    0, 0, 0,        /* Error Service             */
  64.     "st",    0, 0, 0,        /* Symbol Table              */
  65.     "su",    0, 0, 0,        /* Symbol Uses               */
  66.     "ma",    0, 0, 0,        /* Memory Allocator          */
  67.     "cs",    0, 0, 0,        /* Counter Service           */
  68.     "bs",    0, 0, 0,        /* Database Service          */
  69.     "rs",    0, 0, 0,        /* Rotation Service          */
  70.     "tk",    0, 0, 0,        /* Time Keeper               */
  71.     "hy",    0, 0, 0,        /* Hyphenation               */
  72.     "ft",    0, 0, 0,        /* Font Tables               */
  73.     "ev",    0, 0, 0,        /* Encoding Vextors          */
  74.     "sh",    0, 0, 0,        /* String Handler            */
  75.     "pp",    0, 0, 0,        /* Profiling                 */
  76.     "",      0, 0, 0,        /* any                       */
  77. };
  78.  
  79. /*@::DebugInit(), Debug()@****************************************************/
  80. /*                                                                           */
  81. /*  DebugInit(str)                                                           */
  82. /*                                                                           */
  83. /*  Turn on the debug flag given by str.                                     */
  84. /*                                                                           */
  85. /*****************************************************************************/
  86.  
  87. DebugInit(str)
  88. FULL_CHAR *str;
  89. { int j, urg;
  90.   for( urg = 0;  urg < 2 && str[urg+2] == CH_FLAG_DEBUG;  urg++ );
  91.   for( j = 1;  ;  j++ )
  92.   { if( StringEqual(AsciiToFull(dbg[j].flag), &str[urg+2]) )  break;
  93.     if( StringEqual(AsciiToFull(dbg[j].flag), STR_EMPTY) )
  94.       Error(FATAL, no_fpos, "unknown debug flag %s", str);
  95.   }
  96.   for( ;  urg >= 0;  urg-- )  dbg[j].on[urg] = dbg[ANY].on[urg] = TRUE;
  97. } /* end DebugInit */
  98.  
  99.  
  100. /*****************************************************************************/
  101. /*                                                                           */
  102. /*  Debug(category, urgency, str, p1, p2, p3, p4, p5, p6, p7, p8)            */
  103. /*                                                                           */
  104. /*  Print str on debug output, if the flag corresponding to the given        */
  105. /*  debug category and urgency is on.                                        */
  106. /*                                                                           */
  107. /*****************************************************************************/
  108.  
  109. /*VARARGS3*/
  110. Debug(category, urgency, str, p1, p2, p3, p4, p5, p6, p7, p8)
  111. int category, urgency;  char *str;  int p1, p2, p3, p4, p5, p6, p7, p8;
  112. { static BOOLEAN first_message = TRUE;
  113.   if( first_message )
  114.   { fprintf(stderr, "\nLout Debug Output:\n");
  115.     first_message = FALSE;
  116.   }
  117.   fprintf(stderr, "%2s: ", dbg[category].flag);
  118.   fprintf(stderr, str, p1, p2, p3, p4, p5, p6, p7, p8);
  119.   fprintf(stderr, "\n");
  120.   fflush(stderr);
  121. } /* end Debug */
  122.  
  123.  
  124. /*@::ProfileOn(), ProfileOff(), ProfilePrint()@*******************************/
  125. /*                                                                           */
  126. /*  ProfileOn(str)                                                           */
  127. /*                                                                           */
  128. /*  Start profiling label str.                                               */
  129. /*                                                                           */
  130. /*****************************************************************************/
  131. #define MAXPROF    20
  132. #include <time.h>
  133.  
  134. struct profrec
  135. { char *label;            /* label of the profile              */
  136.   int calls;            /* number of calls with this label   */
  137.   long time;            /* total time of this label          */
  138. };
  139.  
  140. static struct profrec profstack[MAXPROF];
  141. static struct profrec profstore[MAXPROF];
  142. static int proftop = 0, profsize = 0;
  143.  
  144. ProfileOn(str)
  145. char *str;
  146. { int i;  long raw_time;
  147.   for( i = 0;  i < proftop;  i++ )
  148.   { if( strcmp(profstack[i].label, str) == 0 )
  149.     { for( i = 0;  i < proftop;  i++ )
  150.     fprintf(stderr, "profstack[%d] = %s\n", i, profstack[i].label);
  151.       Error(INTERN, no_fpos, "ProfileOn: %s restarted", str);
  152.     }
  153.   }
  154.   if( proftop == MAXPROF )  Error(INTERN, no_fpos, "ProfileOn: overflow");
  155.   time(&raw_time);  profstack[proftop].label = str;
  156.   profstack[proftop++].time  = raw_time;
  157. } /* end ProfileOn */
  158.  
  159.  
  160. /*****************************************************************************/
  161. /*                                                                           */
  162. /*  ProfileOff(str)                                                          */
  163. /*                                                                           */
  164. /*  Stop profiling label str.                                                */
  165. /*                                                                           */
  166. /*****************************************************************************/
  167.  
  168. ProfileOff(str)
  169. char *str;
  170. { int i;  long raw_time;
  171.   if( proftop == 0 || strcmp(profstack[proftop-1].label, str) != 0 )
  172.     Error(INTERN, no_fpos, "ProfileOff: %s is not the current label", str);
  173.   for( i = 0;  i < profsize && strcmp(profstore[i].label, str) != 0; i++ );
  174.   if( i >= profsize )
  175.   { if( profsize++ == MAXPROF )  Error(INTERN, no_fpos, "ProfileOff: overflow");
  176.     profstore[i].label = str;
  177.     profstore[i].calls = 0;
  178.     profstore[i].time  = 0;
  179.   }
  180.   time(&raw_time);  profstore[i].calls += 1;
  181.   profstore[i].time  += (raw_time - profstack[--proftop].time);
  182. } /* end ProfileOff */
  183.  
  184.  
  185. /*****************************************************************************/
  186. /*                                                                           */
  187. /*  ProfilePrint()                                                           */
  188. /*                                                                           */
  189. /*  Print results of profiling.                                              */
  190. /*                                                                           */
  191. /*****************************************************************************/
  192.  
  193. ProfilePrint()
  194. { int i;
  195.   for( i = 0;  i < profsize;  i++ )
  196.   { fprintf(stderr, "Profile %-20s  %6d secs, %3d calls, %6.2f secs/call\n",
  197.     profstore[i].label, profstore[i].time, profstore[i].calls,
  198.     (float) profstore[i].time / profstore[i].calls );
  199.   }
  200. } /* end ProfilePrint */
  201. #endif
  202.