| 1 |
/* logger_test.c |
| 2 |
* |
| 3 |
* Copyright 2025 Francois Techene |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 6 |
* a copy of this software and associated documentation files (the |
| 7 |
* "Software"), to deal in the Software without restriction, including |
| 8 |
* without limitation the rights to use, copy, modify, merge, publish, |
| 9 |
* distribute, sublicense, and/or sell copies of the Software, and to |
| 10 |
* permit persons to whom the Software is furnished to do so, subject to |
| 11 |
* the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY |
| 20 |
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 21 |
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 22 |
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 23 |
* |
| 24 |
* Except as contained in this notice, the name(s) of the above copyright |
| 25 |
* holders shall not be used in advertising or otherwise to promote the sale, |
| 26 |
* use or other dealings in this Software without prior written |
| 27 |
* authorization. |
| 28 |
*/ |
| 29 |
|
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <string.h> |
| 33 |
|
| 34 |
#include "logger.h" |
| 35 |
|
| 36 |
#define APP_NAME "Logger Test" |
| 37 |
|
| 38 |
#define VERS_RES_ID 1 |
| 39 |
|
| 40 |
#define ABOUT_DIALOG_ID 130 |
| 41 |
#define ABOUT_OK_BTN_ID 1 |
| 42 |
|
| 43 |
#define MBAR_ID 128 |
| 44 |
|
| 45 |
#define APPLE_MENU_ID 128 |
| 46 |
#define APPLE_MENU_ABOUT_ID 1 |
| 47 |
|
| 48 |
#define FILE_MENU_ID 129 |
| 49 |
#define FILE_MENU_NEW_ID 1 |
| 50 |
#define FILE_MENU_QUIT_ID 2 |
| 51 |
|
| 52 |
void drawHeader(void); |
| 53 |
void testLogLevels(void); |
| 54 |
void runTests(void); |
| 55 |
|
| 56 |
void loadMenuBar(void); |
| 57 |
void setMenuDefaults(void); |
| 58 |
short onMenuClick(long menu_id); |
| 59 |
void doEvent(EventRecord event); |
| 60 |
|
| 61 |
void centerWindow(WindowPtr win); |
| 62 |
|
| 63 |
Rect drawBtn(int h, int v, Str255 str); |
| 64 |
void drawWindow(void); |
| 65 |
void showAbout(void); |
| 66 |
|
| 67 |
WindowPtr mainWindow; |
| 68 |
DialogPtr dialog; |
| 69 |
|
| 70 |
MenuHandle appleMenu; |
| 71 |
MenuHandle fileMenu; |
| 72 |
|
| 73 |
Rect testBtn; |
| 74 |
Rect printBtn; |
| 75 |
Rect logBtn; |
| 76 |
Rect errBtn; |
| 77 |
Rect warnBtn; |
| 78 |
Rect infoBtn; |
| 79 |
Rect debugBtn; |
| 80 |
|
| 81 |
Boolean isReady = false; |
| 82 |
Boolean quitting = false; |
| 83 |
Boolean isFirstUpdate = true; |
| 84 |
|
| 85 |
|
| 86 |
void |
| 87 |
drawHeader(void) |
| 88 |
{ |
| 89 |
logger.log(" \r\r"); |
| 90 |
logger.log(" ____________________ "); |
| 91 |
logger.log(" | __________________ | "); |
| 92 |
logger.log(" | | | | "); |
| 93 |
logger.log(" | | | | "); |
| 94 |
logger.log(" | | | | | | | "); |
| 95 |
logger.log(" | | _| | | "); |
| 96 |
logger.log(" | | | | "); |
| 97 |
logger.log(" | | -___- | | "); |
| 98 |
logger.log(" | |_________________| | "); |
| 99 |
logger.log(" | | "); |
| 100 |
logger.log(" | | "); |
| 101 |
logger.log(" | == ======= | "); |
| 102 |
logger.log(" | | "); |
| 103 |
logger.log(" |_____________________| "); |
| 104 |
logger.log(" |___________________| "); |
| 105 |
logger.log(" _ "); |
| 106 |
logger.log(" / / ___ __ _ __ _ ___ _ __ "); |
| 107 |
logger.log(" / / / _ \\ / _` |/ _` |/ _ \\ '__|"); |
| 108 |
logger.log("/ /__| (_) | (_| | (_| | __/ | "); |
| 109 |
logger.log("\\____/\\___/ \\__, |\\__, |\\___|_| "); |
| 110 |
logger.log(" |___/ |___/ "); |
| 111 |
logger.log("\r"); |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
void |
| 116 |
testLogLevels(void) |
| 117 |
{ |
| 118 |
logger.logLevel = NONE_LEVEL; |
| 119 |
|
| 120 |
logger.print("\r#### Testing NONE_LEVEL: Must not show any log:\r"); |
| 121 |
logger.debug("This debug log should not be visible"); |
| 122 |
logger.info("This info log should not be visible"); |
| 123 |
logger.warn("This warning log should not be visible"); |
| 124 |
logger.error("This error log should not be visible"); |
| 125 |
|
| 126 |
|
| 127 |
logger.logLevel = ERROR_LEVEL; |
| 128 |
|
| 129 |
logger.print("\r#### Testing ERROR_LEVEL:\r"); |
| 130 |
logger.debug("This debug log should not be visible !!!"); |
| 131 |
logger.info("This info log should not be visible !!!"); |
| 132 |
logger.warn("This warning log should not be visible !!!"); |
| 133 |
logger.error("This error log should be visible"); |
| 134 |
|
| 135 |
|
| 136 |
logger.logLevel = WARNING_LEVEL; |
| 137 |
|
| 138 |
logger.print("\r#### Testing WARNING_LEVEL:\r"); |
| 139 |
logger.debug("This debug log should not be visible !!!"); |
| 140 |
logger.info("This info log should not be visible !!!"); |
| 141 |
logger.warn("This warning log should be visible"); |
| 142 |
logger.error("This error log should be visible"); |
| 143 |
|
| 144 |
|
| 145 |
logger.logLevel = INFO_LEVEL; |
| 146 |
|
| 147 |
logger.print("\r#### Testing INFO_LEVEL:\r"); |
| 148 |
logger.debug("This debug log should not be visible !!!"); |
| 149 |
logger.info("This info log should be visible"); |
| 150 |
logger.warn("This warning log should be visible"); |
| 151 |
logger.error("This error log should be visible"); |
| 152 |
|
| 153 |
|
| 154 |
logger.logLevel = DEBUG_LEVEL; |
| 155 |
|
| 156 |
logger.print("\r#### Testing DEBUG_LEVEL:\r"); |
| 157 |
logger.debug("This is a debug message with number %d ", 1); |
| 158 |
logger.info("This is an info message with number %d ", 2); |
| 159 |
logger.warn("This is a warning with number %d ", 3); |
| 160 |
logger.error("This is an error with number %d ", 4); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
void |
| 165 |
runTests(void) |
| 166 |
{ |
| 167 |
#ifdef USE_LOGGER // Fail safe |
| 168 |
LogConsoleScrollToBottom(); |
| 169 |
#endif |
| 170 |
|
| 171 |
drawHeader(); |
| 172 |
testLogLevels(); |
| 173 |
|
| 174 |
logger.log("\rDone!"); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
int main(void) |
| 179 |
{ |
| 180 |
EventRecord event; |
| 181 |
|
| 182 |
short dlgItem; |
| 183 |
|
| 184 |
short eventMask = mDownMask + |
| 185 |
mUpMask + |
| 186 |
keyDownMask + |
| 187 |
keyUpMask + |
| 188 |
autoKeyMask + |
| 189 |
updateMask; |
| 190 |
|
| 191 |
SetEventMask(everyEvent); // So we can get keyUp event. |
| 192 |
FlushEvents(eventMask, 0); |
| 193 |
InitGraf(&thePort); |
| 194 |
InitFonts(); |
| 195 |
InitWindows(); |
| 196 |
InitMenus(); |
| 197 |
TEInit(); |
| 198 |
InitDialogs(0); |
| 199 |
InitCursor(); |
| 200 |
MaxApplZone(); |
| 201 |
|
| 202 |
// Loa menus |
| 203 |
// |
| 204 |
loadMenuBar(); |
| 205 |
|
| 206 |
// Init Window |
| 207 |
// |
| 208 |
mainWindow = GetNewWindow(128, 0L, (WindowPtr)-1L); |
| 209 |
drawWindow(); |
| 210 |
|
| 211 |
dialog = NULL; |
| 212 |
|
| 213 |
isReady = true; |
| 214 |
|
| 215 |
InitLogger(DEBUG_LEVEL, CONSOLE_OUT, NULL); |
| 216 |
|
| 217 |
// Handle events |
| 218 |
// |
| 219 |
while (!quitting) { |
| 220 |
if (GetNextEvent(eventMask, &event)) { |
| 221 |
|
| 222 |
if (dialog && IsDialogEvent(&event)) { |
| 223 |
DialogSelect(&event, &dialog, &dlgItem); |
| 224 |
} |
| 225 |
else { |
| 226 |
doEvent(event); |
| 227 |
} |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
return EXIT_SUCCESS; |
| 233 |
} |
| 234 |
|
| 235 |
///////////////////////////////////////////////////// |
| 236 |
// Windows |
| 237 |
// |
| 238 |
|
| 239 |
void |
| 240 |
centerWindow(WindowPtr win) |
| 241 |
{ |
| 242 |
Rect screenRect; |
| 243 |
Rect winRect; |
| 244 |
int width, height, scrWidth, scrHeight; |
| 245 |
int left, top; |
| 246 |
|
| 247 |
screenRect = screenBits.bounds; |
| 248 |
winRect = win->portRect; |
| 249 |
|
| 250 |
scrWidth = screenRect.right - screenRect.left; |
| 251 |
scrHeight = screenRect.bottom - screenRect.top; |
| 252 |
|
| 253 |
width = winRect.right - winRect.left; |
| 254 |
height = winRect.bottom - winRect.top; |
| 255 |
|
| 256 |
left = (scrWidth / 2) - (width / 2); |
| 257 |
top = (scrHeight / 2) - (height / 2); |
| 258 |
|
| 259 |
MoveWindow(win, left, top, FALSE); |
| 260 |
} |
| 261 |
|
| 262 |
Rect |
| 263 |
drawBtn(int h, int v, Str255 str) |
| 264 |
{ |
| 265 |
Rect r = { 0, 0, 15, 100 }; // top, left, bottom, right |
| 266 |
|
| 267 |
TextFont(monaco); |
| 268 |
TextFace(0); |
| 269 |
TextSize(9); |
| 270 |
|
| 271 |
OffsetRect(&r, h - 1, v - 1); |
| 272 |
FrameRect(&r); |
| 273 |
|
| 274 |
OffsetRect(&r, -1, -1); |
| 275 |
FillRect(&r, white); |
| 276 |
FrameRect(&r); |
| 277 |
|
| 278 |
MoveTo(r.left + 4, r.bottom - 4); |
| 279 |
|
| 280 |
DrawString(str); |
| 281 |
|
| 282 |
return r; |
| 283 |
} |
| 284 |
|
| 285 |
void |
| 286 |
drawWindow(void) |
| 287 |
{ |
| 288 |
SetPort(mainWindow); |
| 289 |
SetPortBits(&mainWindow->portBits); |
| 290 |
|
| 291 |
TextFont(helvetica); |
| 292 |
TextFace(bold); |
| 293 |
TextSize(18); |
| 294 |
|
| 295 |
MoveTo(20, 25); |
| 296 |
DrawString("\pLogger Test App"); |
| 297 |
|
| 298 |
TextFont(geneva); |
| 299 |
TextFace(0); |
| 300 |
TextSize(9); |
| 301 |
|
| 302 |
MoveTo(20, 50); |
| 303 |
DrawString("\pThis is a simple app to test the Logger library \ |
| 304 |
to help debug your Macintosh C applications."); |
| 305 |
|
| 306 |
MoveTo(20, 65); |
| 307 |
DrawString("\pThe Logger console should be visible at the bottom of the screen."); |
| 308 |
|
| 309 |
MoveTo(20, 90); |
| 310 |
DrawString("\pThe following buttons highlight the different logging \ |
| 311 |
functions of the logger."); |
| 312 |
|
| 313 |
printBtn = drawBtn(20, 110, "\plogger.print()"); |
| 314 |
logBtn = drawBtn(20, printBtn.bottom + 10, "\plogger.log()"); |
| 315 |
errBtn = drawBtn(20, logBtn.bottom + 10, "\plogger.error()"); |
| 316 |
warnBtn = drawBtn(20, errBtn.bottom + 10, "\plogger.warn()"); |
| 317 |
infoBtn = drawBtn(20, warnBtn.bottom + 10, "\plogger.info()"); |
| 318 |
debugBtn = drawBtn(20, infoBtn.bottom + 10, "\plogger.debug()"); |
| 319 |
|
| 320 |
testBtn = drawBtn(20, debugBtn.bottom + 25, "\pRun tests"); |
| 321 |
} |
| 322 |
|
| 323 |
void |
| 324 |
showAbout(void) |
| 325 |
{ |
| 326 |
Handle versionRes; |
| 327 |
Str255 versionStr = "\p"; |
| 328 |
char appName[32]; |
| 329 |
short item; |
| 330 |
unsigned char* p; |
| 331 |
|
| 332 |
// Get version details from the version resource |
| 333 |
// |
| 334 |
versionRes = GetResource('vers', VERS_RES_ID); |
| 335 |
if (versionRes) { |
| 336 |
HLock(versionRes); |
| 337 |
p = (unsigned char*)*versionRes; |
| 338 |
|
| 339 |
p += 6; // skip fixed header (6 bytes) |
| 340 |
p += 1 + p[0]; // skip short version string |
| 341 |
|
| 342 |
// Get the long verstion string |
| 343 |
// |
| 344 |
BlockMove(p, versionStr, p[0] + 1); |
| 345 |
|
| 346 |
HUnlock(versionRes); |
| 347 |
} |
| 348 |
|
| 349 |
strcpy(appName, APP_NAME); |
| 350 |
appName[strlen(appName)] = '\0'; |
| 351 |
|
| 352 |
|
| 353 |
ParamText(CtoPstr(appName), versionStr, "\p", "\p"); |
| 354 |
|
| 355 |
// Load the About dialog |
| 356 |
// |
| 357 |
dialog = GetNewDialog(ABOUT_DIALOG_ID, NULL, (WindowPtr)-1); |
| 358 |
|
| 359 |
if (!dialog) { |
| 360 |
return; |
| 361 |
} |
| 362 |
|
| 363 |
centerWindow(dialog); |
| 364 |
|
| 365 |
ShowWindow(dialog); |
| 366 |
|
| 367 |
do { |
| 368 |
ModalDialog(NULL, &item); |
| 369 |
} while (item != ABOUT_OK_BTN_ID); |
| 370 |
|
| 371 |
DisposeDialog(dialog); |
| 372 |
dialog = NULL; |
| 373 |
} |
| 374 |
|
| 375 |
///////////////////////////////////////////////////// |
| 376 |
// Menu Bar |
| 377 |
// |
| 378 |
|
| 379 |
void |
| 380 |
loadMenuBar() |
| 381 |
{ |
| 382 |
Handle mbar = GetNewMBar(MBAR_ID); |
| 383 |
SetMenuBar(mbar); |
| 384 |
|
| 385 |
appleMenu = GetMHandle(APPLE_MENU_ID); |
| 386 |
AddResMenu(appleMenu, 'DRVR'); |
| 387 |
|
| 388 |
fileMenu = GetMHandle(FILE_MENU_ID); |
| 389 |
|
| 390 |
setMenuDefaults(); |
| 391 |
DrawMenuBar(); |
| 392 |
} |
| 393 |
|
| 394 |
void |
| 395 |
setMenuDefaults() |
| 396 |
{ |
| 397 |
DisableItem(fileMenu, FILE_MENU_NEW_ID); |
| 398 |
} |
| 399 |
|
| 400 |
short |
| 401 |
onMenuClick(long menu_id) |
| 402 |
{ |
| 403 |
short menu, item; |
| 404 |
short ret = true; |
| 405 |
|
| 406 |
menu = HiWord(menu_id); |
| 407 |
item = LoWord(menu_id); |
| 408 |
|
| 409 |
if (isReady == 0) { |
| 410 |
return false; |
| 411 |
} |
| 412 |
|
| 413 |
switch (menu) { |
| 414 |
case APPLE_MENU_ID: |
| 415 |
switch (item) { |
| 416 |
case APPLE_MENU_ABOUT_ID: { |
| 417 |
showAbout(); |
| 418 |
break; |
| 419 |
} |
| 420 |
default: { |
| 421 |
Str255 da; |
| 422 |
GrafPtr savePort; |
| 423 |
|
| 424 |
GetItem(appleMenu, item, da); |
| 425 |
GetPort(&savePort); |
| 426 |
OpenDeskAcc(da); |
| 427 |
SetPort(savePort); |
| 428 |
break; |
| 429 |
} |
| 430 |
} |
| 431 |
break; |
| 432 |
case FILE_MENU_ID: |
| 433 |
switch(item) { |
| 434 |
case FILE_MENU_NEW_ID: |
| 435 |
break; |
| 436 |
|
| 437 |
case FILE_MENU_QUIT_ID: |
| 438 |
quitting = true; |
| 439 |
break; |
| 440 |
} |
| 441 |
break; |
| 442 |
default: |
| 443 |
ret = false; |
| 444 |
} |
| 445 |
|
| 446 |
HiliteMenu(0); |
| 447 |
return ret; |
| 448 |
} |
| 449 |
|
| 450 |
|
| 451 |
///////////////////////////////////////////////////// |
| 452 |
// Events |
| 453 |
// |
| 454 |
|
| 455 |
void |
| 456 |
doEvent(EventRecord event) |
| 457 |
{ |
| 458 |
WindowPtr eventWin; |
| 459 |
short eventIn; |
| 460 |
char key; |
| 461 |
Point localPt; |
| 462 |
|
| 463 |
if(logger.onEvent(event)) { |
| 464 |
return; |
| 465 |
} |
| 466 |
|
| 467 |
localPt = event.where; |
| 468 |
GlobalToLocal(&localPt); |
| 469 |
|
| 470 |
switch (event.what) { |
| 471 |
|
| 472 |
case nullEvent: |
| 473 |
break; |
| 474 |
|
| 475 |
case keyDown: |
| 476 |
//case autoKey: |
| 477 |
key = (char)(event.message & charCodeMask); |
| 478 |
if ((event.modifiers & cmdKey) != 0) { |
| 479 |
if (onMenuClick(MenuKey(key))) |
| 480 |
break; |
| 481 |
} |
| 482 |
break; |
| 483 |
|
| 484 |
case keyUp: |
| 485 |
key = (char)(event.message & charCodeMask); |
| 486 |
break; |
| 487 |
|
| 488 |
case mouseDown: |
| 489 |
eventIn = FindWindow(event.where, &eventWin); |
| 490 |
|
| 491 |
switch (eventIn) { |
| 492 |
case inMenuBar: |
| 493 |
onMenuClick(MenuSelect(event.where)); |
| 494 |
break; |
| 495 |
case inSysWindow: |
| 496 |
SystemClick(&event, eventWin); |
| 497 |
break; |
| 498 |
case inContent: |
| 499 |
if (PtInRect(localPt, &testBtn)) { |
| 500 |
runTests(); |
| 501 |
} |
| 502 |
else if (PtInRect(localPt, &printBtn)) { |
| 503 |
logger.print("Printing some text. It doesn't end with a new line. "); |
| 504 |
} |
| 505 |
else if (PtInRect(localPt, &logBtn)) { |
| 506 |
logger.log("This is a simple log"); |
| 507 |
} |
| 508 |
else if (PtInRect(localPt, &errBtn)) { |
| 509 |
logger.error("This is an error log"); |
| 510 |
} |
| 511 |
else if (PtInRect(localPt, &warnBtn)) { |
| 512 |
logger.warn("This is a warning log"); |
| 513 |
} |
| 514 |
else if (PtInRect(localPt, &infoBtn)) { |
| 515 |
logger.info("This is an info log"); |
| 516 |
} |
| 517 |
else if (PtInRect(localPt, &debugBtn)) { |
| 518 |
logger.debug("This is a debug log"); |
| 519 |
} |
| 520 |
break; |
| 521 |
case inDrag: |
| 522 |
DragWindow(eventWin, event.where, &screenBits.bounds); |
| 523 |
break; |
| 524 |
case inGoAway: |
| 525 |
if (TrackGoAway(eventWin, event.where)) { |
| 526 |
quitting = true; |
| 527 |
} |
| 528 |
break; |
| 529 |
} |
| 530 |
break; |
| 531 |
|
| 532 |
case updateEvt: |
| 533 |
|
| 534 |
eventWin = (WindowPtr)event.message; |
| 535 |
if (eventWin) { |
| 536 |
BeginUpdate(eventWin); |
| 537 |
|
| 538 |
// We don't redraw on the first update |
| 539 |
// |
| 540 |
if(!isFirstUpdate) { |
| 541 |
drawWindow(); |
| 542 |
} |
| 543 |
else { |
| 544 |
isFirstUpdate = false; |
| 545 |
} |
| 546 |
|
| 547 |
EndUpdate(eventWin); |
| 548 |
} |
| 549 |
break; |
| 550 |
|
| 551 |
case activateEvt: |
| 552 |
break; |
| 553 |
} |
| 554 |
} |