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