| 1 | /* $Id: cout.cpp,v 1.1 1999-09-06 02:20:02 bird Exp $ | 
|---|
| 2 | * | 
|---|
| 3 | * cout - cout replacement. | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright (c) 1998-1999 knut st. osmundsen | 
|---|
| 6 | * | 
|---|
| 7 | */ | 
|---|
| 8 |  | 
|---|
| 9 | /******************************************************************************* | 
|---|
| 10 | *   Defined Constants                                                          * | 
|---|
| 11 | *******************************************************************************/ | 
|---|
| 12 | #define INCL_DOSERRORS | 
|---|
| 13 | #define INCL_NOPMAPI | 
|---|
| 14 |  | 
|---|
| 15 | /******************************************************************************* | 
|---|
| 16 | *   Header Files                                                               * | 
|---|
| 17 | *******************************************************************************/ | 
|---|
| 18 | #include <os2.h> | 
|---|
| 19 |  | 
|---|
| 20 | #include <builtin.h> | 
|---|
| 21 | #include <stdarg.h> | 
|---|
| 22 |  | 
|---|
| 23 | #include "cout.h" | 
|---|
| 24 | #include "options.h" | 
|---|
| 25 | #include "sprintf.h" | 
|---|
| 26 | #include "yield.h" | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 | /******************************************************************************* | 
|---|
| 30 | *   Global Variables                                                           * | 
|---|
| 31 | *******************************************************************************/ | 
|---|
| 32 | kLog _log_; | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 | /** Constructor -> constructor(..) */ | 
|---|
| 36 | kLog::kLog() | 
|---|
| 37 | { | 
|---|
| 38 | constructor(); | 
|---|
| 39 | } | 
|---|
| 40 |  | 
|---|
| 41 |  | 
|---|
| 42 | /** Destructor */ | 
|---|
| 43 | kLog::~kLog() | 
|---|
| 44 | { | 
|---|
| 45 | } | 
|---|
| 46 |  | 
|---|
| 47 |  | 
|---|
| 48 | /** | 
|---|
| 49 | * Manual constructor. | 
|---|
| 50 | * @param    ulPort   Portaddress for the (com)port to use. default: OUTPUT_COM2 | 
|---|
| 51 | * @remark   (OUTPUT_COM1 and OUTPUT_COM2 are defined in the header file.) | 
|---|
| 52 | */ | 
|---|
| 53 | void kLog::constructor(ULONG ulPort /*= OUTPUT_COM2*/) | 
|---|
| 54 | { | 
|---|
| 55 | fHex = TRUE; | 
|---|
| 56 | this->ulPort = ulPort; | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 |  | 
|---|
| 60 | /** | 
|---|
| 61 | * Put a single char to the com-port. | 
|---|
| 62 | * @param     ch  Char to put. | 
|---|
| 63 | */ | 
|---|
| 64 | void kLog::putch(char ch) | 
|---|
| 65 | { | 
|---|
| 66 | if (!options.fLogging) | 
|---|
| 67 | return; | 
|---|
| 68 |  | 
|---|
| 69 | #if !defined(RING0) || defined(RING0_DEBUG_IN_RING3) | 
|---|
| 70 | putc(ch, stdout); | 
|---|
| 71 | #else | 
|---|
| 72 | while (!(_inp(ulPort + 5) & 0x20));     /* waits for port To Be ready */ | 
|---|
| 73 | _outp(ulPort, ch);                      /* put char */ | 
|---|
| 74 | #endif | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 | /** | 
|---|
| 79 | * Puts a string to the comport. | 
|---|
| 80 | * @param     psz  String to put. | 
|---|
| 81 | * @remark    Note that this function may yield the CPU. | 
|---|
| 82 | */ | 
|---|
| 83 | void kLog::putstr(const char *psz) | 
|---|
| 84 | { | 
|---|
| 85 | int   cch = 0; | 
|---|
| 86 |  | 
|---|
| 87 | while (*psz != '\0') | 
|---|
| 88 | { | 
|---|
| 89 | while (!(_inp(ulPort + 5) & 0x20));  /* waits for port To Be ready */ | 
|---|
| 90 | _outp(ulPort, *psz);                 /* put char and inc pointer */ | 
|---|
| 91 |  | 
|---|
| 92 | /* new line fix */ | 
|---|
| 93 | if (psz[1] == '\n' && psz[0] != '\r') | 
|---|
| 94 | { | 
|---|
| 95 | while (!(_inp(ulPort + 5) & 0x20));  /* waits for port To Be ready */ | 
|---|
| 96 | _outp(ulPort, '\r');                 /* put char and inc pointer */ | 
|---|
| 97 | cch++; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | psz++; | 
|---|
| 101 | if ((++cch % 3) == 0) /* 9600 bit/s -> 1066 bytes (9bit) -> 3 chars takes 3.2 ms to send */ | 
|---|
| 102 | Yield(); | 
|---|
| 103 | } | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 |  | 
|---|
| 107 | /** | 
|---|
| 108 | * Put an unsigned long value to the comport. | 
|---|
| 109 | * @returns   Reference to this object. | 
|---|
| 110 | * @param     ul  Number to put. | 
|---|
| 111 | * @remark    May yield the CPU. | 
|---|
| 112 | */ | 
|---|
| 113 | kLog& kLog::operator<<(unsigned long ul) | 
|---|
| 114 | { | 
|---|
| 115 | sprintf(szBuffer, fHex ? "%lx" : "%lu", ul); | 
|---|
| 116 | putstr(szBuffer); | 
|---|
| 117 | return *this; | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 |  | 
|---|
| 121 | /** | 
|---|
| 122 | * Put a pointer value to the comport. | 
|---|
| 123 | * @returns   Reference to this object. | 
|---|
| 124 | * @param     p  Pointer to put. | 
|---|
| 125 | * @remark    May yield the CPU. | 
|---|
| 126 | */ | 
|---|
| 127 | kLog& kLog::operator<<(const void *pv) | 
|---|
| 128 | { | 
|---|
| 129 | if (pv != NULL) | 
|---|
| 130 | { | 
|---|
| 131 | sprintf(szBuffer,"%#.8lx", pv); | 
|---|
| 132 | putstr(szBuffer); | 
|---|
| 133 | } | 
|---|
| 134 | else | 
|---|
| 135 | putstr("(null)"); | 
|---|
| 136 | return *this; | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 |  | 
|---|
| 140 |  | 
|---|
| 141 | /** | 
|---|
| 142 | * Put a signed long value to the comport. | 
|---|
| 143 | * @returns   Reference to this object. | 
|---|
| 144 | * @param     l  Number to put. | 
|---|
| 145 | * @remark    May yield the CPU. | 
|---|
| 146 | */ | 
|---|
| 147 | kLog& kLog::operator<<(long l) | 
|---|
| 148 | { | 
|---|
| 149 | sprintf(szBuffer, fHex ? "%lx" : "%ld", l); | 
|---|
| 150 | putstr(szBuffer); | 
|---|
| 151 | return *this; | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 |  | 
|---|
| 155 | /** | 
|---|
| 156 | * Set format. | 
|---|
| 157 | * @param     hex      Hex indicator. | 
|---|
| 158 | * @param     ignored  This parameter is ignored. | 
|---|
| 159 | */ | 
|---|
| 160 | void kLog::setf(int fHex, int ignored) | 
|---|
| 161 | { | 
|---|
| 162 | if (fHex == 1 || fHex == 0) | 
|---|
| 163 | this->fHex = fHex; | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 |  | 
|---|
| 167 | /** | 
|---|
| 168 | * Put a char to the comport. | 
|---|
| 169 | * @returns   Reference to this object. | 
|---|
| 170 | * @param     c  Char to put. | 
|---|
| 171 | */ | 
|---|
| 172 | kLog& kLog::operator<<(char c) | 
|---|
| 173 | { | 
|---|
| 174 | kLog::putch(c); | 
|---|
| 175 | return *this; | 
|---|
| 176 | } | 
|---|
| 177 |  | 
|---|
| 178 |  | 
|---|
| 179 | /** | 
|---|
| 180 | * Put a string to the COM-port. | 
|---|
| 181 | * @returns   Reference to this object. | 
|---|
| 182 | * @param     psz  Pointer to string. | 
|---|
| 183 | * @remark    May yield the CPU. | 
|---|
| 184 | */ | 
|---|
| 185 | kLog& kLog::operator<<(const char *psz) | 
|---|
| 186 | { | 
|---|
| 187 | kLog::putstr(psz); | 
|---|
| 188 | return *this; | 
|---|
| 189 | } | 
|---|
| 190 |  | 
|---|
| 191 |  | 
|---|
| 192 | /** | 
|---|
| 193 | * Put a string to the COM-port. | 
|---|
| 194 | * @returns   Reference to this object. | 
|---|
| 195 | * @param     psz  Pointer to string. | 
|---|
| 196 | * @remark    May yield the CPU. | 
|---|
| 197 | */ | 
|---|
| 198 | kLog& kLog::operator<<(const signed char *psz) | 
|---|
| 199 | { | 
|---|
| 200 | kLog::putstr((char*)psz); | 
|---|
| 201 | return *this; | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 |  | 
|---|
| 205 | /** | 
|---|
| 206 | * Put a string to the COM-port. | 
|---|
| 207 | * @returns   Reference to this object. | 
|---|
| 208 | * @param     psz  Pointer to string. | 
|---|
| 209 | * @remark    May yield the CPU. | 
|---|
| 210 | */ | 
|---|
| 211 | kLog& kLog::operator<<(const unsigned char *psz) | 
|---|
| 212 | { | 
|---|
| 213 | kLog::putstr((char*)psz); | 
|---|
| 214 | return *this; | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 |  | 
|---|
| 218 | /** | 
|---|
| 219 | * Put an (signed) int to the COM-port. | 
|---|
| 220 | * @returns   Reference to this object. | 
|---|
| 221 | * @param     a  Number to put. | 
|---|
| 222 | * @remark    May yield the CPU. | 
|---|
| 223 | */ | 
|---|
| 224 | kLog& kLog::operator<<(int a) | 
|---|
| 225 | { | 
|---|
| 226 | return *this << (long)a; | 
|---|
| 227 | } | 
|---|
| 228 |  | 
|---|
| 229 |  | 
|---|
| 230 | /** | 
|---|
| 231 | * Put an unsigned int to the COM-port. | 
|---|
| 232 | * @returns   Reference to this object. | 
|---|
| 233 | * @param     a  Number to put. | 
|---|
| 234 | * @remark    May yield the CPU. | 
|---|
| 235 | */ | 
|---|
| 236 | kLog& kLog::operator<<(unsigned int a) | 
|---|
| 237 | { | 
|---|
| 238 | return *this << (unsigned long)a; | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 |  | 
|---|
| 242 | /** | 
|---|
| 243 | * Put a signed short int to the COM-port. | 
|---|
| 244 | * @returns   Reference to this object. | 
|---|
| 245 | * @param     a  Number to put. | 
|---|
| 246 | * @remark    May yield the CPU. | 
|---|
| 247 | */ | 
|---|
| 248 | kLog& kLog::operator<<(short i) | 
|---|
| 249 | { | 
|---|
| 250 | return *this << (long)i; | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 |  | 
|---|
| 254 | /** | 
|---|
| 255 | * Put a unsigned short int to the COM-port. | 
|---|
| 256 | * @returns   Reference to this object. | 
|---|
| 257 | * @param     a  Number to put. | 
|---|
| 258 | * @remark    May yield the CPU. | 
|---|
| 259 | */ | 
|---|
| 260 | kLog& kLog::operator<<(unsigned short i) | 
|---|
| 261 | { | 
|---|
| 262 | return *this << (unsigned long)i; | 
|---|
| 263 | } | 
|---|
| 264 |  | 
|---|
| 265 |  | 
|---|
| 266 | /** | 
|---|
| 267 | * Init function for cout. | 
|---|
| 268 | * @param    ulPort   Portaddress for the (com)port to use. default: OUTPUT_COM2 | 
|---|
| 269 | * @remark   (OUTPUT_COM1 and OUTPUT_COM2 are defined in the header file.) | 
|---|
| 270 | */ | 
|---|
| 271 | void coutInit(USHORT usCom) | 
|---|
| 272 | { | 
|---|
| 273 | _log_.constructor(usCom); | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 |  | 
|---|