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

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

* empty log message *

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