| 1 | Unit DebugUnit; | 
|---|
| 2 |  | 
|---|
| 3 | // NewView - a new OS/2 Help Viewer | 
|---|
| 4 | // Copyright 2006 Ronald Brill (rbri at rbri dot de) | 
|---|
| 5 | // This software is released under the GNU Public License - see readme.txt | 
|---|
| 6 |  | 
|---|
| 7 | // Helper functions for debugging | 
|---|
| 8 |  | 
|---|
| 9 | Interface | 
|---|
| 10 |  | 
|---|
| 11 | {$define DEBUG} | 
|---|
| 12 |  | 
|---|
| 13 |  | 
|---|
| 14 | uses | 
|---|
| 15 | Classes, | 
|---|
| 16 | OS2Def, | 
|---|
| 17 | PMWin, | 
|---|
| 18 | SysUtils; | 
|---|
| 19 |  | 
|---|
| 20 | {$ifdef DEBUG} | 
|---|
| 21 | imports | 
|---|
| 22 | Function PmPrintfString(aString:PChar):BYTE; APIENTRY; 'PMPRINTF' NAME 'PmPrintfString'; | 
|---|
| 23 | end; | 
|---|
| 24 | {$endif} | 
|---|
| 25 |  | 
|---|
| 26 | // -- Logging -- | 
|---|
| 27 | Type | 
|---|
| 28 | LogAspect = (LogStartup, LogShutdown, LogSettings, LogParse, LogDisplay, LogSearch, LogViewStub); | 
|---|
| 29 | LogAspects = SET OF LogAspect; | 
|---|
| 30 |  | 
|---|
| 31 | Procedure LogEvent(const aLogAspect: LogAspect; const anEventDescription: String); | 
|---|
| 32 |  | 
|---|
| 33 |  | 
|---|
| 34 | // -- Profiling -- | 
|---|
| 35 |  | 
|---|
| 36 | // Starts the timer | 
|---|
| 37 | Procedure PrfStartTimer; | 
|---|
| 38 |  | 
|---|
| 39 | // Stops the timer | 
|---|
| 40 | Procedure PrfStopTimer; | 
|---|
| 41 |  | 
|---|
| 42 | // Writes the eventDesctiption together with | 
|---|
| 43 | // the time since timer start to PMPrintF | 
|---|
| 44 | // Procedure PrfTraceEvent(const anEventDescription: String); | 
|---|
| 45 |  | 
|---|
| 46 | const | 
|---|
| 47 | activeLogAspects : LogAspects = [ | 
|---|
| 48 | LogStartup | 
|---|
| 49 | //                                        LogShutdown, | 
|---|
| 50 | //                                        LogSettings, | 
|---|
| 51 | //                                        LogParse, | 
|---|
| 52 | //                                        LogDisplay, | 
|---|
| 53 | //                                        LogSearch, | 
|---|
| 54 | //                                        LogViewStub | 
|---|
| 55 | ]; | 
|---|
| 56 |  | 
|---|
| 57 | var | 
|---|
| 58 | startTime : ULONG; | 
|---|
| 59 | lastTime : ULONG; | 
|---|
| 60 |  | 
|---|
| 61 | Implementation | 
|---|
| 62 |  | 
|---|
| 63 | Function GetAspectPrefix(const aLogAspect: LogAspect): String; | 
|---|
| 64 | Begin | 
|---|
| 65 | Case aLogAspect of | 
|---|
| 66 | LogStartup  :  result := 'Startup'; | 
|---|
| 67 | LogShutdown :  result := 'Start'; | 
|---|
| 68 | LogSettings :  result := 'Settings'; | 
|---|
| 69 | LogParse    :  result := 'Parse'; | 
|---|
| 70 | LogDisplay  :  result := 'Display'; | 
|---|
| 71 | LogSearch   :  result := 'Search'; | 
|---|
| 72 | LogViewStub :  result := 'ViewStub'; | 
|---|
| 73 | else           result := 'Unknown'; | 
|---|
| 74 | end; | 
|---|
| 75 | End; | 
|---|
| 76 |  | 
|---|
| 77 |  | 
|---|
| 78 | Procedure LogEvent(const aLogAspect: LogAspect; const anEventDescription: String); | 
|---|
| 79 | {$ifdef DEBUG} | 
|---|
| 80 | Var | 
|---|
| 81 | tmpMessage : String; | 
|---|
| 82 | tmpPCharMessage : PChar; | 
|---|
| 83 | {$endif} | 
|---|
| 84 | Begin | 
|---|
| 85 | {$ifdef DEBUG} | 
|---|
| 86 | if (aLogAspect IN activeLogAspects) then | 
|---|
| 87 | begin | 
|---|
| 88 | tmpMessage := 'Log[' + GetAspectPrefix(aLogAspect) + ']  ' + anEventDescription; | 
|---|
| 89 |  | 
|---|
| 90 | tmpPCharMessage := StrAlloc(length(tmpMessage) + 1); | 
|---|
| 91 | StrPCopy(tmpPCharMessage, tmpMessage); | 
|---|
| 92 |  | 
|---|
| 93 | PmPrintfString(tmpPCharMessage); | 
|---|
| 94 | StrDispose(tmpPCharMessage); | 
|---|
| 95 | end; | 
|---|
| 96 | {$endif} | 
|---|
| 97 | end; | 
|---|
| 98 |  | 
|---|
| 99 |  | 
|---|
| 100 |  | 
|---|
| 101 |  | 
|---|
| 102 | Function GetSystemMSCount: ULONG; | 
|---|
| 103 | Begin | 
|---|
| 104 | result:= WinGetCurrentTime(AppHandle); | 
|---|
| 105 | End; | 
|---|
| 106 |  | 
|---|
| 107 |  | 
|---|
| 108 | Procedure PrfStartTimer; | 
|---|
| 109 | Begin | 
|---|
| 110 | {$ifdef DEBUG} | 
|---|
| 111 | startTime := GetSystemMSCount; | 
|---|
| 112 | lastTime := startTime; | 
|---|
| 113 | {$endif} | 
|---|
| 114 | End; | 
|---|
| 115 |  | 
|---|
| 116 | Procedure PrfStopTimer; | 
|---|
| 117 | Begin | 
|---|
| 118 | End; | 
|---|
| 119 |  | 
|---|
| 120 |  | 
|---|
| 121 | Procedure PrfTraceEvent(const anEventDescription: String); | 
|---|
| 122 | {$ifdef DEBUG} | 
|---|
| 123 | Var | 
|---|
| 124 | tmpTime : ULONG; | 
|---|
| 125 | tmpMessage : String; | 
|---|
| 126 | tmpPCharMessage : PChar; | 
|---|
| 127 | {$endif} | 
|---|
| 128 | Begin | 
|---|
| 129 | {$ifdef DEBUG} | 
|---|
| 130 | tmpTime := GetSystemMSCount; | 
|---|
| 131 | tmpMessage := 'Prf: ' + IntToStr(tmpTime - lastTime) + 'ms ' + anEventDescription + IntToStr(tmpTime - startTime) + 'ms since start'; | 
|---|
| 132 |  | 
|---|
| 133 | tmpPCharMessage := StrAlloc(length(tmpMessage) + 1); | 
|---|
| 134 | StrPCopy(tmpPCharMessage, tmpMessage); | 
|---|
| 135 |  | 
|---|
| 136 | PmPrintfString(tmpPCharMessage); | 
|---|
| 137 | StrDispose(tmpPCharMessage); | 
|---|
| 138 |  | 
|---|
| 139 | lastTime := GetSystemMSCount; | 
|---|
| 140 | {$endif} | 
|---|
| 141 | end; | 
|---|
| 142 | END. | 
|---|