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

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

Logfile changes

File size: 12.6 KB
Line 
1/* $Id: misc.cpp,v 1.9 1999-08-23 13:54:43 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 "os2util.h"
30#include "initterm.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
242#if 1 /*PLF Mon 97-09-08 20:04:28*/
243static FILE *flog = NULL; /*PLF Mon 97-09-08 20:00:15*/
244static BOOL init = FALSE;
245static BOOL fLogging = TRUE;
246
247int SYSTEM EXPORT WriteLog(char *tekst, ...)
248{
249 USHORT sel = RestoreOS2FS();
250 va_list argptr;
251
252 if(!init)
253 {
254 init = TRUE;
255
256 if(!getenv("NOWIN32LOG")) {
257 char logname[CCHMAXPATH];
258
259 sprintf(logname, "odin32_%d.log", loadNr);
260 flog = fopen(logname, "w");
261 }
262 else
263 fLogging = FALSE;
264 }
265
266 if(fLogging && flog)
267 {
268 va_start(argptr, tekst);
269 vfprintf(flog, tekst, argptr);
270 va_end(argptr);
271
272 if(tekst[strlen(tekst)-1] != '\n')
273 fprintf(flog, "\n");
274 }
275
276 SetFS(sel);
277 return 1;
278}
279
280//NOTE: No need to save/restore FS, as our FS selectors have already been
281// destroyed and FS == 0x150B.
282void CloseLogFile()
283{
284 fclose(flog);
285 flog = 0;
286}
287
288
289int SYSTEM EXPORT WriteLogError(char *tekst, ...)
290{
291 USHORT sel = RestoreOS2FS();
292 va_list argptr;
293
294 va_start(argptr, tekst);
295 printf("ERROR: ");
296 vprintf(tekst, argptr);
297 va_end(argptr);
298 if(tekst[strlen(tekst)-1] != '\n')
299 printf("\n");
300
301 SetFS(sel);
302 return 1;
303}
304#endif
305
306
307/*****************************************************************************
308 * Modified Standard Version *
309 *****************************************************************************/
310
311#if 0 /*PLF Mon 97-09-08 20:04:26*/
312/******************************************************************************/
313static BOOL init = FALSE;
314static BOOL fLog = TRUE;
315/******************************************************************************/
316int SYSTEM EXPORT WriteLog(char *tekst, ...)
317{
318ULONG Action, Wrote;
319HFILE log;
320APIRET rc;
321char message[4096];
322va_list argptr;
323ULONG openFlags = OPEN_ACTION_CREATE_IF_NEW;
324
325 if(fLog == FALSE)
326 return(FALSE);
327
328 if(!init) {
329 init = TRUE;
330 openFlags |= OPEN_ACTION_REPLACE_IF_EXISTS;
331 if(getenv("NOWIN32LOG"))
332 fLog = FALSE;
333 }
334 else openFlags |= OPEN_ACTION_OPEN_IF_EXISTS;
335
336 rc = DosOpen(
337 "win32os2.log",
338 &log, /* file handle returned */
339 &Action,
340 0L,
341 FILE_NORMAL,
342 openFlags,
343 OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYWRITE,
344 (PEAOP2)NULL);
345
346 rc = DosSetFilePtr(log, 0, FILE_END, &Wrote);
347 va_start(argptr, tekst);
348 vsprintf(message, tekst, argptr);
349 va_end(argptr);
350
351 rc = DosWrite(log, message, strlen(message), &Wrote);
352
353 DosClose(log); /*PLF Mon 97-09-08 20:01:43*/
354 return(TRUE);
355}
356#endif /*PLF Mon 97-09-08 20:04:23*/
357
Note: See TracBrowser for help on using the repository browser.