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

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

Logging change, DosAllocMem for Aurora change (PE loader) + fixed AllocFixedMem

File size: 12.4 KB
Line 
1/* $Id: misc.cpp,v 1.7 1999-08-21 19:47:30 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
30
31/*****************************************************************************
32 * PMPRINTF Version *
33 *****************************************************************************/
34
35#ifdef PMPRINTF
36
37/* ----- Customization variables ----- */
38#define PRINTFID ""
39#define PRINTFMAXLEN 300
40#define PRINTFLINELEN 100
41#define PRINTFTHREADS 54
42#define PRINTFQNAME "\\QUEUES\\PRINTF32"
43
44/* ----- Includes and externals ----- */
45#include <stddef.h> /* .. */
46#include <time.h> /* .. */
47
48extern ULONG flAllocMem; /*Tue 03.03.1998: knut */
49
50/* ----- Local defines ----- */
51#define PRINTFIDSIZE sizeof(PRINTFID)
52#define PRINTFMAXBUF PRINTFIDSIZE+PRINTFLINELEN
53
54
55/*****************************************************************************
56 * Structures *
57 *****************************************************************************/
58
59/* ----- Per-thread output buffer and current indices into line ---- */
60struct perthread {
61 LONG lineindex; /* where next char */
62 LONG tidemark; /* rightmost char */
63 int bell; /* TRUE if line has bell */
64 UCHAR line[PRINTFMAXBUF]; /* accumulator */
65 };
66
67/* ----- Local static variables ----- */
68static ULONG ourpid=0; /* our process ID */
69static ULONG servepid=0; /* process IDs of the server */
70static HQUEUE qhandle=0; /* handle for the queue */
71static struct perthread *tps[PRINTFTHREADS+1]; /* -> per-thread data */
72
73/* ----- Local subroutine ----- */
74static int printf_(struct perthread *);
75
76
77/* ----------------------------------------------------------------- */
78/* The "printf" function. Note this has a variable number of */
79/* arguments. */
80/* ----------------------------------------------------------------- */
81int SYSTEM EXPORT WriteLog(char *f, ...)
82 {
83 TIB *ptib; /* process/thread id structures */
84 PIB *ppib; /* .. */
85 TID ourtid; /* thread ID */
86 struct perthread *tp; /* pointer to per-thread data */
87 int rc; /* returncode */
88 ULONG urc; /* returncode */
89
90 urc=DosOpenQueue(&servepid, &qhandle, PRINTFQNAME); /* Open the Q */
91 /* Non-0 RC means Q does not exist or cannot be opened */
92 if (urc==343) return 0; /* queue does not exist, so quit */
93 if (urc!=0) return -1; /* report any other error */
94
95 /* First determine our thread ID (and hence get access to the */
96 /* correct per-thread data. If the per-thread data has not been */
97 /* allocated, then allocate it now. It is never freed, once */
98 /* allocated, as PRINTF is not notified of end-of-thread. */
99 DosGetInfoBlocks(&ptib,&ppib); /* get process/thread info */
100 ourtid=ptib->tib_ptib2->tib2_ultid; /* .. and copy TID */
101 if (ourtid>PRINTFTHREADS) /* too many threads .. */
102 return 0; /* .. so quit, quietly */
103 tp=tps[ourtid]; /* copy to local pointer */
104 if (tp==NULL) { /* uninitialized (NULL=0) */
105 /* allocate a per-thread structure */
106 tp=(struct perthread *)malloc(sizeof(struct perthread));
107 if (tp==NULL) return -1; /* out of memory -- return error */
108 tps[ourtid]=tp; /* save for future calls */
109 strcpy(tp->line,PRINTFID); /* initialize: line.. */
110 tp->lineindex=PRINTFIDSIZE-1; /* ..where next char */
111 tp->tidemark =PRINTFIDSIZE-2; /* ..rightmost char */
112 tp->bell=FALSE; /* ..if line has bell */
113 if (ourpid==0) ourpid=ppib->pib_ulpid; /* save PID for all to use */
114 }
115
116 { /* Block for declarations -- only needed if queue exists, etc. */
117 LONG count; /* count of characters formatted */
118 UCHAR buffer[PRINTFMAXLEN+1]; /* formatting area */
119 LONG i, newind; /* work */
120 UCHAR ch; /* .. */
121 va_list argptr; /* -> variable argument list */
122
123 va_start(argptr, f); /* get pointer to argument list */
124 count=vsprintf(buffer, f, argptr);
125 va_end(argptr); /* done with variable arguments */
126
127 if (count<0) return count-1000;/* bad start */
128
129 if (count>PRINTFMAXLEN) {
130 /* Disaster -- we are probably "dead", but just in case we */
131 /* are not, carry on with truncated data. */
132 count=PRINTFMAXLEN;
133 }
134 buffer[count]='\0'; /* ensure terminated */
135 /* OK, ready to go with the data now in BUFFER */
136 /* We copy from the formatted string to the output (line) buffer, */
137 /* taking note of certain control characters and sending a line */
138 /* the queue whenever we see a LF control, or when the line */
139 /* fills (causing a forced break). */
140 for (i=0; ; i++) {
141 ch=buffer[i]; if (!ch) break;
142 switch(ch) {
143 case '\r': /* carriage return */
144 tp->lineindex=PRINTFIDSIZE-1; /* back to start of line */
145 break;
146 case '\n': /* new line */
147 case '\f': /* form feed */
148 rc=printf_(tp); /* print a line */
149 if (rc!=0) return rc; /* error */
150 break;
151 case '\t': /* tab */
152 newind=tp->lineindex-PRINTFIDSIZE+1; /* offset into data */
153 newind=tp->lineindex+5-newind%5; /* new index requested */
154 if (newind>=PRINTFMAXBUF) newind=PRINTFMAXBUF; /* clamp */
155 for (; tp->lineindex<newind; tp->lineindex++) {
156 if (tp->lineindex>tp->tidemark) { /* beyond current end */
157 tp->line[tp->lineindex]=' '; /* add space */
158 tp->tidemark=tp->lineindex;
159 }
160 }
161 break;
162 case '\v': /* vertical tab */
163 /* ignore it */
164 break;
165 case '\b': /* backspace */
166 tp->lineindex=max(tp->lineindex-1,PRINTFIDSIZE);
167 break;
168 case '\a': /* alert (bell) */
169 tp->bell=TRUE;
170 break;
171 default: /* ordinary character */
172 tp->line[tp->lineindex]=ch;
173 if (tp->lineindex>tp->tidemark) /* is rightmost.. */
174 tp->tidemark=tp->lineindex;
175 tp->lineindex++; /* step for next */
176 } /* switch */
177 if (tp->lineindex>=PRINTFMAXBUF) {
178 rc=printf_(tp); /* print a line */
179 if (rc!=0) return rc; /* error */
180 }
181
182 } /* copy loop */
183 return count; /* all formatted data processed */
184 } /* block */
185 } /* printf */
186
187/* ----- printf_(tp) -- Local subroutine to send a line ------------ */
188/* A line has been completed (or overflowed): write it to the queue. */
189int printf_(struct perthread *tp) /* pointer to per-thread data */
190 {
191 ULONG urc; /* unsigned returncode */
192 PSZ pszTo, pszFrom; /* character pointers */
193 PVOID addr; /* address of output data */
194 long size; /* total size of output data */
195 time_t timenow; /* holds current time */
196
197 tp->line[tp->tidemark+1]='\0'; /* add terminator */
198 size=tp->tidemark+2; /* total length of data */
199
200 /* Get some shared memory that can be given away */
201 urc=DosAllocSharedMem(&addr, NULL, (unsigned)size,
202 OBJ_GIVEABLE|PAG_WRITE|PAG_COMMIT|flAllocMem);
203 /*knut: added flAllocMem */
204
205 if (urc!=0) return -2; /* error */
206
207 pszTo=addr; /* copy for clarity */
208 pszFrom=&(tp->line[0]); /* pointer to source */
209 strcpy(pszTo,pszFrom); /* copy the string to shared memory */
210
211 if (ourpid!=servepid) { /* (no giveaway needed if to self) */
212 urc=DosGiveSharedMem(addr, servepid, PAG_READ); /* give access */
213 if (urc!=0) return -3;} /* error */
214
215 /* Write the selector, size, and timestamp to the queue */
216 if (tp->bell) size=-size; /* BELL passed by negation */
217 time(&timenow); /* optional - else use 0 */
218 urc=DosWriteQueue(qhandle, /* handle */
219 (unsigned)timenow, /* 'request' (timestamp) */
220 (unsigned)size, /* 'length' (length/bell) */
221 addr, /* 'address' (address) */
222 0); /* priority (FIFO if enabled) */
223 if (urc!=0) return -4; /* error */
224 if (ourpid!=servepid) { /* if given away.. */
225 urc=DosFreeMem(addr); /* .. *we* are done with it */
226 if (urc!=0) return -5;} /* error */
227 /* Reset the line buffer and indices */
228 tp->lineindex=PRINTFIDSIZE-1; /* where next char */
229 tp->tidemark =PRINTFIDSIZE-2; /* rightmost char */
230 tp->bell =FALSE; /* true if line has bell */
231 return 0; /* success! */
232 } /* printf_ */
233#endif
234
235
236
237/*****************************************************************************
238 * Standard Version *
239 *****************************************************************************/
240
241#if 1 /*PLF Mon 97-09-08 20:04:28*/
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 if(!getenv("NOWIN32LOG"))
256 flog = fopen("win32os2.log", "w");
257 else
258 fLogging = FALSE;
259 }
260
261 if(fLogging && flog)
262 {
263 va_start(argptr, tekst);
264 vfprintf(flog, tekst, argptr);
265 va_end(argptr);
266
267 if(tekst[strlen(tekst)-1] != '\n')
268 fprintf(flog, "\n");
269 }
270
271 SetFS(sel);
272 return 1;
273}
274
275void CloseLogFile()
276{
277 USHORT sel = RestoreOS2FS();
278 fclose(flog);
279 SetFS(sel);
280}
281
282
283int SYSTEM EXPORT WriteLogError(char *tekst, ...)
284{
285 USHORT sel = RestoreOS2FS();
286 va_list argptr;
287
288 va_start(argptr, tekst);
289 printf("ERROR: ");
290 vprintf(tekst, argptr);
291 va_end(argptr);
292 if(tekst[strlen(tekst)-1] != '\n')
293 printf("\n");
294
295 SetFS(sel);
296 return 1;
297}
298#endif
299
300
301/*****************************************************************************
302 * Modified Standard Version *
303 *****************************************************************************/
304
305#if 0 /*PLF Mon 97-09-08 20:04:26*/
306/******************************************************************************/
307static BOOL init = FALSE;
308static BOOL fLog = TRUE;
309/******************************************************************************/
310int SYSTEM EXPORT WriteLog(char *tekst, ...)
311{
312ULONG Action, Wrote;
313HFILE log;
314APIRET rc;
315char message[4096];
316va_list argptr;
317ULONG openFlags = OPEN_ACTION_CREATE_IF_NEW;
318
319 if(fLog == FALSE)
320 return(FALSE);
321
322 if(!init) {
323 init = TRUE;
324 openFlags |= OPEN_ACTION_REPLACE_IF_EXISTS;
325 if(getenv("NOWIN32LOG"))
326 fLog = FALSE;
327 }
328 else openFlags |= OPEN_ACTION_OPEN_IF_EXISTS;
329
330 rc = DosOpen(
331 "win32os2.log",
332 &log, /* file handle returned */
333 &Action,
334 0L,
335 FILE_NORMAL,
336 openFlags,
337 OPEN_ACCESS_READWRITE | OPEN_SHARE_DENYWRITE,
338 (PEAOP2)NULL);
339
340 rc = DosSetFilePtr(log, 0, FILE_END, &Wrote);
341 va_start(argptr, tekst);
342 vsprintf(message, tekst, argptr);
343 va_end(argptr);
344
345 rc = DosWrite(log, message, strlen(message), &Wrote);
346
347 DosClose(log); /*PLF Mon 97-09-08 20:01:43*/
348 return(TRUE);
349}
350#endif /*PLF Mon 97-09-08 20:04:23*/
351
Note: See TracBrowser for help on using the repository browser.