source: trunk/src/ddraw/misc.cpp@ 1456

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

log fixes (FS)

File size: 10.3 KB
Line 
1#define INCL_BASE
2#define INCL_WIN
3#define INCL_WINERRORS
4#define INCL_DOSFILEMGR
5#include <os2wrap.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <stdarg.h>
10#include <misc.h>
11#include "initterm.h"
12
13//#define PMPRINTF
14#ifdef PMPRINTF
15
16/* ----- Customization variables ----- */
17#define PRINTFID ""
18#define PRINTFMAXLEN 300
19#define PRINTFLINELEN 100
20#define PRINTFTHREADS 54
21#define PRINTFQNAME "\\QUEUES\\PRINTF32"
22
23/* ----- Includes and externals ----- */
24#include <stddef.h> /* .. */
25#include <time.h> /* .. */
26
27extern ULONG flAllocMem; /*Tue 03.03.1998: knut */
28
29/* ----- Local defines ----- */
30#define PRINTFIDSIZE sizeof(PRINTFID)
31#define PRINTFMAXBUF PRINTFIDSIZE+PRINTFLINELEN
32
33/* ----- Per-thread output buffer and current indices into line ---- */
34struct perthread
35{
36 LONG lineindex; /* where next char */
37 LONG tidemark; /* rightmost char */
38 int bell; /* TRUE if line has bell */
39 UCHAR line[PRINTFMAXBUF]; /* accumulator */
40};
41
42/* ----- Local static variables ----- */
43static ULONG ourpid=0; /* our process ID */
44static ULONG servepid=0; /* process IDs of the server */
45static HQUEUE qhandle=0; /* handle for the queue */
46static struct perthread *tps[PRINTFTHREADS+1]; /* -> per-thread data */
47
48/* ----- Local subroutine ----- */
49static int printf_(struct perthread *);
50
51/* ----------------------------------------------------------------- */
52/* The "printf" function. Note this has a variable number of */
53/* arguments. */
54/* ----------------------------------------------------------------- */
55int SYSTEM EXPORT WriteLog(char *f, ...)
56{
57 TIB *ptib; /* process/thread id structures */
58 PIB *ppib; /* .. */
59 TID ourtid; /* thread ID */
60 struct perthread *tp; /* pointer to per-thread data */
61 int rc; /* returncode */
62 ULONG urc; /* returncode */
63
64 urc=DosOpenQueue( &servepid,
65 &qhandle,
66 PRINTFQNAME); /* Open the Q */
67 /* Non-0 RC means Q does not exist or cannot be opened */
68 if (urc==343)
69 return 0; /* queue does not exist, so quit */
70 if (urc!=0)
71 return -1; /* report any other error */
72
73 /* First determine our thread ID (and hence get access to the */
74 /* correct per-thread data. If the per-thread data has not been */
75 /* allocated, then allocate it now. It is never freed, once */
76 /* allocated, as PRINTF is not notified of end-of-thread. */
77 DosGetInfoBlocks(&ptib,&ppib); /* get process/thread info */
78 ourtid=ptib->tib_ptib2->tib2_ultid; /* .. and copy TID */
79 if (ourtid>PRINTFTHREADS) /* too many threads .. */
80 return 0; /* .. so quit, quietly */
81 tp=tps[ourtid]; /* copy to local pointer */
82 if (tp==NULL)
83 {
84 /* uninitialized (NULL=0) */
85 /* allocate a per-thread structure */
86 tp=(struct perthread *)malloc(sizeof(struct perthread));
87 if (tp==NULL)
88 return -1; /* out of memory -- return error */
89 tps[ourtid]=tp; /* save for future calls */
90 strcpy(tp->line,PRINTFID); /* initialize: line.. */
91 tp->lineindex=PRINTFIDSIZE-1; /* ..where next char */
92 tp->tidemark =PRINTFIDSIZE-2; /* ..rightmost char */
93 tp->bell=FALSE; /* ..if line has bell */
94 if (ourpid==0)
95 ourpid=ppib->pib_ulpid; /* save PID for all to use */
96 }
97
98 { /* Block for declarations -- only needed if queue exists, etc. */
99 LONG count; /* count of characters formatted */
100 UCHAR buffer[PRINTFMAXLEN+1]; /* formatting area */
101 LONG i, newind; /* work */
102 UCHAR ch; /* .. */
103 va_list argptr; /* -> variable argument list */
104
105 va_start(argptr, f); /* get pointer to argument list */
106 count=vsprintf(buffer, f, argptr);
107 va_end(argptr); /* done with variable arguments */
108
109 if (count<0)
110 return count-1000;/* bad start */
111
112 if (count>PRINTFMAXLEN)
113 {
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 {
126 ch=buffer[i]; if (!ch) break;
127 switch(ch)
128 {
129 case '\r': /* carriage return */
130 tp->lineindex=PRINTFIDSIZE-1; /* back to start of line */
131 break;
132 case '\n': /* new line */
133 case '\f': /* form feed */
134 rc=printf_(tp); /* print a line */
135 if (rc!=0) return rc; /* error */
136 break;
137 case '\t': /* tab */
138 newind=tp->lineindex-PRINTFIDSIZE+1; /* offset into data */
139 newind=tp->lineindex+5-newind%5; /* new index requested */
140 if (newind>=PRINTFMAXBUF)
141 newind=PRINTFMAXBUF; /* clamp */
142 for (; tp->lineindex<newind; tp->lineindex++)
143 {
144 if (tp->lineindex>tp->tidemark)
145 { /* 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 {
168 rc=printf_(tp); /* print a line */
169 if (rc!=0)
170 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,
193 NULL,
194 (unsigned)size,
195 OBJ_GIVEABLE|PAG_WRITE|PAG_COMMIT|flAllocMem);
196 /*knut: added flAllocMem */
197
198 if (urc!=0)
199 return -2; /* error */
200
201 pszTo=addr; /* copy for clarity */
202 pszFrom=&(tp->line[0]); /* pointer to source */
203 strcpy(pszTo,pszFrom); /* copy the string to shared memory */
204
205 if (ourpid!=servepid)
206 { /* (no giveaway needed if to self) */
207 urc=DosGiveSharedMem(addr, servepid, PAG_READ); /* give access */
208 if (urc!=0)
209 return -3; /* error */
210 }
211
212 /* Write the selector, size, and timestamp to the queue */
213 if (tp->bell) size=-size; /* BELL passed by negation */
214 time(&timenow); /* optional - else use 0 */
215 urc=DosWriteQueue( qhandle, /* handle */
216 (unsigned)timenow, /* 'request' (timestamp) */
217 (unsigned)size, /* 'length' (length/bell) */
218 addr, /* 'address' (address) */
219 0); /* priority (FIFO if enabled) */
220 if (urc!=0)
221 return -4; /* error */
222 if (ourpid!=servepid)
223 { /* if given away.. */
224 urc=DosFreeMem(addr); /* .. *we* are done with it */
225 if (urc!=0)
226 return -5; /* error */
227 }
228 /* Reset the line buffer and indices */
229 tp->lineindex=PRINTFIDSIZE-1; /* where next char */
230 tp->tidemark =PRINTFIDSIZE-2; /* rightmost char */
231 tp->bell =FALSE; /* true if line has bell */
232 return 0; /* success! */
233} /* printf_ */
234#else
235
236
237/*****************************************************************************
238 * Standard Version *
239 *****************************************************************************/
240
241static FILE *flog = NULL; /*PLF Mon 97-09-08 20:00:15*/
242static BOOL init = FALSE;
243static BOOL fLogging = TRUE;
244
245int SYSTEM EXPORT WriteLog(char *tekst, ...)
246{
247 USHORT sel = RestoreOS2FS();
248 va_list argptr;
249
250 if(!init)
251 {
252 init = TRUE;
253
254 if(!getenv("NODDRAWLOG")) {
255 char logname[CCHMAXPATH];
256
257 flog = fopen("DDRAW.log", "w");
258 if(flog == NULL) {//probably running exe on readonly device
259 sprintf(logname, "%sddraw.log", ddrawPath);
260 flog = fopen(logname, "w");
261 }
262 }
263 else
264 fLogging = FALSE;
265 }
266
267 if(fLogging && flog)
268 {
269 va_start(argptr, tekst);
270 vfprintf(flog, tekst, argptr);
271 va_end(argptr);
272
273 if(tekst[strlen(tekst)-1] != '\n')
274 fprintf(flog, "\n");
275 }
276
277 SetFS(sel);
278 return 1;
279}
280//******************************************************************************
281//NOTE: No need to save/restore FS, as our FS selectors have already been
282// destroyed and FS == 0x150B.
283//******************************************************************************
284void CloseLogFile()
285{
286 fclose(flog);
287 flog = 0;
288}
289#endif
Note: See TracBrowser for help on using the repository browser.