source: trunk/src/kernel32/misc.c@ 17

Last change on this file since 17 was 17, checked in by phaller, 26 years ago

Code cleanup #1 for build, mainly addresses linkage problems

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