2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 /* Copyright (C) 1994 Cazamar Systems, Inc. */
12 #include <afs/param.h>
27 /* the size; overrideable */
28 long osi_logSize = OSI_LOG_DEFAULTSIZE;
30 static osi_once_t osi_logOnce;
32 osi_log_t *osi_allLogsp; /* all logs known; for use during panic */
34 unsigned long osi_logFreq; /* 0, or frequency of high perf counter */
35 unsigned long osi_logTixToMicros; /* mult. correction factor */
37 osi_fdOps_t osi_logFDOps = {
45 /* create a new log, taking a name and a size in entries (not words) */
46 osi_log_t *osi_LogCreate(char *namep, long size)
51 LARGE_INTEGER bigFreq;
52 LARGE_INTEGER bigTemp;
53 LARGE_INTEGER bigJunk;
56 if (osi_Once(&osi_logOnce)) {
57 QueryPerformanceFrequency(&bigFreq);
58 if (bigFreq.LowPart == 0 && bigFreq.HighPart == 0)
61 /* turn frequency into ticks per 10 micros */
62 bigTemp.LowPart = 100000;
64 osi_logTixToMicros = 10;
65 bigFreq = LargeIntegerDivide(bigFreq, bigTemp, &bigJunk);
67 /* check if resolution is too fine or to gross for this to work */
68 if (bigFreq.HighPart > 0 || bigFreq.LowPart < 8)
69 osi_logFreq = 0; /* too big to represent as long */
71 osi_logFreq = bigFreq.LowPart;
75 osi_EndOnce(&osi_logOnce);
79 logp = malloc(sizeof(osi_log_t));
80 memset(logp, 0, sizeof(osi_log_t));
81 logp->namep = malloc(strlen(namep)+1);
82 strcpy(logp->namep, namep);
84 osi_QAdd((osi_queue_t **) &osi_allLogsp, &logp->q);
86 /* compute size we'll use */
87 if (size == 0) size = osi_logSize;
89 /* handle init for this size */
91 logp->datap = malloc(size * sizeof(osi_logEntry_t));
93 /* init strings array */
94 logp->maxstringindex = size/10;
95 logp->stringindex = 0;
96 logp->stringsp = malloc((size/10) * OSI_LOG_STRINGSIZE);
99 thrd_InitCrit(&logp->cs);
101 strcpy(tbuffer, "log:");
102 strcat(tbuffer, namep);
103 typep = osi_RegisterFDType(tbuffer, &osi_logFDOps, logp);
106 /* add formatting info */
107 osi_AddFDFormatInfo(typep, OSI_DBRPC_REGIONINT, 0,
108 "Thread ID", OSI_DBRPC_HEX);
109 osi_AddFDFormatInfo(typep, OSI_DBRPC_REGIONSTRING, 1,
117 /* we just panic'd. Turn off all logging adding special log record
118 * to all enabled logs. Be careful not to wait for a lock.
120 void osi_LogPanic(char *filep, long lineNumber)
124 for(tlp = osi_allLogsp; tlp; tlp = (osi_log_t *) osi_QNext(&tlp->q)) {
125 if (!tlp->enabled) continue;
127 /* otherwise, proceed */
129 osi_LogAdd(tlp, "**PANIC** (file %s:%d)", (long) filep, lineNumber, 0, 0);
131 osi_LogAdd(tlp, "**PANIC**", 0, 0, 0, 0);
133 /* should grab lock for this, but we're in panic, and better safe than
140 /* reset the contents of a log */
141 void osi_LogReset(osi_log_t *logp)
144 thrd_EnterCrit(&logp->cs);
146 thrd_LeaveCrit(&logp->cs);
151 void osi_LogFree(osi_log_t *logp)
155 osi_QRemove((osi_queue_t **) &osi_allLogsp, &logp->q);
159 thrd_DeleteCrit(&logp->cs);
163 /* add an element to a log */
164 void osi_LogAdd(osi_log_t *logp, char *formatp, long p0, long p1, long p2, long p3)
168 LARGE_INTEGER bigTime;
170 /* handle init races */
173 /* do this w/o locking for speed; it is obviously harmless if we're off
176 if (!logp->enabled) return;
178 thrd_EnterCrit(&logp->cs);
179 if (logp->nused < logp->alloc) logp->nused++;
182 if (logp->first >= logp->alloc) logp->first -= logp->alloc;
184 ix = logp->first + logp->nused - 1;
185 if (ix >= logp->alloc) ix -= logp->alloc;
187 lep = logp->datap + ix; /* ptr arith */
188 lep->tid = thrd_Current();
190 /* get the time, using the high res timer if available */
193 QueryPerformanceCounter(&bigTime);
194 lep->micros = (bigTime.LowPart / osi_logFreq) * osi_logTixToMicros;
196 else lep->micros = GetCurrentTime() * 1000;
198 lep->micros = gettime_us();
201 lep->formatp = formatp;
206 thrd_LeaveCrit(&logp->cs);
209 void osi_LogPrint(osi_log_t *logp, FILE_HANDLE handle)
211 char wholemsg[1000], msg[1000];
215 if (!logp->enabled) return;
217 thrd_EnterCrit(&logp->cs);
219 for (ix = logp->first, i = 0;
221 i++, ix++, (ix >= logp->alloc ? ix -= logp->alloc : 0)) {
222 lep = logp->datap + ix; /* pointer arithmetic */
223 sprintf(msg, lep->formatp,
224 lep->parms[0], lep->parms[1],
225 lep->parms[2], lep->parms[3]);
226 sprintf(wholemsg, "time %d.%06d, pid %d %s\n",
227 lep->micros / 1000000,
228 lep->micros % 1000000,
231 if (!WriteFile(handle, wholemsg, strlen(wholemsg),
234 if ((ioCount = fwrite(wholemsg, 1, strlen(wholemsg), handle)) == 0)
239 thrd_LeaveCrit(&logp->cs);
242 char *osi_LogSaveString(osi_log_t *logp, char *s)
244 char *saveplace = logp->stringsp[logp->stringindex];
246 if (s == NULL) return NULL;
248 if (strlen(s) >= OSI_LOG_STRINGSIZE)
249 sprintf(saveplace, "...%s",
250 s + strlen(s) - (OSI_LOG_STRINGSIZE - 4));
252 strcpy(saveplace, s);
254 if (logp->stringindex >= logp->maxstringindex)
255 logp->stringindex = 0;
260 long osi_LogFDCreate(osi_fdType_t *typep, osi_fd_t **outpp)
265 lfdp = malloc(sizeof(*lfdp));
266 logp = lfdp->logp = typep->rockp; /* the log we were created for */
267 thrd_EnterCrit(&logp->cs);
268 lfdp->nused = logp->nused;
269 lfdp->first = logp->first;
271 thrd_LeaveCrit(&logp->cs);
278 long osi_LogFDGetInfo(osi_fd_t *ifd, osi_remGetInfoParms_t *outp)
286 lfdp = (osi_logFD_t *) ifd;
289 /* see if we're done */
290 if (lfdp->current >= lfdp->nused) return OSI_DBRPC_EOF;
293 thrd_EnterCrit(&logp->cs);
295 /* compute which one we want */
296 ix = lfdp->first + lfdp->current;
297 if (ix >= logp->alloc) ix -= logp->alloc;
299 lep = logp->datap + ix; /* ptr arith to current index */
301 sprintf(tbuffer, lep->formatp, lep->parms[0], lep->parms[1],
302 lep->parms[2], lep->parms[3]);
304 /* now copy out info */
305 strcpy(outp->sdata[0], tbuffer);
306 sprintf(tbuffer, "%5.6f", ((double)lep->micros)/1000000.0);
307 strcpy(outp->sdata[1], tbuffer);
308 outp->idata[0] = lep->tid;
312 thrd_LeaveCrit(&logp->cs);
317 long osi_LogFDClose(osi_fd_t *ifdp)
323 void osi_LogEnable(osi_log_t *logp)
329 void osi_LogDisable(osi_log_t *logp)
335 #define REG_CLIENT_PARMS_KEY "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"
336 #define TRACE_OPTION_EVENT 1
337 #define ISLOGONTRACE(v) ( ((v) & TRACE_OPTION_EVENT)==TRACE_OPTION_EVENT)
339 DWORD osi_TraceOption=0;
341 void osi_InitTraceOption()
343 DWORD LSPtype, LSPsize;
345 (void) RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_CLIENT_PARMS_KEY,
346 0, KEY_QUERY_VALUE, &NPKey);
347 LSPsize=sizeof(osi_TraceOption);
348 RegQueryValueEx(NPKey, "TraceOption", NULL,
349 &LSPtype, (LPBYTE)&osi_TraceOption, &LSPsize);
\r
354 void osi_LogEvent0(char *a,char *b)
356 HANDLE h; char *ptbuf[1],buf[MAXBUF_+1];
357 if (!ISLOGONTRACE(osi_TraceOption))
359 h = RegisterEventSource(NULL, a);
361 ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, (const char **)ptbuf, NULL);
362 DeregisterEventSource(h);
366 void osi_LogEvent(char *a,char *b,char *c,...)
368 HANDLE h; char *ptbuf[1],buf[MAXBUF_+1];
370 if (!ISLOGONTRACE(osi_TraceOption))
375 h = RegisterEventSource(NULL, buf);
378 h = RegisterEventSource(NULL, a);
380 _vsnprintf(buf,MAXBUF_,c,marker);
382 ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, (const char **)ptbuf, NULL);\
383 DeregisterEventSource(h);