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