source: trunk/src/kernel32/misc.cpp@ 2061

Last change on this file since 2061 was 2061, checked in by sandervl, 26 years ago

added support for private logfiles

File size: 16.6 KB
Line 
1/* $Id: misc.cpp,v 1.15 1999-12-12 14:32:38 sandervl Exp $ */
2
3/*
4 * Project Odin Software License can be found in LICENSE.TXT
5 * Logging procedures
6 *
7 * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl)
8 * Copyright 1998 Joel Troster
9 * Copyright 1998 Peter FitzSimmons
10 *
11 */
12
13
14/*****************************************************************************
15 * Includes *
16 *****************************************************************************/
17
18#define INCL_BASE
19#define INCL_WIN
20#define INCL_WINERRORS
21#define INCL_DOSFILEMGR
22#include <os2wrap.h> //Odin32 OS/2 api wrappers
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <stdarg.h>
27#include <win32type.h>
28#include <misc.h>
29#include "initterm.h"
30#include "logging.h"
31
32/*****************************************************************************
33 * PMPRINTF Version *
34 *****************************************************************************/
35
36#ifdef PMPRINTF
37
38/* ----- Customization variables ----- */
39#define PRINTFID ""
40#define PRINTFMAXLEN 300
41#define PRINTFLINELEN 100
42#define PRINTFTHREADS 54
43#define PRINTFQNAME "\\QUEUES\\PRINTF32"
44
45/* ----- Includes and externals ----- */
46#include <stddef.h> /* .. */
47#include <time.h> /* .. */
48
49extern ULONG flAllocMem; /*Tue 03.03.1998: knut */
50
51/* ----- Local defines ----- */
52#define PRINTFIDSIZE sizeof(PRINTFID)
53#define PRINTFMAXBUF PRINTFIDSIZE+PRINTFLINELEN
54
55
56/*****************************************************************************
57 * Structures *
58 *****************************************************************************/
59
60/* ----- Per-thread output buffer and current indices into line ---- */
61struct perthread {
62 LONG lineindex; /* where next char */
63 LONG tidemark; /* rightmost char */
64 int bell; /* TRUE if line has bell */
65 UCHAR line[PRINTFMAXBUF]; /* accumulator */
66 };
67
68/* ----- Local static variables ----- */
69static ULONG ourpid=0; /* our process ID */
70static ULONG servepid=0; /* process IDs of the server */
71static HQUEUE qhandle=0; /* handle for the queue */
72static struct perthread *tps[PRINTFTHREADS+1]; /* -> per-thread data */
73
74/* ----- Local subroutine ----- */
75static int printf_(struct perthread *);
76
77
78/* ----------------------------------------------------------------- */
79/* The "printf" function. Note this has a variable number of */
80/* arguments. */
81/* ----------------------------------------------------------------- */
82int SYSTEM EXPORT WriteLog(char *f, ...)
83 {
84 TIB *ptib; /* process/thread id structures */
85 PIB *ppib; /* .. */
86 TID ourtid; /* thread ID */
87 struct perthread *tp; /* pointer to per-thread data */
88 int rc; /* returncode */
89 ULONG urc; /* returncode */
90
91 urc=DosOpenQueue(&servepid, &qhandle, PRINTFQNAME); /* Open the Q */
92 /* Non-0 RC means Q does not exist or cannot be opened */
93 if (urc==343) return 0; /* queue does not exist, so quit */
94 if (urc!=0) return -1; /* report any other error */
95
96 /* First determine our thread ID (and hence get access to the */
97 /* correct per-thread data. If the per-thread data has not been */
98 /* allocated, then allocate it now. It is never freed, once */
99 /* allocated, as PRINTF is not notified of end-of-thread. */
100 DosGetInfoBlocks(&ptib,&ppib); /* get process/thread info */
101 ourtid=ptib->tib_ptib2->tib2_ultid; /* .. and copy TID */
102 if (ourtid>PRINTFTHREADS) /* too many threads .. */
103 return 0; /* .. so quit, quietly */
104 tp=tps[ourtid]; /* copy to local pointer */
105 if (tp==NULL) { /* uninitialized (NULL=0) */
106 /* allocate a per-thread structure */
107 tp=(struct perthread *)malloc(sizeof(struct perthread));
108 if (tp==NULL) return -1; /* out of memory -- return error */
109 tps[ourtid]=tp; /* save for future calls */
110 strcpy(tp->line,PRINTFID); /* initialize: line.. */
111 tp->lineindex=PRINTFIDSIZE-1; /* ..where next char */
112 tp->tidemark =PRINTFIDSIZE-2; /* ..rightmost char */
113 tp->bell=FALSE; /* ..if line has bell */
114 if (ourpid==0) ourpid=ppib->pib_ulpid; /* save PID for all to use */
115 }
116
117 { /* Block for declarations -- only needed if queue exists, etc. */
118 LONG count; /* count of characters formatted */
119 UCHAR buffer[PRINTFMAXLEN+1]; /* formatting area */
120 LONG i, newind; /* work */
121 UCHAR ch; /* .. */
122 va_list argptr; /* -> variable argument list */
123
124 va_start(argptr, f); /* get pointer to argument list */
125 count=vsprintf(buffer, f, argptr);
126 va_end(argptr); /* done with variable arguments */
127
128 if (count<0) return count-1000;/* bad start */
129
130 if (count>PRINTFMAXLEN) {
131 /* Disaster -- we are probably "dead", but just in case we */
132 /* are not, carry on with truncated data. */
133 count=PRINTFMAXLEN;
134 }
135 buffer[count]='\0'; /* ensure terminated */
136 /* OK, ready to go with the data now in BUFFER */
137 /* We copy from the formatted string to the output (line) buffer, */
138 /* taking note of certain control characters and sending a line */
139 /* the queue whenever we see a LF control, or when the line */
140 /* fills (causing a forced break). */
141 for (i=0; ; i++) {
142 ch=buffer[i]; if (!ch) break;
143 switch(ch) {
144 case '\r': /* carriage return */
145 tp->lineindex=PRINTFIDSIZE-1; /* back to start of line */
146 break;
147 case '\n': /* new line */
148 case '\f': /* form feed */
149 rc=printf_(tp); /* print a line */
150 if (rc!=0) return rc; /* error */
151 break;
152 case '\t': /* tab */
153 newind=tp->lineindex-PRINTFIDSIZE+1; /* offset into data */
154 newind=tp->lineindex+5-newind%5; /* new index requested */
155 if (newind>=PRINTFMAXBUF) newind=PRINTFMAXBUF; /* clamp */
156 for (; tp->lineindex<newind; tp->lineindex++) {
157 if (tp->lineindex>tp->tidemark) { /* beyond current end */
158 tp->line[tp->lineindex]=' '; /* add space */
159 tp->tidemark=tp->lineindex;
160 }
161 }
162 break;
163 case '\v': /* vertical tab */
164 /* ignore it */
165 break;
166 case '\b': /* backspace */
167 tp->lineindex=max(tp->lineindex-1,PRINTFIDSIZE);
168 break;
169 case '\a': /* alert (bell) */
170 tp->bell=TRUE;
171 break;
172 default: /* ordinary character */
173 tp->line[tp->lineindex]=ch;
174 if (tp->lineindex>tp->tidemark) /* is rightmost.. */
175 tp->tidemark=tp->lineindex;
176 tp->lineindex++; /* step for next */
177 } /* switch */
178 if (tp->lineindex>=PRINTFMAXBUF) {
179 rc=printf_(tp); /* print a line */
180 if (rc!=0) return rc; /* error */
181 }
182
183 } /* copy loop */
184 return count; /* all formatted data processed */
185 } /* block */
186 } /* printf */
187
188/* ----- printf_(tp) -- Local subroutine to send a line ------------ */
189/* A line has been completed (or overflowed): write it to the queue. */
190int printf_(struct perthread *tp) /* pointer to per-thread data */
191 {
192 ULONG urc; /* unsigned returncode */
193 PSZ pszTo, pszFrom; /* character pointers */
194 PVOID addr; /* address of output data */
195 long size; /* total size of output data */
196 time_t timenow; /* holds current time */
197
198 tp->line[tp->tidemark+1]='\0'; /* add terminator */
199 size=tp->tidemark+2; /* total length of data */
200
201 /* Get some shared memory that can be given away */
202 urc=DosAllocSharedMem(&addr, NULL, (unsigned)size,
203 OBJ_GIVEABLE|PAG_WRITE|PAG_COMMIT|flAllocMem);
204 /*knut: added flAllocMem */
205
206 if (urc!=0) return -2; /* error */
207
208 pszTo=addr; /* copy for clarity */
209 pszFrom=&(tp->line[0]); /* pointer to source */
210 strcpy(pszTo,pszFrom); /* copy the string to shared memory */
211
212 if (ourpid!=servepid) { /* (no giveaway needed if to self) */
213 urc=DosGiveSharedMem(addr, servepid, PAG_READ); /* give access */
214 if (urc!=0) return -3;} /* error */
215
216 /* Write the selector, size, and timestamp to the queue */
217 if (tp->bell) size=-size; /* BELL passed by negation */
218 time(&timenow); /* optional - else use 0 */
219 urc=DosWriteQueue(qhandle, /* handle */
220 (unsigned)timenow, /* 'request' (timestamp) */
221 (unsigned)size, /* 'length' (length/bell) */
222 addr, /* 'address' (address) */
223 0); /* priority (FIFO if enabled) */
224 if (urc!=0) return -4; /* error */
225 if (ourpid!=servepid) { /* if given away.. */
226 urc=DosFreeMem(addr); /* .. *we* are done with it */
227 if (urc!=0) return -5;} /* error */
228 /* Reset the line buffer and indices */
229 tp->lineindex=PRINTFIDSIZE-1; /* where next char */
230 tp->tidemark =PRINTFIDSIZE-2; /* rightmost char */
231 tp->bell =FALSE; /* true if line has bell */
232 return 0; /* success! */
233 } /* printf_ */
234#endif
235
236
237
238/*****************************************************************************
239 * Standard Version *
240 *****************************************************************************/
241
242static FILE *flog = NULL; /*PLF Mon 97-09-08 20:00:15*/
243static BOOL init = FALSE;
244static BOOL fLogging = TRUE;
245
246int SYSTEM EXPORT WriteLog(char *tekst, ...)
247{
248 USHORT sel = RestoreOS2FS();
249 va_list argptr;
250
251 if(!init)
252 {
253 init = TRUE;
254
255#ifdef DEFAULT_LOGGING_OFF
256 if(getenv("WIN32LOG_ENABLED")) {
257#else
258 if(!getenv("NOWIN32LOG")) {
259#endif
260 char logname[CCHMAXPATH];
261
262 sprintf(logname, "odin32_%d.log", loadNr);
263 flog = fopen(logname, "w");
264 if(flog == NULL) {//probably running exe on readonly device
265 sprintf(logname, "%sodin32_%d.log", kernel32Path, loadNr);
266 flog = fopen(logname, "w");
267 }
268 }
269 else
270 fLogging = FALSE;
271 }
272
273 if(fLogging && flog)
274 {
275 va_start(argptr, tekst);
276 vfprintf(flog, tekst, argptr);
277 va_end(argptr);
278
279 if(tekst[strlen(tekst)-1] != '\n')
280 fprintf(flog, "\n");
281 }
282
283 SetFS(sel);
284 return 1;
285}
286//******************************************************************************
287//******************************************************************************
288int SYSTEM EXPORT WritePrivateLog(void *logfile, char *tekst, ...)
289{
290 USHORT sel = RestoreOS2FS();
291 va_list argptr;
292
293 if(fLogging && logfile)
294 {
295 va_start(argptr, tekst);
296 vfprintf((FILE *)logfile, tekst, argptr);
297 va_end(argptr);
298
299 if(tekst[strlen(tekst)-1] != '\n')
300 fprintf((FILE *)logfile, "\n");
301 }
302
303 SetFS(sel);
304 return 1;
305}
306//******************************************************************************
307//NOTE: No need to save/restore FS, as our FS selectors have already been
308// destroyed and FS == 0x150B.
309//******************************************************************************
310void CloseLogFile()
311{
312 fclose(flog);
313 flog = 0;
314}
315//******************************************************************************
316//Used to open any private logfiles used in kernel32 (for now only in winimagepeldr.cpp)
317//******************************************************************************
318void OpenPrivateLogFiles()
319{
320 OpenPrivateLogFilePE();
321}
322//******************************************************************************
323//Used to close all private logfiles used in kernel32 (for now only in winimagepeldr.cpp)
324//******************************************************************************
325void ClosePrivateLogFiles()
326{
327 ClosePrivateLogFilePE();
328}
329//******************************************************************************
330//******************************************************************************
331int SYSTEM EXPORT WriteLogError(char *tekst, ...)
332{
333 USHORT sel = RestoreOS2FS();
334 va_list argptr;
335
336 va_start(argptr, tekst);
337 printf("ERROR: ");
338 vprintf(tekst, argptr);
339 va_end(argptr);
340 if(tekst[strlen(tekst)-1] != '\n')
341 printf("\n");
342
343 SetFS(sel);
344 return 1;
345}
346//******************************************************************************
347//******************************************************************************
348void SYSTEM CheckVersion(ULONG version, char *modname)
349{
350 dprintf(("CheckVersion of %s, %d\n", modname, version));
351 if(version != PE2LX_VERSION){
352 static char msg[300];
353 int r;
354 dprintf(("Version mismatch! %d, %d: %s\n", version, PE2LX_VERSION, modname));
355 sprintf(msg, "%s is intended for use with a different release of PE2LX.\n", modname);
356 do{
357 r = WinMessageBox(HWND_DESKTOP, NULLHANDLE, msg, "Version Mismatch!", 0, MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION | MB_MOVEABLE);
358 }while(r == MBID_RETRY); // giggle
359 if( r != MBID_IGNORE )
360 exit(987);
361 }
362}
363//******************************************************************************
364//******************************************************************************
365void SYSTEM CheckVersionFromHMOD(ULONG version, HMODULE hModule)
366{
367 char name[_MAX_PATH];
368
369 // query name of dll.
370 if(!DosQueryModuleName(hModule, sizeof(name), name))
371 CheckVersion(version, name);
372}
373//******************************************************************************
374//******************************************************************************
375#ifdef __WATCOMC__ /*PLF Sat 97-06-21 17:12:36*/
376 extern void interrupt3( void );
377 #pragma aux interrupt3= \
378 "int 3"
379#endif
380void WIN32API DebugBreak()
381{
382 dprintf(("DebugBreak\n"));
383
384 LPSTR lpstrEnv = getenv("WIN32.DEBUGBREAK"); /* query environment */
385 if (lpstrEnv == NULL) /* if environment is not set, don't call debugger ! */
386 return;
387
388#ifdef __WATCOMC__
389 interrupt3();
390#else
391 _interrupt(3);
392#endif
393}
394//******************************************************************************
395//******************************************************************************
396
397
398/*****************************************************************************
399 * Name : DebugErrorBox
400 * Purpose : display an apprioriate error box with detailed information
401 * about the error cause
402 * Parameters: APIRET iErrorCode - OS/2 error code
403 * PSZ pszFormat - printf-format string
404 * ...
405 * Variables :
406 * Result : return code of the message box
407 * Remark :
408 * Status :
409 *
410 * Author : Patrick Haller [Tue, 1999/09/13 19:55]
411 *****************************************************************************/
412
413int SYSTEM EXPORT DebugErrorBox(ULONG iErrorCode,
414 char* pszFormat,
415 ...)
416{
417 char szMessageBuffer[1024]; /* buffer for the text message */
418 char szErrorBuffer[1024]; /* buffer for the operating system text */
419 ULONG bc; /* dummy */
420 APIRET rc2; /* API returncode */
421 int iRC; /* message box return code */
422
423 USHORT sel = RestoreOS2FS();
424 va_list argptr;
425
426 // clear memory
427 memset (szMessageBuffer, 0, sizeof(szMessageBuffer));
428 memset (szErrorBuffer, 0, sizeof(szErrorBuffer));
429
430 // build message string
431 va_start(argptr, pszFormat);
432 vsprintf(szMessageBuffer, pszFormat, argptr);
433 va_end(argptr);
434
435 // query error string
436 rc2 = DosGetMessage(NULL,
437 0,
438 szErrorBuffer,
439 sizeof(szErrorBuffer),
440 iErrorCode,
441 "OSO001.MSG",
442 &bc);
443
444 // display message box
445 iRC = WinMessageBox(HWND_DESKTOP,
446 NULLHANDLE,
447 szMessageBuffer,
448 szErrorBuffer,
449 0,
450 MB_OK | MB_ICONEXCLAMATION | MB_MOVEABLE);
451 SetFS(sel);
452 return iRC;
453}
Note: See TracBrowser for help on using the repository browser.