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>
22 #include <WINNT\afsreg.h>
25 #define AFS_DAEMON_EVENT_NAME "TransarcAFSDaemon"
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 #define TRACE_OPTION_EVENT 2
38 #define TRACE_OPTION_DEBUGLOG 4
40 #define ISCLIENTTRACE(v) ( ((v) & TRACE_OPTION_EVENT)==TRACE_OPTION_EVENT)
41 #define ISCLIENTDEBUGLOG(v) (((v) & TRACE_OPTION_DEBUGLOG)==TRACE_OPTION_DEBUGLOG)
43 DWORD osi_TraceOption=0;
45 osi_fdOps_t osi_logFDOps = {
51 /* create a new log, taking a name and a size in entries (not words) */
52 osi_log_t *osi_LogCreate(char *namep, size_t size)
57 LARGE_INTEGER bigFreq;
58 LARGE_INTEGER bigTemp;
59 LARGE_INTEGER bigJunk;
61 if (osi_Once(&osi_logOnce)) {
62 QueryPerformanceFrequency(&bigFreq);
63 if (bigFreq.LowPart == 0 && bigFreq.HighPart == 0)
66 /* turn frequency into ticks per 10 micros */
67 bigTemp.LowPart = 100000;
69 osi_logTixToMicros = 10;
70 bigFreq = LargeIntegerDivide(bigFreq, bigTemp, &bigJunk);
72 /* check if resolution is too fine or to gross for this to work */
73 if (bigFreq.HighPart > 0 || bigFreq.LowPart < 8)
74 osi_logFreq = 0; /* too big to represent as long */
76 osi_logFreq = bigFreq.LowPart;
80 osi_EndOnce(&osi_logOnce);
83 logp = malloc(sizeof(osi_log_t));
84 memset(logp, 0, sizeof(osi_log_t));
86 size_t namelen = strlen(namep) + 1;
88 logp->namep = malloc(namelen * sizeof(char));
89 StringCchCopyA(logp->namep, namelen, namep);
91 osi_QAdd((osi_queue_t **) &osi_allLogsp, &logp->q);
93 /* compute size we'll use */
94 if (size == 0) size = osi_logSize;
96 /* handle init for this size */
98 logp->datap = malloc(size * sizeof(osi_logEntry_t));
100 /* init strings array */
101 logp->maxstringindex = size/3;
102 logp->stringindex = 0;
103 logp->stringsp = malloc(logp->maxstringindex * OSI_LOG_STRINGSIZE);
106 thrd_InitCrit(&logp->cs);
108 StringCbCopyA(tbuffer, sizeof(tbuffer), "log:");
109 StringCbCatA(tbuffer, sizeof(tbuffer), namep);
110 typep = osi_RegisterFDType(tbuffer, &osi_logFDOps, logp);
112 /* add formatting info */
113 osi_AddFDFormatInfo(typep, OSI_DBRPC_REGIONINT, 0,
114 "Thread ID", OSI_DBRPC_HEX);
115 osi_AddFDFormatInfo(typep, OSI_DBRPC_REGIONSTRING, 1,
122 /* we just panic'd. Turn off all logging adding special log record
123 * to all enabled logs. Be careful not to wait for a lock.
125 void osi_LogPanic(char *filep, size_t lineNumber)
129 for(tlp = osi_allLogsp; tlp; tlp = (osi_log_t *) osi_QNext(&tlp->q)) {
130 if (!tlp->enabled) continue;
132 /* otherwise, proceed */
134 osi_LogAdd(tlp, "**PANIC** (file %s:%d)", (size_t) filep, lineNumber, 0, 0, 0);
136 osi_LogAdd(tlp, "**PANIC**", 0, 0, 0, 0, 0);
138 /* should grab lock for this, but we're in panic, and better safe than
145 /* reset the contents of a log */
146 void osi_LogReset(osi_log_t *logp)
149 thrd_EnterCrit(&logp->cs);
151 thrd_LeaveCrit(&logp->cs);
156 void osi_LogFree(osi_log_t *logp)
160 osi_QRemove((osi_queue_t **) &osi_allLogsp, &logp->q);
164 thrd_DeleteCrit(&logp->cs);
168 /* add an element to a log */
169 void osi_LogAdd(osi_log_t *logp, char *formatp, size_t p0, size_t p1, size_t p2, size_t p3, size_t p4)
173 LARGE_INTEGER bigTime;
175 /* handle init races */
178 /* do this w/o locking for speed; it is obviously harmless if we're off
181 if (!logp->enabled) return;
183 thrd_EnterCrit(&logp->cs);
184 if (logp->nused < logp->alloc) logp->nused++;
187 if (logp->first >= logp->alloc) logp->first -= logp->alloc;
189 ix = logp->first + logp->nused - 1;
190 if (ix >= logp->alloc) ix -= logp->alloc;
192 lep = logp->datap + ix; /* ptr arith */
193 lep->tid = thrd_Current();
195 /* get the time, using the high res timer if available */
197 QueryPerformanceCounter(&bigTime);
198 lep->micros = (bigTime.LowPart / osi_logFreq) * osi_logTixToMicros;
200 else lep->micros = GetCurrentTime() * 1000;
202 lep->formatp = formatp;
209 printf( "%9ld:", lep->micros );
210 printf( formatp, p0, p1, p2, p3, p4);
214 if(ISCLIENTDEBUGLOG(osi_TraceOption)) {
215 char wholemsg[1024], msg[1000];
217 StringCbPrintfA(msg, sizeof(msg), formatp,
219 StringCbPrintfA(wholemsg, sizeof(wholemsg),
222 OutputDebugStringA(wholemsg);
225 thrd_LeaveCrit(&logp->cs);
228 void osi_LogPrint(osi_log_t *logp, FILE_HANDLE handle)
230 char wholemsg[1024], msg[1000];
234 if (!logp->enabled) return;
236 thrd_EnterCrit(&logp->cs);
238 for (ix = logp->first, i = 0;
240 i++, ix++, (ix >= logp->alloc ? ix -= logp->alloc : 0)) {
241 lep = logp->datap + ix; /* pointer arithmetic */
242 StringCbPrintfA(msg, sizeof(msg), lep->formatp,
243 lep->parms[0], lep->parms[1],
244 lep->parms[2], lep->parms[3],
246 StringCbPrintfA(wholemsg, sizeof(wholemsg),
247 "time %d.%06d, tid %d %s\r\n",
248 lep->micros / 1000000,
249 lep->micros % 1000000,
251 if (!WriteFile(handle, wholemsg, strlen(wholemsg),
256 thrd_LeaveCrit(&logp->cs);
259 char *osi_LogSaveString(osi_log_t *logp, char *s)
265 if (!logp->enabled) return s;
267 if (s == NULL) return NULL;
269 thrd_EnterCrit(&logp->cs);
271 saveplace = logp->stringsp[logp->stringindex];
273 if (strlen(s) >= OSI_LOG_STRINGSIZE)
274 StringCbPrintfA(saveplace, OSI_LOG_STRINGSIZE,
276 s + strlen(s) - (OSI_LOG_STRINGSIZE - 4));
278 StringCbCopyA(saveplace, OSI_LOG_STRINGSIZE, s);
282 if (logp->stringindex >= logp->maxstringindex)
283 logp->stringindex = 0;
285 thrd_LeaveCrit(&logp->cs);
290 wchar_t *osi_LogSaveStringW(osi_log_t *logp, wchar_t *s)
296 if (!logp->enabled) return s;
298 if (s == NULL) return NULL;
300 thrd_EnterCrit(&logp->cs);
302 saveplace = (wchar_t *) (logp->stringsp[logp->stringindex]);
304 if (wcslen(s)*sizeof(wchar_t) >= OSI_LOG_STRINGSIZE)
305 StringCbPrintfW(saveplace, OSI_LOG_STRINGSIZE, L"...%s",
306 (s + wcslen(s) - (OSI_LOG_STRINGSIZE/sizeof(wchar_t) - 4)));
308 StringCbCopyW(saveplace, OSI_LOG_STRINGSIZE, s);
312 if (logp->stringindex >= logp->maxstringindex)
313 logp->stringindex = 0;
315 thrd_LeaveCrit(&logp->cs);
320 long osi_LogFDCreate(osi_fdType_t *typep, osi_fd_t **outpp)
325 lfdp = malloc(sizeof(*lfdp));
326 logp = lfdp->logp = typep->rockp; /* the log we were created for */
327 thrd_EnterCrit(&logp->cs);
328 lfdp->nused = logp->nused;
329 lfdp->first = logp->first;
331 thrd_LeaveCrit(&logp->cs);
337 long osi_LogFDGetInfo(osi_fd_t *ifd, osi_remGetInfoParms_t *outp)
345 lfdp = (osi_logFD_t *) ifd;
348 /* see if we're done */
349 if (lfdp->current >= lfdp->nused) return OSI_DBRPC_EOF;
352 thrd_EnterCrit(&logp->cs);
354 /* compute which one we want */
355 ix = lfdp->first + lfdp->current;
356 if (ix >= logp->alloc) ix -= logp->alloc;
358 lep = logp->datap + ix; /* ptr arith to current index */
360 StringCbPrintfA(tbuffer, sizeof(tbuffer), lep->formatp, lep->parms[0], lep->parms[1],
361 lep->parms[2], lep->parms[3], lep->parms[4]);
363 /* now copy out info */
364 StringCbCopyA(outp->sdata[0], sizeof(outp->sdata[0]), tbuffer);
365 StringCbPrintfA(tbuffer, sizeof(tbuffer), "%5.6f", ((double)lep->micros)/1000000.0);
366 StringCbCopyA(outp->sdata[1], sizeof(outp->sdata[0]), tbuffer);
367 outp->idata[0] = lep->tid;
371 thrd_LeaveCrit(&logp->cs);
375 long osi_LogFDClose(osi_fd_t *ifdp)
381 void osi_LogEnable(osi_log_t *logp)
387 void osi_LogDisable(osi_log_t *logp)
393 void osi_InitTraceOption()
395 DWORD LSPtype, LSPsize;
397 (void) RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
398 0, KEY_QUERY_VALUE, &NPKey);
399 LSPsize=sizeof(osi_TraceOption);
400 RegQueryValueEx(NPKey, "TraceOption", NULL,
401 &LSPtype, (LPBYTE)&osi_TraceOption, &LSPsize);
406 void osi_LogEvent0(char *a,char *b)
410 if (!ISCLIENTTRACE(osi_TraceOption))
412 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
414 ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, (const char **)ptbuf, NULL);
415 DeregisterEventSource(h);
419 void osi_LogEvent(char *a,char *b,char *c,...)
421 HANDLE h; char *ptbuf[1],buf[MAXBUF_+1];
423 if (!ISCLIENTTRACE(osi_TraceOption))
425 h = RegisterEventSource(NULL, AFS_DAEMON_EVENT_NAME);
427 StringCbVPrintfA(buf,MAXBUF_,c,marker);
429 ReportEvent(h, EVENTLOG_INFORMATION_TYPE, 0, 0, NULL, 1, 0, (const char **)ptbuf, NULL);\
430 DeregisterEventSource(h);
434 char *osi_HexifyString(char *s) {
436 char *hex = "0123456789abcdef";
437 char *buf, *counter, *bufp;
441 bufp = buf = malloc( len * 3 ); /* [xx.xx.xx.xx\0] */
443 if(!buf) return NULL;
445 for(counter = s; *counter; counter ++) {
446 if(counter != s) *bufp++ = '.';
448 *bufp++ = hex[(c>>4) & 0xf];
449 *bufp++ = hex[c & 0xf];