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