modules/up/src/Core/util/Trail.cc

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. EnabledTrace
  2. EnabledTrace
  3. Trail
  4. Trail
  5. trace
  6. log
  7. Trace
  8. Trace
  9. enable
  10. disable
  11. makeStatusKnown
  12. isDisabled
  13. isEnabled
  14. isUnknown
  15. name

   1 //
   2 // $Id: Trail.cc,v 1.1.1.1 2000/03/10 16:32:20 engin Exp $
   3 //
   4 // Trail.cc
   5 // Author: Ramesh Govindan <govindan@isi.edu>
   6 //
   7 // Tracing implementation
   8 
   9 #ifdef HAVE_CONFIG_H
  10 #include <config.h>
  11 #endif
  12 
  13 #include <cstdio>
  14 #include <cstdlib>
  15 #include <cstdarg>
  16 
  17 #include "util/Types.hh"
  18 #include "util/Trail.hh"
  19 #include "sched/Dispatcher.hh"
  20 
  21 extern "C" {
  22 #include <sys/types.h>
  23 #include <syslog.h>
  24 #include <string.h>    
  25 
  26 #ifndef STDC_HEADERS
  27 extern void openlog(const char*, int, int);
  28 extern void syslog(int, const char*, ...);
  29 extern void closelog(void);
  30 extern int setlinebuf(...);
  31 #endif
  32 }
  33 
  34 #ifndef LOG_LOCAL0
  35 #define LOG_LOCAL0      (16 << 3)
  36 #endif // LOG_LOCAL0
  37 
  38 // Globals
  39 Trail*          systemTrail = NULL;
  40 TrailConfig     trailConfig;
  41 
  42 // Constants
  43 const int       MaxTraceBufferSize  = 1024;
  44 
  45 EnabledTrace::EnabledTrace(char* n)
     /* [<][>][^][v][top][bottom][index][help] */
  46         : ListNode()
  47 {
  48     name = new char[strlen(n)+1];
  49     strcpy(name, n);
  50     return;
  51 }
  52 
  53 EnabledTrace::~EnabledTrace()
     /* [<][>][^][v][top][bottom][index][help] */
  54 {
  55     delete [] name;
  56 }
  57 
  58 Trail::Trail(char *programName)
     /* [<][>][^][v][top][bottom][index][help] */
  59 {
  60     buffer = new char[MaxTraceBufferSize];
  61     dispatcher.systemClock.sync();
  62     start = dispatcher.systemClock;
  63 
  64     // Open the log if necessary
  65     if (trailConfig.background) {
  66         int     l;
  67         
  68         l = LOG_LOCAL0;
  69         if (trailConfig.logLevel <= 7) {        // Is this kosher?
  70             l += (trailConfig.logLevel << 3);
  71         }
  72         trailConfig.logLevel = l;
  73         openlog(programName, LOG_PID | LOG_NDELAY, trailConfig.logLevel);
  74     }
  75     return;
  76 }
  77 
  78 Trail::~Trail()
     /* [<][>][^][v][top][bottom][index][help] */
  79 {
  80     trailConfig.enabled.clear();
  81     closelog();
  82 }
  83 
  84 void
  85 Trail::trace(const char *func,
     /* [<][>][^][v][top][bottom][index][help] */
  86              TraceCode& code,
  87              const char *fmt,
  88              ...)
  89 {
  90     va_list ap;
  91     
  92     // cengiz 
  93     // return immediately if this trace is disabled
  94     if (code.isDisabled()) {
  95         return;
  96     }
  97 
  98     // it is a bug to return w/o va_end after a va_start
  99     va_start(ap, fmt);
 100 #ifdef HAVE_VSNPRINTF
 101     (void) vsnprintf(buffer, MaxTraceBufferSize, fmt, ap);
 102 #else
 103     (void) vsprintf(buffer, fmt, ap);
 104 #endif
 105     va_end(ap);
 106     if (trailConfig.background) {
 107         syslog(trailConfig.logLevel | LOG_DEBUG, 
 108                "(%s) %s", code.name(), buffer);
 109     } else {
 110         if (trailConfig.printTimeStamp) {
 111            time_t t = time(NULL);
 112            struct tm *tm = localtime(&t);
 113   
 114            fprintf(stderr,
 115                    "%02d/%02d/%04d %02d:%02d:%02d ", 
 116                    tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900,
 117                    tm->tm_hour, tm->tm_min, tm->tm_sec);
 118 
 119            /*
 120             TimeShort   diff;
 121         
 122             dispatcher.systemClock.sync();
 123             diff = dispatcher.systemClock - start;
 124             (void) fprintf(stderr,
 125                            "(%u,%u) ",
 126                            (u_int) diff.seconds(),
 127                            (u_int) diff.fraction());
 128            */
 129         }
 130         (void) fprintf(stderr, "(%s) ", code.name());
 131         (void) fprintf(stderr, "%s", buffer);
 132         if (trailConfig.printFuncs) {
 133             (void) fprintf(stderr, "\t\t%.40s\n", func);
 134         }
 135     }
 136     (void) fflush(stderr);
 137     return;
 138 }
 139 
 140 void
 141 Trail::log(Boolean fatal,
     /* [<][>][^][v][top][bottom][index][help] */
 142            const char* func,
 143            const char* fmt,
 144            ...)
 145 {
 146     va_list ap;
 147     
 148     va_start(ap, fmt);
 149 #ifdef HAVE_VSNPRINTF
 150     (void) vsnprintf(buffer, MaxTraceBufferSize, fmt, ap);
 151 #else
 152     (void) vsprintf(buffer, fmt, ap);
 153 #endif
 154     va_end(ap);
 155     if (trailConfig.background) {
 156         (void) syslog(trailConfig.logLevel | 
 157                       ((fatal) ? LOG_CRIT : LOG_WARNING), 
 158                       "%s\t\t%.40s%s\n", buffer, func);
 159     } else {
 160         (void) fprintf(stderr, "ERROR in %s: %s", func, buffer);
 161         (void) fflush(stderr);
 162     }
 163     return;
 164 }
 165 
 166 TraceCode::Trace(const char* str)
     /* [<][>][^][v][top][bottom][index][help] */
 167 {
 168     nameString = new char[strlen(str)+1];
 169     strcpy(nameString, str);
 170     status = TraceStatusUnknown;
 171 }
 172 
 173 TraceCode::~Trace()
     /* [<][>][^][v][top][bottom][index][help] */
 174 {
 175   // Modified by wlee
 176   // delete nameString;
 177   delete []nameString;
 178 }
 179 
 180 void
 181 TraceCode::enable()
     /* [<][>][^][v][top][bottom][index][help] */
 182 {
 183     status = TraceStatusEnabled;
 184 }
 185 
 186 void
 187 TraceCode::disable()
     /* [<][>][^][v][top][bottom][index][help] */
 188 {
 189     status = TraceStatusDisabled;
 190 }
 191 
 192 inline void TraceCode::makeStatusKnown() {
     /* [<][>][^][v][top][bottom][index][help] */
 193    if (!isUnknown())
 194       return;
 195 
 196    for (EnabledTrace *et = trailConfig.enabled.head(); 
 197         et != NULL; 
 198         et = trailConfig.enabled.next(et))
 199       if (!strcmp(name(), et->name)) {
 200          enable();
 201          return;
 202       }
 203    disable();
 204    return;
 205 }
 206 
 207 Boolean
 208 TraceCode::isDisabled()
     /* [<][>][^][v][top][bottom][index][help] */
 209 {
 210     // cengiz modified the following
 211     if (status == TraceStatusDisabled)
 212        return true;
 213     makeStatusKnown();
 214     
 215     return (status == TraceStatusDisabled);
 216 }
 217 
 218 Boolean
 219 TraceCode::isEnabled()
     /* [<][>][^][v][top][bottom][index][help] */
 220 {
 221     // cengiz modified the following
 222     if (status == TraceStatusEnabled)
 223        return true;
 224     makeStatusKnown();
 225     
 226     return (status == TraceStatusEnabled);
 227 }
 228 
 229 Boolean
 230 TraceCode::isUnknown()
     /* [<][>][^][v][top][bottom][index][help] */
 231 {
 232     return (status == TraceStatusUnknown);
 233 }
 234 
 235 char*
 236 TraceCode::name()
     /* [<][>][^][v][top][bottom][index][help] */
 237 {
 238     return nameString;
 239 }
 240 
 241 // 
 242 //  Copyright (c) 1994 by the University of Southern California.
 243 //  All rights reserved.
 244 //
 245 //  Permission to use, copy, modify, and distribute this software and
 246 //  its documentation in source and binary forms for lawful
 247 //  non-commercial purposes and without fee is hereby granted, provided
 248 //  that the above copyright notice appear in all copies and that both
 249 //  the copyright notice and this permission notice appear in supporting
 250 //  documentation, and that any documentation, advertising materials,
 251 //  and other materials related to such distribution and use acknowledge
 252 //  that the software was developed by the University of Southern
 253 //  California and/or Information Sciences Institute.
 254 //  The name of the University of Southern California may not
 255 //  be used to endorse or promote products derived from this software
 256 //  without specific prior written permission.
 257 //
 258 //  THE UNIVERSITY OF SOUTHERN CALIFORNIA DOES NOT MAKE ANY REPRESENTATIONS
 259 //  ABOUT THE SUITABILITY OF THIS SOFTWARE FOR ANY PURPOSE.  THIS SOFTWARE IS
 260 //  PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
 261 //  INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 262 //  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, TITLE, AND 
 263 //  NON-INFRINGEMENT.
 264 //
 265 //  IN NO EVENT SHALL USC, OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY
 266 //  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES, WHETHER IN CONTRACT,
 267 //  TORT, OR OTHER FORM OF ACTION, ARISING OUT OF OR IN CONNECTION WITH,
 268 //  THE USE OR PERFORMANCE OF THIS SOFTWARE.
 269 //
 270 //  Questions concerning this software should be directed to 
 271 //  info-ra@isi.edu.
 272 //
 273 //  Author(s): Ramesh Govindan <govindan@isi.edu>
 274 

/* [<][>][^][v][top][bottom][index][help] */