| 1 |
/** |
| 2 |
* @file protoDebug.cpp |
| 3 |
* |
| 4 |
* @brief Debugging functions |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "protoDebug.h" |
| 8 |
#include "protoPipe.h" // to support OpenDebugPipe() option |
| 9 |
|
| 10 |
#include <stdio.h> |
| 11 |
#include <stdlib.h> // for abort() |
| 12 |
#include <stdarg.h> // for variable args |
| 13 |
|
| 14 |
#ifdef WIN32 |
| 15 |
#ifndef _WIN32_WCE |
| 16 |
#include <io.h> |
| 17 |
#include <fcntl.h> |
| 18 |
#endif // ! _WIN32_CE |
| 19 |
#include <Windows.h> |
| 20 |
#include <tchar.h> |
| 21 |
#endif // WIN32 |
| 22 |
|
| 23 |
#ifdef __ANDROID__ |
| 24 |
#include <android/log.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#ifdef MACOSX |
| 28 |
#include <fcntl.h> |
| 29 |
#endif |
| 30 |
|
| 31 |
#if defined(PROTO_DEBUG) || defined(PROTO_MSG) |
| 32 |
// Note - the static debug_level, debug_log, etc variables are |
| 33 |
// now "wrapped" in function calls to avoid the |
| 34 |
// "static initialization order fiasco" |
| 35 |
|
| 36 |
/** |
| 37 |
* @brief set default to pick up errors as well as fatal errors by default. |
| 38 |
*/ |
| 39 |
static unsigned int DebugLevel(bool set = false, unsigned int value = PL_ERROR) |
| 40 |
{ |
| 41 |
static unsigned int debug_level = PL_ERROR; |
| 42 |
if (set) debug_level = value; |
| 43 |
return debug_level; |
| 44 |
} // end DebugLevel() |
| 45 |
|
| 46 |
/** |
| 47 |
* @brief log to stderr by default |
| 48 |
*/ |
| 49 |
static FILE* DebugLog(bool set = false, FILE* value = stderr) |
| 50 |
{ |
| 51 |
static FILE* debug_log = stderr; |
| 52 |
if (set) debug_log = value; |
| 53 |
return debug_log; |
| 54 |
} // end DebugLog() |
| 55 |
|
| 56 |
FILE* GetDebugLog() |
| 57 |
{ |
| 58 |
return DebugLog(); |
| 59 |
} // end GetDebugLog() |
| 60 |
|
| 61 |
// TBD - "wrap" these assert options and "debug_pipe" in the same way |
| 62 |
static ProtoAssertFunction* assert_function = NULL; |
| 63 |
static void* assert_data = NULL; |
| 64 |
|
| 65 |
#ifndef SIMULATE |
| 66 |
static ProtoPipe debug_pipe(ProtoPipe::MESSAGE); |
| 67 |
#endif |
| 68 |
|
| 69 |
void SetDebugLevel(unsigned int level) |
| 70 |
{ |
| 71 |
unsigned int debugLevel = DebugLevel(); |
| 72 |
#if defined(WIN32) && !defined(SIMULATE) |
| 73 |
FILE* debugLog = DebugLog(); |
| 74 |
if (0 != level) |
| 75 |
{ |
| 76 |
if ((0 == debugLevel) && ((stderr == debugLog) || (stdout == debugLog))) |
| 77 |
OpenDebugWindow(); |
| 78 |
} |
| 79 |
else |
| 80 |
{ |
| 81 |
if ((0 != debugLevel) && ((stderr == debugLog) || (debugLog == stdout))) |
| 82 |
CloseDebugWindow(); |
| 83 |
} |
| 84 |
#endif // WIN32 |
| 85 |
DebugLevel(true, level); // this sets the underlying static "debug_level" state variable |
| 86 |
if (level != debugLevel) |
| 87 |
PLOG(PL_INFO,"ProtoDebug>SetDebugLevel: debug level changed from %d to %d\n", debugLevel, level); |
| 88 |
} // end SetDebugLevel() |
| 89 |
|
| 90 |
unsigned int GetDebugLevel() |
| 91 |
{ |
| 92 |
return DebugLevel(); |
| 93 |
} |
| 94 |
|
| 95 |
bool OpenDebugLog(const char *path) |
| 96 |
{ |
| 97 |
PLOG(PL_INFO,"ProtoDebug>OpenDebugLog: debug log is being set to \"%s\"\n",path); |
| 98 |
#ifdef OPNET // JPH 4/26/06 |
| 99 |
if ('\0' == *path) return false; |
| 100 |
#endif // OPNET |
| 101 |
CloseDebugLog(); |
| 102 |
FILE* ptr = fopen(path, "w+"); |
| 103 |
if (ptr) |
| 104 |
{ |
| 105 |
#if defined(WIN32) && !defined(SIMULATE) |
| 106 |
FILE* debugLog = DebugLog(); |
| 107 |
if ((0 != DebugLevel()) && ((debugLog == stdout) || (debugLog == stderr))) |
| 108 |
CloseDebugWindow(); |
| 109 |
#endif // WIN32 && !SIMULATE |
| 110 |
DebugLog(true, ptr); |
| 111 |
return true; |
| 112 |
} |
| 113 |
else |
| 114 |
{ |
| 115 |
#if defined(WIN32) && !defined(SIMULATE) |
| 116 |
FILE* debugLog = DebugLog(); |
| 117 |
if ((0 != DebugLevel()) && (debugLog != stdout) && (debugLog != stderr)) |
| 118 |
OpenDebugWindow(); |
| 119 |
#endif // WIN32 && !SIMULATE |
| 120 |
DebugLog(true, stderr); |
| 121 |
PLOG(PL_ERROR, "OpenDebugLog: Error opening debug log file: %s\n", path); |
| 122 |
return false; |
| 123 |
} |
| 124 |
} // end OpenLogFile() |
| 125 |
|
| 126 |
void CloseDebugLog() |
| 127 |
{ |
| 128 |
FILE* debugLog = DebugLog(); |
| 129 |
if (debugLog && (debugLog != stderr) && (debugLog != stdout)) |
| 130 |
{ |
| 131 |
fclose(debugLog); |
| 132 |
#if defined(WIN32) && !defined(SIMULATE) |
| 133 |
if (0 != DebugLevel()) OpenDebugWindow(); |
| 134 |
#endif // WIN32 && !SIMULATE |
| 135 |
} |
| 136 |
#ifndef SIMULATE |
| 137 |
if (debug_pipe.IsOpen()) debug_pipe.Close(); |
| 138 |
#endif // !SIMULATE |
| 139 |
DebugLog(true, stderr); |
| 140 |
} // end CloseDebugLog() |
| 141 |
|
| 142 |
|
| 143 |
/** |
| 144 |
* @brief log debug messages to a datagram ProtoPipe (PLOG only) |
| 145 |
*/ |
| 146 |
bool OpenDebugPipe(const char* pipeName) |
| 147 |
{ |
| 148 |
#ifndef SIMULATE |
| 149 |
if (!debug_pipe.Connect(pipeName)) |
| 150 |
{ |
| 151 |
PLOG(PL_ERROR, "OpenDebugPipe: error opening/connecting debug_pipe!\n"); |
| 152 |
return false; |
| 153 |
} |
| 154 |
return true; |
| 155 |
#else |
| 156 |
return false; |
| 157 |
#endif // if/else !SIMULATE |
| 158 |
} // end OpenDebugPipe() |
| 159 |
|
| 160 |
void CloseDebugPipe() |
| 161 |
{ |
| 162 |
#ifndef SIMULATE |
| 163 |
if (debug_pipe.IsOpen()) debug_pipe.Close(); |
| 164 |
#endif |
| 165 |
} // end CloseDebugPipe() |
| 166 |
|
| 167 |
|
| 168 |
#if defined(WIN32) && !defined(SIMULATE) |
| 169 |
|
| 170 |
static unsigned int console_reference_count = 0; |
| 171 |
|
| 172 |
/** |
| 173 |
* @brief Alternative WIN32 debug window (useful for WinCE) |
| 174 |
* This is a simple window used instead of the |
| 175 |
* normal console (which is not available in WinCE) |
| 176 |
*/ |
| 177 |
class ProtoDebugWindow |
| 178 |
{ |
| 179 |
public: |
| 180 |
ProtoDebugWindow(); |
| 181 |
~ProtoDebugWindow(); |
| 182 |
|
| 183 |
bool IsOpen() {return (NULL != hwnd);} |
| 184 |
bool Create(); |
| 185 |
void Destroy(); |
| 186 |
void Print(const char* text, unsigned int len); |
| 187 |
|
| 188 |
void Popup() {if (hwnd) SetForegroundWindow(hwnd);} // brings debug window to foreground |
| 189 |
|
| 190 |
private: |
| 191 |
static DWORD WINAPI RunInThread(LPVOID lpParameter); |
| 192 |
DWORD Run(); |
| 193 |
static LRESULT CALLBACK MessageHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); |
| 194 |
|
| 195 |
enum {BUFFER_MAX = 8190}; |
| 196 |
HANDLE thread_handle; |
| 197 |
DWORD thread_id; |
| 198 |
DWORD parent_thread_id; |
| 199 |
HWND hwnd; |
| 200 |
char buffer[BUFFER_MAX+2]; |
| 201 |
unsigned int count; |
| 202 |
CRITICAL_SECTION buffer_section; |
| 203 |
int content_height; |
| 204 |
double scroll_fraction; |
| 205 |
|
| 206 |
}; // end class ProtoDebugWindow |
| 207 |
|
| 208 |
ProtoDebugWindow::ProtoDebugWindow() |
| 209 |
: thread_handle(NULL), thread_id(NULL), parent_thread_id(NULL), |
| 210 |
hwnd(NULL), count(0), |
| 211 |
content_height(0), scroll_fraction(0.0) |
| 212 |
{ |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
ProtoDebugWindow::~ProtoDebugWindow() |
| 217 |
{ |
| 218 |
Destroy(); |
| 219 |
} |
| 220 |
|
| 221 |
bool ProtoDebugWindow::Create() |
| 222 |
{ |
| 223 |
Destroy(); |
| 224 |
parent_thread_id = GetCurrentThreadId(); |
| 225 |
if (!(thread_handle = CreateThread(NULL, 0, RunInThread, this, 0, &thread_id))) |
| 226 |
{ |
| 227 |
return true; |
| 228 |
} |
| 229 |
else |
| 230 |
{ |
| 231 |
PLOG(PL_ERROR, "ProtoDebugWindow::Create() CreateThread() error: %s\n", GetErrorString()); |
| 232 |
return false; |
| 233 |
} |
| 234 |
} // end ProtoDebugWindow::Create() |
| 235 |
|
| 236 |
void ProtoDebugWindow::Destroy() |
| 237 |
{ |
| 238 |
parent_thread_id = NULL; |
| 239 |
if (NULL != thread_handle) |
| 240 |
{ |
| 241 |
if (thread_id != GetCurrentThreadId()) |
| 242 |
{ |
| 243 |
if (hwnd) PostMessage(hwnd, WM_CLOSE, 0, 0); |
| 244 |
WaitForSingleObject(thread_handle, INFINITE); |
| 245 |
CloseHandle(thread_handle); |
| 246 |
thread_handle = NULL; |
| 247 |
thread_id = NULL; |
| 248 |
} |
| 249 |
else if (hwnd) |
| 250 |
{ |
| 251 |
DestroyWindow(hwnd); |
| 252 |
} |
| 253 |
} |
| 254 |
} // end ProtoDebugWindow::Destroy() |
| 255 |
|
| 256 |
|
| 257 |
DWORD WINAPI ProtoDebugWindow::RunInThread(LPVOID lpParameter) |
| 258 |
{ |
| 259 |
DWORD result = ((ProtoDebugWindow*)lpParameter)->Run(); |
| 260 |
ExitThread(result); |
| 261 |
return result; |
| 262 |
} |
| 263 |
|
| 264 |
DWORD ProtoDebugWindow::Run() |
| 265 |
{ |
| 266 |
content_height = 0; |
| 267 |
scroll_fraction = 1.0; |
| 268 |
InitializeCriticalSection(&buffer_section); |
| 269 |
EnterCriticalSection(&buffer_section); |
| 270 |
memset(buffer, '\0', BUFFER_MAX+2); |
| 271 |
LeaveCriticalSection(&buffer_section); |
| 272 |
count = 0; |
| 273 |
|
| 274 |
HINSTANCE theInstance = GetModuleHandle(NULL); |
| 275 |
// Register our msg_window class |
| 276 |
WNDCLASS cl; |
| 277 |
cl.style = CS_HREDRAW | CS_VREDRAW; |
| 278 |
cl.lpfnWndProc = MessageHandler; |
| 279 |
cl.cbClsExtra = 0; |
| 280 |
cl.cbWndExtra = 0; |
| 281 |
cl.hInstance = theInstance; |
| 282 |
cl.hIcon = NULL; |
| 283 |
cl.hCursor = NULL; |
| 284 |
cl.hbrBackground = NULL; |
| 285 |
cl.lpszMenuName = NULL; |
| 286 |
|
| 287 |
|
| 288 |
TCHAR moduleName[256]; |
| 289 |
DWORD nameLen = GetModuleFileName(GetModuleHandle(NULL), moduleName, 256); |
| 290 |
if (0 != nameLen) |
| 291 |
_tcsncat(moduleName, _T(" Debug"), 256 - nameLen); |
| 292 |
const LPCTSTR myName = (0 != nameLen) ? moduleName : _T("ProtoDebugWindow"); // default name |
| 293 |
|
| 294 |
cl.lpszClassName = myName; |
| 295 |
|
| 296 |
if (!RegisterClass(&cl)) |
| 297 |
{ |
| 298 |
LPVOID lpMsgBuf; |
| 299 |
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 300 |
FORMAT_MESSAGE_FROM_SYSTEM | |
| 301 |
FORMAT_MESSAGE_IGNORE_INSERTS, |
| 302 |
NULL, GetLastError(), |
| 303 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language |
| 304 |
(LPTSTR) &lpMsgBuf, 0, NULL ); |
| 305 |
// Display the string. |
| 306 |
MessageBox( NULL, (LPCTSTR)lpMsgBuf, (LPCTSTR)"Error", MB_OK | MB_ICONINFORMATION ); |
| 307 |
// Free the buffer. |
| 308 |
LocalFree(lpMsgBuf); |
| 309 |
DeleteCriticalSection(&buffer_section); |
| 310 |
PLOG(PL_ERROR, "ProtoDebugWindow::Win32Init() Error registering message window class!\n"); |
| 311 |
return GetLastError(); |
| 312 |
} |
| 313 |
hwnd = CreateWindow(myName, |
| 314 |
myName, |
| 315 |
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | |
| 316 |
WS_SIZEBOX | WS_VISIBLE | WS_VSCROLL, |
| 317 |
CW_USEDEFAULT, CW_USEDEFAULT, |
| 318 |
CW_USEDEFAULT, CW_USEDEFAULT, |
| 319 |
NULL, NULL, theInstance, this); |
| 320 |
if (NULL == hwnd) |
| 321 |
{ |
| 322 |
Destroy(); |
| 323 |
LPVOID lpMsgBuf; |
| 324 |
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 325 |
FORMAT_MESSAGE_FROM_SYSTEM | |
| 326 |
FORMAT_MESSAGE_IGNORE_INSERTS, |
| 327 |
NULL, GetLastError(), 0, |
| 328 |
(LPTSTR)&lpMsgBuf, 0, NULL ); |
| 329 |
// Display the string. |
| 330 |
MessageBox( NULL, (LPCTSTR)lpMsgBuf, (LPCTSTR)_T("Error"), MB_OK | MB_ICONINFORMATION); |
| 331 |
// Free the buffer. |
| 332 |
LocalFree(lpMsgBuf); |
| 333 |
UnregisterClass(cl.lpszClassName, theInstance); |
| 334 |
DeleteCriticalSection(&buffer_section); |
| 335 |
return GetLastError(); |
| 336 |
} |
| 337 |
// Message loop |
| 338 |
MSG msg; |
| 339 |
while (GetMessage(&msg, NULL, 0, 0)) |
| 340 |
{ |
| 341 |
TranslateMessage(&msg); |
| 342 |
DispatchMessage(&msg); |
| 343 |
} |
| 344 |
UnregisterClass(cl.lpszClassName, theInstance); |
| 345 |
DeleteCriticalSection(&buffer_section); |
| 346 |
return msg.wParam; |
| 347 |
} // end ProtoDebugWindow::Run() |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
void ProtoDebugWindow::Print(const char* text, unsigned int len) |
| 352 |
{ |
| 353 |
ASSERT(NULL != hwnd); |
| 354 |
EnterCriticalSection(&buffer_section); |
| 355 |
if (len > BUFFER_MAX) |
| 356 |
{ |
| 357 |
memcpy(buffer, text + len - BUFFER_MAX, BUFFER_MAX); |
| 358 |
count = BUFFER_MAX; |
| 359 |
} |
| 360 |
else |
| 361 |
{ |
| 362 |
unsigned int space = BUFFER_MAX - count; |
| 363 |
unsigned int move = (len > space) ? len - space : 0; |
| 364 |
if (move) memmove(buffer, buffer+move, count - move); |
| 365 |
memcpy(buffer+count-move, text, len); |
| 366 |
count += len - move; |
| 367 |
} |
| 368 |
#ifdef _UNICODE |
| 369 |
*((wchar_t*)(buffer+count)) = 0; |
| 370 |
#else |
| 371 |
buffer[count] = '\0'; |
| 372 |
#endif // end if/else _UNICODE |
| 373 |
|
| 374 |
LeaveCriticalSection(&buffer_section); |
| 375 |
RECT rect; |
| 376 |
GetClientRect(hwnd, &rect); |
| 377 |
InvalidateRect(hwnd, &rect, FALSE); |
| 378 |
PostMessage(hwnd, WM_PAINT, 0, 0); |
| 379 |
} // end ProtoDebugWindow::Print() |
| 380 |
|
| 381 |
|
| 382 |
LRESULT CALLBACK ProtoDebugWindow::MessageHandler(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) |
| 383 |
{ |
| 384 |
switch (message) |
| 385 |
{ |
| 386 |
case WM_CREATE: |
| 387 |
{ |
| 388 |
CREATESTRUCT *info = (CREATESTRUCT*)lParam; |
| 389 |
ProtoDebugWindow* dbg = (ProtoDebugWindow*)info->lpCreateParams; |
| 390 |
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)dbg); |
| 391 |
RECT rect; |
| 392 |
GetClientRect(hwnd, &rect); |
| 393 |
SetScrollRange(hwnd, SB_VERT, 0, 0, FALSE); |
| 394 |
SetScrollPos(hwnd, SB_VERT, 0, FALSE); |
| 395 |
return 0; |
| 396 |
} |
| 397 |
|
| 398 |
case WM_SIZE: |
| 399 |
{ |
| 400 |
ProtoDebugWindow* dbg = (ProtoDebugWindow*)GetWindowLongPtr(hwnd, GWLP_USERDATA); |
| 401 |
int newHeight = HIWORD(lParam); |
| 402 |
if (dbg->content_height > newHeight) |
| 403 |
{ |
| 404 |
SetScrollRange(hwnd, SB_VERT, 0, newHeight, TRUE); |
| 405 |
} |
| 406 |
else |
| 407 |
{ |
| 408 |
SetScrollRange(hwnd, SB_VERT, 0, 0, TRUE); |
| 409 |
dbg->scroll_fraction = 1.0; |
| 410 |
} |
| 411 |
int pos = (int)((dbg->scroll_fraction * newHeight) + 0.5); |
| 412 |
SetScrollPos(hwnd, SB_VERT, pos, TRUE); |
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
case WM_VSCROLL: |
| 417 |
{ |
| 418 |
ProtoDebugWindow* dbg = (ProtoDebugWindow*)GetWindowLongPtr(hwnd, GWLP_USERDATA); |
| 419 |
RECT rect; |
| 420 |
GetClientRect(hwnd, &rect); |
| 421 |
switch (LOWORD(wParam)) |
| 422 |
{ |
| 423 |
case SB_BOTTOM: |
| 424 |
SetScrollPos(hwnd, SB_VERT, rect.bottom, TRUE); |
| 425 |
dbg->scroll_fraction = 1.0; |
| 426 |
break; |
| 427 |
case SB_TOP: |
| 428 |
SetScrollPos(hwnd, SB_VERT, 0, TRUE); |
| 429 |
dbg->scroll_fraction = 0.0; |
| 430 |
break; |
| 431 |
case SB_THUMBPOSITION: |
| 432 |
case SB_THUMBTRACK: |
| 433 |
{ |
| 434 |
int pos = HIWORD(wParam); |
| 435 |
SetScrollPos(hwnd, SB_VERT, pos, TRUE); |
| 436 |
dbg->scroll_fraction = (double)pos / rect.bottom; |
| 437 |
break; |
| 438 |
} |
| 439 |
default: |
| 440 |
break; |
| 441 |
} |
| 442 |
InvalidateRect(hwnd, &rect, FALSE); |
| 443 |
PostMessage(hwnd, WM_PAINT, 0, 0); |
| 444 |
return 0; |
| 445 |
} |
| 446 |
|
| 447 |
case WM_PAINT: |
| 448 |
{ |
| 449 |
ProtoDebugWindow* dbg = (ProtoDebugWindow*)GetWindowLongPtr(hwnd, GWLP_USERDATA); |
| 450 |
char tempBuffer[BUFFER_MAX+2]; |
| 451 |
EnterCriticalSection(&dbg->buffer_section); |
| 452 |
memcpy(tempBuffer, dbg->buffer, BUFFER_MAX+2); |
| 453 |
LeaveCriticalSection(&dbg->buffer_section); |
| 454 |
PAINTSTRUCT ps; |
| 455 |
HDC hdc = BeginPaint(hwnd, &ps); |
| 456 |
RECT rect; |
| 457 |
GetClientRect(hwnd, &rect); |
| 458 |
RECT textRect = rect; |
| 459 |
#ifndef _WIN32_WCE |
| 460 |
HFONT oldFont = (HFONT)SelectObject(hdc, GetStockObject(ANSI_FIXED_FONT)); |
| 461 |
#endif // !_WIN32_WCE |
| 462 |
HPEN oldPen = (HPEN)SelectObject(hdc, GetStockObject(BLACK_PEN)); |
| 463 |
int textHeight = |
| 464 |
DrawText(hdc, (LPCTSTR)tempBuffer, -1, &textRect, DT_CALCRECT | DT_LEFT | DT_NOPREFIX | DT_WORDBREAK); |
| 465 |
FillRect(hdc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH)); |
| 466 |
int windowHeight = rect.bottom - rect.top; |
| 467 |
if (textHeight > windowHeight) |
| 468 |
{ |
| 469 |
int delta = textHeight - windowHeight; |
| 470 |
delta = (int)((delta * dbg->scroll_fraction) + 0.5); |
| 471 |
rect.top -= delta; |
| 472 |
bool updateScroll = dbg->content_height <= windowHeight; |
| 473 |
dbg->content_height = textHeight; |
| 474 |
SetScrollRange(hwnd, SB_VERT, 0, rect.bottom, TRUE); |
| 475 |
} |
| 476 |
DrawText(hdc, (LPCTSTR)tempBuffer, -1, &rect, DT_LEFT | DT_NOPREFIX | DT_WORDBREAK); |
| 477 |
#ifndef _WIN32_WCE |
| 478 |
SelectObject(hdc, oldFont); |
| 479 |
#endif // _WIN32_WCE |
| 480 |
SelectObject(hdc, oldPen); |
| 481 |
EndPaint(hwnd, &ps); |
| 482 |
return 0; |
| 483 |
} |
| 484 |
|
| 485 |
case WM_DESTROY: |
| 486 |
{ |
| 487 |
ProtoDebugWindow* dbg = (ProtoDebugWindow*)GetWindowLongPtr(hwnd, GWLP_USERDATA); |
| 488 |
dbg->hwnd = NULL; |
| 489 |
console_reference_count = 0; |
| 490 |
PostQuitMessage(0); |
| 491 |
if (NULL != dbg->parent_thread_id) |
| 492 |
PostThreadMessage(dbg->parent_thread_id, WM_QUIT, 0, 0); |
| 493 |
return 0; |
| 494 |
} |
| 495 |
|
| 496 |
// (TBD) We could pick up WM_CLOSE to see if it's a user click or app command |
| 497 |
|
| 498 |
default: |
| 499 |
break; |
| 500 |
} |
| 501 |
return DefWindowProc(hwnd, message, wParam, lParam); |
| 502 |
} // end ProtoDebugWindow::MessageHandler() |
| 503 |
|
| 504 |
#ifdef _WIN32_WCE |
| 505 |
static ProtoDebugWindow debug_window; |
| 506 |
#endif // _WIN32_WCE |
| 507 |
|
| 508 |
#endif // WIN32 |
| 509 |
|
| 510 |
|
| 511 |
/** |
| 512 |
* @brief Display string if statement's debug level is large enough |
| 513 |
This call is DEPRECATED! |
| 514 |
*/ |
| 515 |
void ProtoDMSG(unsigned int level, const char *format, ...) |
| 516 |
{ |
| 517 |
if (level <= DebugLevel()) |
| 518 |
{ |
| 519 |
FILE* debugLog = DebugLog(); |
| 520 |
va_list args; |
| 521 |
va_start(args, format); |
| 522 |
#ifdef _WIN32_WCE |
| 523 |
if (debug_window.IsOpen() && ((stderr == debugLog) || (stdout == debugLog))) |
| 524 |
{ |
| 525 |
char charBuffer[2048]; |
| 526 |
charBuffer[2047] = '\0'; |
| 527 |
int count = _vsnprintf(charBuffer, 2047, format, args); |
| 528 |
#ifdef _UNICODE |
| 529 |
wchar_t wideBuffer[2048]; |
| 530 |
count = mbstowcs(wideBuffer, charBuffer, count); |
| 531 |
count *= sizeof(wchar_t); |
| 532 |
const char* theBuffer = (char*)wideBuffer; |
| 533 |
#else |
| 534 |
const char* theBuffer = charBuffer; |
| 535 |
#endif // if/else _UNICODE |
| 536 |
debug_window.Print(theBuffer, count); |
| 537 |
} |
| 538 |
else |
| 539 |
#endif // _WIN32_WCE |
| 540 |
{ |
| 541 |
vfprintf(debugLog, format, args); |
| 542 |
fflush(debugLog); |
| 543 |
} |
| 544 |
va_end(args); |
| 545 |
} |
| 546 |
} // end ProtoDMSG(); |
| 547 |
|
| 548 |
|
| 549 |
/** |
| 550 |
* @brief Provides a basic logging facility for Protlib that uses the typical logging levels |
| 551 |
* to allow different levels for logging and run-time debugging of protolib applications. |
| 552 |
*/ |
| 553 |
void ProtoLog(ProtoDebugLevel level, const char* format, ...) |
| 554 |
{ |
| 555 |
if (((unsigned int)level <= DebugLevel()) || (PL_ALWAYS == level)) |
| 556 |
{ |
| 557 |
va_list args; |
| 558 |
va_start(args, format); |
| 559 |
const char* header = ""; |
| 560 |
switch(level) |
| 561 |
{ // Print out the Logging Type before the message |
| 562 |
case PL_FATAL: header = "Proto Fatal: "; |
| 563 |
break; |
| 564 |
case PL_ERROR: header = "Proto Error: "; |
| 565 |
break; |
| 566 |
case PL_WARN: header = "Proto Warn: "; |
| 567 |
break; |
| 568 |
case PL_INFO: header = "Proto Info: "; |
| 569 |
break; |
| 570 |
case PL_DEBUG: header = "Proto Debug: "; |
| 571 |
break; |
| 572 |
case PL_TRACE: header = "Proto Trace: "; |
| 573 |
break; |
| 574 |
case PL_DETAIL: header = "Proto Detail: "; |
| 575 |
break; |
| 576 |
case PL_MAX: header = "Proto Max: "; |
| 577 |
break; |
| 578 |
default: |
| 579 |
break; |
| 580 |
} |
| 581 |
size_t headerLen = strlen(header); |
| 582 |
FILE* debugLog = DebugLog(); |
| 583 |
#ifdef _WIN32_WCE |
| 584 |
if (debug_window.IsOpen() && !debug_pipe.IsOpen() && ((stderr == debugLog) || (stdout == debugLog))) |
| 585 |
{ |
| 586 |
char charBuffer[8192]; |
| 587 |
charBuffer[8191] = '\0'; |
| 588 |
int count = _vsnprintf(charBuffer, 8191, format, args); |
| 589 |
#ifdef _UNICODE |
| 590 |
wchar_t wideBuffer[8192]; |
| 591 |
count = mbstowcs(wideBuffer, charBuffer, count); |
| 592 |
count *= sizeof(wchar_t); |
| 593 |
const char* theBuffer = (char*)wideBuffer; |
| 594 |
#else |
| 595 |
const char* theBuffer = charBuffer; |
| 596 |
#endif // if/else _UNICODE |
| 597 |
debug_window.Print(theBuffer, count); |
| 598 |
} |
| 599 |
else |
| 600 |
#endif // _WIN32_WCE |
| 601 |
#ifndef SIMULATE |
| 602 |
if (debug_pipe.IsOpen()) |
| 603 |
#else |
| 604 |
if (false)//always fails for simulation |
| 605 |
#endif // if/else n SIMULATE |
| 606 |
{ |
| 607 |
char buffer[8192]; |
| 608 |
buffer[8191] = '\0'; |
| 609 |
#ifdef _WIN32_WCE |
| 610 |
unsigned int count = (unsigned int)_vsnprintf(buffer, 8191, format, args) + 1; |
| 611 |
#else |
| 612 |
#ifdef WIN32 |
| 613 |
strcpy(buffer, header); |
| 614 |
unsigned int count = (unsigned int)_vsnprintf(buffer + headerLen, 8191-headerLen, format, args) + 1; |
| 615 |
count += (unsigned int)headerLen; |
| 616 |
#else |
| 617 |
strcpy(buffer, header); |
| 618 |
unsigned int count = (unsigned int)vsnprintf(buffer + headerLen, 8191-headerLen, format, args) + 1; |
| 619 |
count += headerLen; |
| 620 |
#endif |
| 621 |
#endif // if/else _WIN32_WCE |
| 622 |
if (count > 8192) count = 8192; |
| 623 |
#ifndef SIMULATE |
| 624 |
if (!debug_pipe.Send(buffer, count)) |
| 625 |
#else |
| 626 |
if (true) |
| 627 |
#endif //if/else n SIMULATE |
| 628 |
{ |
| 629 |
// We have no recourse but to go to stderr here |
| 630 |
fprintf(stderr, "PLOG() error: unable to send to debug pipe!!!\n"); |
| 631 |
vfprintf(stderr, format, args); |
| 632 |
fflush(stderr); |
| 633 |
} |
| 634 |
} |
| 635 |
else |
| 636 |
{ |
| 637 |
#ifdef __ANDROID__ |
| 638 |
android_LogPriority prio; |
| 639 |
switch(level) |
| 640 |
{ |
| 641 |
case PL_FATAL: |
| 642 |
prio = ANDROID_LOG_FATAL; |
| 643 |
break; |
| 644 |
case PL_ERROR: |
| 645 |
prio = ANDROID_LOG_ERROR; |
| 646 |
break; |
| 647 |
case PL_WARN: |
| 648 |
prio = ANDROID_LOG_WARN; |
| 649 |
break; |
| 650 |
case PL_INFO: |
| 651 |
prio = ANDROID_LOG_INFO; |
| 652 |
break; |
| 653 |
case PL_DEBUG: |
| 654 |
prio = ANDROID_LOG_DEBUG; |
| 655 |
break; |
| 656 |
case PL_TRACE: |
| 657 |
case PL_DETAIL: /* explicit fallthrough */ |
| 658 |
case PL_MAX: /* explicit fallthrough */ |
| 659 |
case PL_ALWAYS: /* explicit fallthrough */ |
| 660 |
prio = ANDROID_LOG_VERBOSE; |
| 661 |
break; |
| 662 |
default: |
| 663 |
prio = ANDROID_LOG_DEFAULT; |
| 664 |
break; |
| 665 |
} |
| 666 |
__android_log_vprint(prio, "protolib", format, args); |
| 667 |
#else |
| 668 |
fprintf(debugLog, "%s", header); |
| 669 |
if (vfprintf(debugLog, format, args) < 0) |
| 670 |
{ |
| 671 |
// perror() seems more resilient for some reason (at least on Mac OSX) |
| 672 |
perror(""); // flushes any partial printout from failed vfprintf() |
| 673 |
char buffer[8192]; |
| 674 |
buffer[8191] = '\0'; |
| 675 |
strcpy(buffer, header); |
| 676 |
va_end(args); |
| 677 |
va_start(args, format); |
| 678 |
int count = vsnprintf(buffer + headerLen, 8191 - headerLen, format, args); |
| 679 |
if ('\n' == buffer[headerLen + count - 1]) |
| 680 |
buffer[headerLen + count - 1] = '\0'; |
| 681 |
perror(buffer); |
| 682 |
clearerr(debugLog); |
| 683 |
} |
| 684 |
fflush(debugLog); |
| 685 |
#endif |
| 686 |
} |
| 687 |
va_end(args); |
| 688 |
} |
| 689 |
|
| 690 |
} // end ProtoLog() |
| 691 |
|
| 692 |
// LP 11-01-05 - replaced |
| 693 |
// #ifdef WIN32 |
| 694 |
#if defined(WIN32) && !defined(SIMULATE) |
| 695 |
// end LP |
| 696 |
|
| 697 |
/** |
| 698 |
* Open console for displaying debug messages |
| 699 |
*/ |
| 700 |
void OpenDebugWindow() |
| 701 |
{ |
| 702 |
// Open a console window and redirect stderr and stdout to it |
| 703 |
if (0 == console_reference_count) |
| 704 |
{ |
| 705 |
#ifndef _WIN32_WCE |
| 706 |
int fdStd; |
| 707 |
HANDLE hStd; |
| 708 |
CONSOLE_SCREEN_BUFFER_INFO coninfo; |
| 709 |
/* ensure references to current console are flushed and closed |
| 710 |
* before freeing the console. To get things set up in case we're |
| 711 |
* not a console application, first re-open the std streams to |
| 712 |
* NUL with no buffering, and close invalid file descriptors |
| 713 |
* 0, 1, and 2. The std streams will be redirected to the console |
| 714 |
* once it's created. */ |
| 715 |
|
| 716 |
if (_get_osfhandle(0) < 0) |
| 717 |
_close(0); |
| 718 |
freopen("//./NUL", "r", stdin); |
| 719 |
setvbuf(stdin, NULL, _IONBF, 0); |
| 720 |
if (_get_osfhandle(1) < 0) |
| 721 |
_close(1); |
| 722 |
freopen("//./NUL", "w", stdout); |
| 723 |
setvbuf(stdout, NULL, _IONBF, 0); |
| 724 |
if (_get_osfhandle(2) < 0) |
| 725 |
_close(2); |
| 726 |
freopen("//./NUL", "w", stderr); |
| 727 |
setvbuf(stderr, NULL, _IONBF, 0); |
| 728 |
|
| 729 |
FreeConsole(); |
| 730 |
|
| 731 |
if (!AllocConsole()) { |
| 732 |
//ERROR_WINDOW("Cannot allocate windows console!"); |
| 733 |
return; |
| 734 |
} |
| 735 |
//SetConsoleTitle("My Nice Console"); |
| 736 |
|
| 737 |
// set the screen buffer to be big enough to let us scroll text |
| 738 |
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &coninfo); |
| 739 |
coninfo.dwSize.Y = 1024; |
| 740 |
//coninfo.dwSize.X = 100; |
| 741 |
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), coninfo.dwSize); |
| 742 |
|
| 743 |
// redirect unbuffered STDIN to the console |
| 744 |
hStd = GetStdHandle(STD_INPUT_HANDLE); |
| 745 |
fdStd = _open_osfhandle((intptr_t)hStd, _O_TEXT); |
| 746 |
_dup2(fdStd, _fileno(stdin)); |
| 747 |
SetStdHandle(STD_INPUT_HANDLE, (HANDLE)_get_osfhandle(_fileno(stdin))); |
| 748 |
_close(fdStd); |
| 749 |
|
| 750 |
// redirect unbuffered STDOUT to the console |
| 751 |
hStd = GetStdHandle(STD_OUTPUT_HANDLE); |
| 752 |
fdStd = _open_osfhandle((intptr_t)hStd, _O_TEXT); |
| 753 |
_dup2(fdStd, _fileno(stdout)); |
| 754 |
SetStdHandle(STD_OUTPUT_HANDLE, (HANDLE)_get_osfhandle(_fileno(stdout))); |
| 755 |
_close(fdStd); |
| 756 |
|
| 757 |
// redirect unbuffered STDERR to the console |
| 758 |
hStd = GetStdHandle(STD_ERROR_HANDLE); |
| 759 |
fdStd = _open_osfhandle((intptr_t)hStd, _O_TEXT); |
| 760 |
_dup2(fdStd, _fileno(stderr)); |
| 761 |
SetStdHandle(STD_ERROR_HANDLE, (HANDLE)_get_osfhandle(_fileno(stderr))); |
| 762 |
_close(fdStd); |
| 763 |
|
| 764 |
// Set Con Attributes |
| 765 |
//SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), |
| 766 |
// FOREGROUND_GREEN | FOREGROUND_INTENSITY); |
| 767 |
SetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), |
| 768 |
ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT); |
| 769 |
//SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), |
| 770 |
// ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT); |
| 771 |
/* |
| 772 |
AllocConsole(); |
| 773 |
//int hCrt = _open_osfhandle((long) GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT); |
| 774 |
//FILE* hf = _fdopen(hCrt, "w" ); |
| 775 |
// *stdout = *hf; |
| 776 |
FILE* hf; |
| 777 |
freopen_s(&hf, "CONOUT$", "w", stdout); |
| 778 |
int i = setvbuf(stdout, NULL, _IONBF, 0 ); |
| 779 |
|
| 780 |
//hCrt = _open_osfhandle((long) GetStdHandle(STD_INPUT_HANDLE), _O_TEXT); |
| 781 |
//hf = _fdopen(hCrt, "r" ); |
| 782 |
// *stdin = *hf; |
| 783 |
freopen_s(&hf, "CONIN$", "r", stdin); |
| 784 |
|
| 785 |
//hCrt = _open_osfhandle((long) GetStdHandle(STD_ERROR_HANDLE), _O_TEXT); |
| 786 |
//hf = _fdopen(hCrt, "w" ); |
| 787 |
// *stderr = *hf; |
| 788 |
freopen_s(&hf, "CONERR$", "w", stderr); |
| 789 |
i = setvbuf(stderr, NULL, _IONBF, 0); |
| 790 |
*/ |
| 791 |
#endif // if !_WIN32_CE |
| 792 |
} |
| 793 |
#ifdef _WIN32_WCE |
| 794 |
if (!debug_window.IsOpen()) debug_window.Create(); |
| 795 |
#endif // _WIN32_WCE |
| 796 |
console_reference_count++; |
| 797 |
} // end OpenDebugWindow() |
| 798 |
|
| 799 |
#ifndef _WIN32_WCE |
| 800 |
static HWND GetDebugWindowHandle(void) |
| 801 |
{ |
| 802 |
#define MY_BUFSIZE 1024 // Buffer size for console window titles. |
| 803 |
HWND hwndFound; // This is what is returned to the caller. |
| 804 |
#ifdef _UNICODE |
| 805 |
wchar_t pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated |
| 806 |
// WindowTitle. |
| 807 |
wchar_t pszOldWindowTitle[MY_BUFSIZE]; // Contains original |
| 808 |
// WindowTitle. |
| 809 |
#else |
| 810 |
char pszNewWindowTitle[MY_BUFSIZE]; // Contains fabricated |
| 811 |
// WindowTitle. |
| 812 |
char pszOldWindowTitle[MY_BUFSIZE]; // Contains original |
| 813 |
// WindowTitle. |
| 814 |
#endif |
| 815 |
// Fetch current window title |
| 816 |
GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE); |
| 817 |
// Format a "unique" NewWindowTitle. |
| 818 |
wsprintf(pszNewWindowTitle, _T("%d/%d"), |
| 819 |
GetTickCount(), |
| 820 |
GetCurrentProcessId()); |
| 821 |
// Change current window title. |
| 822 |
SetConsoleTitle(pszNewWindowTitle); |
| 823 |
// Ensure window title has been updated. |
| 824 |
Sleep(40); |
| 825 |
// Look for NewWindowTitle. |
| 826 |
hwndFound=FindWindow(NULL, pszNewWindowTitle); |
| 827 |
// Restore original window title. |
| 828 |
SetConsoleTitle(pszOldWindowTitle); |
| 829 |
return(hwndFound); |
| 830 |
} // end GetDebugWindowHandle() |
| 831 |
#endif // !WIN32_WCE |
| 832 |
|
| 833 |
void PopupDebugWindow() |
| 834 |
{ |
| 835 |
#ifdef _WIN32_WCE |
| 836 |
if (debug_window.IsOpen()) |
| 837 |
debug_window.Popup(); |
| 838 |
else |
| 839 |
OpenDebugWindow(); |
| 840 |
#else |
| 841 |
HWND debugWindow = GetDebugWindowHandle(); |
| 842 |
if (debugWindow) |
| 843 |
SetForegroundWindow(debugWindow); |
| 844 |
else |
| 845 |
OpenDebugWindow(); |
| 846 |
#endif // if/else _WIN32_WCE |
| 847 |
} |
| 848 |
|
| 849 |
void CloseDebugWindow() |
| 850 |
{ |
| 851 |
if (console_reference_count > 0) |
| 852 |
{ |
| 853 |
console_reference_count--; |
| 854 |
if (0 == console_reference_count) |
| 855 |
{ |
| 856 |
#ifdef _WIN32_WCE |
| 857 |
debug_window.Destroy(); |
| 858 |
#else |
| 859 |
FreeConsole(); |
| 860 |
#endif // if/else _WIN32_CE |
| 861 |
} |
| 862 |
} |
| 863 |
} |
| 864 |
#endif // WIN32 |
| 865 |
|
| 866 |
#endif // PROTO_DEBUG || PROTO_MSG |
| 867 |
|
| 868 |
|
| 869 |
#ifdef PROTO_DEBUG |
| 870 |
|
| 871 |
void TRACE(const char *format, ...) |
| 872 |
{ |
| 873 |
FILE* debugLog = DebugLog(); |
| 874 |
va_list args; |
| 875 |
va_start(args, format); |
| 876 |
#ifdef _WIN32_WCE |
| 877 |
if (debug_window.IsOpen() && ((stderr == debugLog) || (stdout == debugLog))) |
| 878 |
{ |
| 879 |
char charBuffer[2048]; |
| 880 |
charBuffer[2047] = '\0'; |
| 881 |
int count = _vsnprintf(charBuffer, 2047, format, args); |
| 882 |
#ifdef _UNICODE |
| 883 |
wchar_t wideBuffer[2048]; |
| 884 |
count = mbstowcs(wideBuffer, charBuffer, count); |
| 885 |
count *= sizeof(wchar_t); |
| 886 |
const char* theBuffer = (char*)wideBuffer; |
| 887 |
#else |
| 888 |
const char* theBuffer = charBuffer; |
| 889 |
#endif // if/else _UNICODE |
| 890 |
debug_window.Print(theBuffer, count); |
| 891 |
} |
| 892 |
else |
| 893 |
#endif // _WIN32_WCE |
| 894 |
{ |
| 895 |
#ifdef __ANDROID__ |
| 896 |
__android_log_vprint(ANDROID_LOG_ERROR, "protolib", format, args); |
| 897 |
#else |
| 898 |
if (vfprintf(debugLog, format, args) < 0) |
| 899 |
{ |
| 900 |
// perror() seems more resilient for some reason (at least on Mac OSX) |
| 901 |
perror(""); |
| 902 |
char buffer[8192]; |
| 903 |
buffer[8191] = '\0'; |
| 904 |
va_end(args); |
| 905 |
va_start(args, format); |
| 906 |
int count = vsnprintf(buffer, 8191, format, args); |
| 907 |
if ('\n' == buffer[count - 1]) |
| 908 |
buffer[count - 1] = '\0'; |
| 909 |
perror(buffer); |
| 910 |
clearerr(debugLog); |
| 911 |
} |
| 912 |
fflush(debugLog); |
| 913 |
|
| 914 |
#endif // if/else __ANDROID__ |
| 915 |
} |
| 916 |
va_end(args); |
| 917 |
} // end TRACE(); |
| 918 |
|
| 919 |
void PROTO_ABORT(const char *format, ...) |
| 920 |
{ |
| 921 |
#ifndef _WIN32_WCE // TBD add an fprintf? |
| 922 |
FILE* debugLog = DebugLog(); |
| 923 |
va_list args; |
| 924 |
va_start(args, format); |
| 925 |
vfprintf(debugLog, format, args); |
| 926 |
fflush(debugLog); |
| 927 |
va_end(args); |
| 928 |
abort(); |
| 929 |
#endif // !_WIN32_WCE |
| 930 |
} |
| 931 |
|
| 932 |
void SetAssertFunction(ProtoAssertFunction* assertFunction, void* userData) |
| 933 |
{ |
| 934 |
assert_function = assertFunction; |
| 935 |
assert_data = userData; |
| 936 |
} // end SetAssertFunction() |
| 937 |
|
| 938 |
void ClearAssertFunction() |
| 939 |
{ |
| 940 |
assert_function = NULL; |
| 941 |
} // end ClearAssertFunction() |
| 942 |
|
| 943 |
bool HasAssertFunction() |
| 944 |
{ |
| 945 |
return (NULL != assert_function); |
| 946 |
} // end HasAssertHandler() |
| 947 |
|
| 948 |
void ProtoAssertHandler(bool condition, const char* conditionText, const char* fileName, int lineNumber) |
| 949 |
{ |
| 950 |
if (NULL != assert_function) assert_function(condition, conditionText, fileName, lineNumber, assert_data); |
| 951 |
} // end ProtoAssertHandler() |
| 952 |
|
| 953 |
#endif // PROTO_DEBUG |