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

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

Include os2wrap.h instead of os2.h

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