home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: OtherApp / OtherApp.zip / wincam.zip / winc_src.zip / trace.c < prev    next >
C/C++ Source or Header  |  1997-02-28  |  3KB  |  130 lines

  1. /*
  2.  *
  3.  * simple tracing control, to isolate most of the code from the vagaries
  4.  * of where this stuff should end up.  clients of the library should
  5.  * register their preferred output routines using the "register_...()"
  6.  * functions.  these routines should have semantics similar to vfprintf(),
  7.  * (though return codes are ignored).  for a command line app, you might
  8.  * register vfprintf itself, with a cookie of "stderr".  for a graphical
  9.  * app, you would do something more reasonable.
  10.  *
  11.  * Copyright (C) 1996, Paul G. Fox
  12.  *
  13.  * This program is free software; you can redistribute it and/or modify it
  14.  * under the terms of the GNU General Public License as published by the
  15.  * Free Software Foundation; either version 2 of the License, or (at your
  16.  * option) any later version.
  17.  * 
  18.  * This program is distributed in the hope that it will be useful, but
  19.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  21.  * General Public License for more details.
  22.  * 
  23.  * You should have received a copy of the GNU General Public License along
  24.  * with this program; if not, write to the Free Software Foundation, Inc.,
  25.  * 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  *
  27.  * $Header: E:/winc/RCS/trace.c 1.1 1997/03/01 03:44:14 Derek Exp Derek $
  28.  */
  29.  
  30.  
  31. #include <stdarg.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include "trace.h"
  35.  
  36.  
  37. /*
  38.  * errors
  39.  */
  40. int errorlevel;
  41. static vprintffunc errorfunc;
  42. static void *errorcookie;
  43.  
  44. /* hook in the user's callback routine */
  45. void
  46. register_error_func(vprintffunc f, void * cookie)
  47. {
  48.     errorfunc = f;
  49.     errorcookie = cookie;
  50. }
  51.  
  52. /* set the current output level:  higher is more detail */
  53. void
  54. set_error_level(int new)
  55. {
  56.     errorlevel = new;
  57. }
  58.  
  59. /* emit a message to the right place */
  60. void
  61. errormsg(char *fmt, ...)
  62. {
  63.     va_list ap;
  64.  
  65.     if (!errorfunc)
  66.     return;
  67.  
  68.     va_start(ap, fmt);
  69.     (*errorfunc)(errorcookie, fmt, ap);
  70.     va_end(ap);
  71. }
  72.  
  73. /*
  74.  * tracing
  75.  */
  76. char tracechars[30];
  77. static int fulltrace;
  78. static vprintffunc tracefunc;
  79. static void *tracecookie;
  80.  
  81. /* hook in the user's callback routine */
  82. void
  83. register_trace_func(vprintffunc f, void * cookie)
  84. {
  85.     tracefunc = f;
  86.     tracecookie = cookie;
  87. }
  88.  
  89.  
  90. /* set the current output level:  higher is more detail */
  91. void
  92. set_trace_class(char *newclass)
  93. {
  94.     strncpy(tracechars, newclass, sizeof(tracechars));
  95.     tracechars[sizeof(tracechars)-1] = '\0';
  96.  
  97.     /* a 'V' anywhere in the global set turns on all tracing */
  98.     fulltrace = (strchr(tracechars, 'V') != 0);
  99.  
  100. }
  101.  
  102. /* emit a message to the right place */
  103. void
  104. tracemsg(char *class, char *fmt, ...)
  105. {
  106.     va_list ap;
  107.     char *cp;
  108.  
  109.     if (!tracefunc)
  110.     return;
  111.  
  112.     /* if the same character appears in both the global selector
  113.     string and in the string attached to the tracepoint itself,
  114.     then we print it.  if the global selector string has an
  115.     uppercase char, it will match a lowercase tracepoint, but not
  116.     vice-versa. (so a user asking for "A" messages will get both
  117.     "a" and "A" messages, but a user asking for just "a" messages
  118.     will get only those. */
  119.  
  120.     for (cp = class; *cp; cp++) {
  121.     if (fulltrace || strchr(tracechars, *cp) ||
  122.         (islower(*cp) && strchr(tracechars, toupper(*cp)))) {
  123.         va_start(ap, fmt);
  124.         (*tracefunc)(tracecookie, fmt, ap);
  125.         va_end(ap);
  126.         return;
  127.     }
  128.     }
  129. }
  130.