source: trunk/src/ddraw/misc.c@ 46

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

* empty log message *

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