[43] | 1 | Unit DebugUnit;
|
---|
| 2 |
|
---|
| 3 | // NewView - a new OS/2 Help Viewer
|
---|
[76] | 4 | // Copyright 2006/2007 Ronald Brill (rbri at rbri dot de)
|
---|
[43] | 5 | // This software is released under the GNU Public License - see readme.txt
|
---|
| 6 |
|
---|
| 7 | // Helper functions for debugging
|
---|
| 8 |
|
---|
| 9 | Interface
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | uses
|
---|
| 13 | Classes,
|
---|
| 14 | OS2Def,
|
---|
| 15 | PMWin,
|
---|
| 16 | SysUtils;
|
---|
| 17 |
|
---|
| 18 |
|
---|
| 19 | // -- Logging --
|
---|
| 20 | Type
|
---|
[70] | 21 | LogAspect = ( LogStartup,
|
---|
| 22 | LogShutdown,
|
---|
| 23 | LogSettings,
|
---|
| 24 | LogParse,
|
---|
| 25 | LogDisplay,
|
---|
| 26 | LogSearch,
|
---|
| 27 | LogNHM,
|
---|
| 28 | LogViewStub,
|
---|
| 29 | LogObjConstDest,
|
---|
| 30 | LogDebug
|
---|
| 31 | );
|
---|
[55] | 32 | LogAspects = SET OF LogAspect;
|
---|
[43] | 33 |
|
---|
| 34 | Procedure LogEvent(const aLogAspect: LogAspect; const anEventDescription: String);
|
---|
| 35 |
|
---|
| 36 |
|
---|
| 37 | // -- Profiling --
|
---|
| 38 |
|
---|
| 39 | // Starts the timer
|
---|
| 40 | Procedure PrfStartTimer;
|
---|
| 41 |
|
---|
| 42 | // Stops the timer
|
---|
| 43 | Procedure PrfStopTimer;
|
---|
| 44 |
|
---|
| 45 | // Writes the eventDesctiption together with
|
---|
| 46 | // the time since timer start to PMPrintF
|
---|
| 47 | // Procedure PrfTraceEvent(const anEventDescription: String);
|
---|
| 48 |
|
---|
[76] | 49 | Procedure SetLogAspects(const aCommaSeparatedListOfAspectNames : String);
|
---|
[43] | 50 |
|
---|
[76] | 51 |
|
---|
[43] | 52 | var
|
---|
| 53 | startTime : ULONG;
|
---|
| 54 | lastTime : ULONG;
|
---|
[76] | 55 | PMPrintfModuleHandle : HMODULE;
|
---|
| 56 | PMPrintfString : Function(aString : PChar) : ULONG; APIENTRY;
|
---|
| 57 | activeLogAspects : LogAspects;
|
---|
[43] | 58 |
|
---|
[76] | 59 |
|
---|
| 60 |
|
---|
[43] | 61 | Implementation
|
---|
[82] | 62 |
|
---|
[76] | 63 | uses
|
---|
| 64 | BseDos,
|
---|
| 65 | StringUtilsUnit;
|
---|
[43] | 66 |
|
---|
[76] | 67 | FUNCTION LoadPMPrinfFLib : integer;
|
---|
| 68 | Var
|
---|
| 69 | tmpDllName : CString;
|
---|
| 70 | tmpErrorInfo : CString;
|
---|
| 71 | tmpProcedureName : CSTRING;
|
---|
| 72 | tmpProcedureAddress : POINTER;
|
---|
| 73 | tmpRC : APIRET;
|
---|
| 74 |
|
---|
| 75 | begin
|
---|
| 76 | PMPrintfString := nil;
|
---|
| 77 |
|
---|
| 78 | tmpDllName:='PMPRINTF';
|
---|
| 79 | tmpRC := DosLoadModule(tmpErrorInfo, 255, tmpDllName, PMPrintfModuleHandle);
|
---|
| 80 | if tmpRC <> 0 then
|
---|
| 81 | begin
|
---|
| 82 | PMPrintfModuleHandle := 0;
|
---|
| 83 | result := 0;
|
---|
| 84 | exit;
|
---|
| 85 | end;
|
---|
| 86 |
|
---|
| 87 | tmpProcedureName := 'PmPrintfString';
|
---|
| 88 | tmpRC := DosQueryProcAddr(PMPrintfModuleHandle, 0, tmpProcedureName, tmpProcedureAddress);
|
---|
| 89 | if tmpRC <> 0 then
|
---|
| 90 | begin
|
---|
| 91 | DosFreeModule(PMPrintfModuleHandle);
|
---|
| 92 | PMPrintfModuleHandle := 0;
|
---|
| 93 | result := 0;
|
---|
| 94 | exit;
|
---|
| 95 | end;
|
---|
| 96 |
|
---|
| 97 | PMPrintfString := tmpProcedureAddress;
|
---|
| 98 | result := 0;
|
---|
| 99 | end;
|
---|
| 100 |
|
---|
| 101 |
|
---|
| 102 | Procedure PMPrintf(const aString : String);
|
---|
| 103 | Var
|
---|
| 104 | tmpPCharMessage : PChar;
|
---|
| 105 | Begin
|
---|
| 106 | if (0 <> PMPrintfModuleHandle) then
|
---|
| 107 | begin
|
---|
| 108 | tmpPCharMessage := StrAlloc(length(aString) + 1);
|
---|
| 109 | StrPCopy(tmpPCharMessage, aString);
|
---|
| 110 |
|
---|
| 111 | PmPrintfString(tmpPCharMessage);
|
---|
| 112 |
|
---|
| 113 | StrDispose(tmpPCharMessage);
|
---|
| 114 | end;
|
---|
| 115 | end;
|
---|
| 116 |
|
---|
| 117 |
|
---|
[43] | 118 | Function GetAspectPrefix(const aLogAspect: LogAspect): String;
|
---|
| 119 | Begin
|
---|
| 120 | Case aLogAspect of
|
---|
[61] | 121 | LogStartup : result := 'Startup';
|
---|
| 122 | LogShutdown : result := 'Start';
|
---|
| 123 | LogSettings : result := 'Settings';
|
---|
| 124 | LogParse : result := 'Parse';
|
---|
| 125 | LogDisplay : result := 'Display';
|
---|
| 126 | LogSearch : result := 'Search';
|
---|
[70] | 127 | LogNHM : result := 'NewHelpManager';
|
---|
[61] | 128 | LogViewStub : result := 'ViewStub';
|
---|
| 129 | LogObjConstDest : result := 'ObjConstDest';
|
---|
[70] | 130 | LogDebug : result := 'Debug';
|
---|
[61] | 131 | else result := 'Unknown';
|
---|
[43] | 132 | end;
|
---|
| 133 | End;
|
---|
| 134 |
|
---|
| 135 |
|
---|
[76] | 136 | Procedure SetLogAspects(const aCommaSeparatedListOfAspectNames : String);
|
---|
| 137 | Var
|
---|
| 138 | tmpAspects : TStringList;
|
---|
| 139 | i : Integer;
|
---|
| 140 | Begin
|
---|
| 141 | tmpAspects := TStringList.Create;
|
---|
| 142 | StrExtractStrings(tmpAspects, aCommaSeparatedListOfAspectNames, [','], #0);
|
---|
| 143 |
|
---|
| 144 | for i:=0 to tmpAspects.count-1 do
|
---|
| 145 | begin
|
---|
| 146 | if tmpAspects[i] = 'LogStartup' then activeLogAspects := activeLogAspects + [ LogStartup ];
|
---|
| 147 | if tmpAspects[i] = 'LogShutdown' then activeLogAspects := activeLogAspects + [ LogShutdown ];
|
---|
| 148 | if tmpAspects[i] = 'LogSettings' then activeLogAspects := activeLogAspects + [ LogSettings ];
|
---|
| 149 | if tmpAspects[i] = 'LogParse' then activeLogAspects := activeLogAspects + [ LogParse ];
|
---|
| 150 | if tmpAspects[i] = 'LogDisplay' then activeLogAspects := activeLogAspects + [ LogDisplay ];
|
---|
| 151 | if tmpAspects[i] = 'LogSearch' then activeLogAspects := activeLogAspects + [ LogSearch ];
|
---|
| 152 | if tmpAspects[i] = 'LogNHM' then activeLogAspects := activeLogAspects + [ LogNHM ];
|
---|
| 153 | if tmpAspects[i] = 'LogViewStub' then activeLogAspects := activeLogAspects + [ LogViewStub ];
|
---|
| 154 | if tmpAspects[i] = 'LogObjConstDest' then activeLogAspects := activeLogAspects + [ LogObjConstDest ];
|
---|
| 155 | if tmpAspects[i] = 'LogDebug' then activeLogAspects := activeLogAspects + [ LogDebug ];
|
---|
| 156 | end;
|
---|
| 157 |
|
---|
| 158 | tmpAspects.Destroy;
|
---|
| 159 | End;
|
---|
| 160 |
|
---|
| 161 |
|
---|
[43] | 162 | Procedure LogEvent(const aLogAspect: LogAspect; const anEventDescription: String);
|
---|
| 163 | Var
|
---|
| 164 | tmpMessage : String;
|
---|
| 165 | Begin
|
---|
[55] | 166 | if (aLogAspect IN activeLogAspects) then
|
---|
| 167 | begin
|
---|
| 168 | tmpMessage := 'Log[' + GetAspectPrefix(aLogAspect) + '] ' + anEventDescription;
|
---|
[76] | 169 | PmPrintf(tmpMessage);
|
---|
[55] | 170 | end;
|
---|
[43] | 171 | end;
|
---|
| 172 |
|
---|
| 173 |
|
---|
| 174 | Function GetSystemMSCount: ULONG;
|
---|
| 175 | Begin
|
---|
| 176 | result:= WinGetCurrentTime(AppHandle);
|
---|
| 177 | End;
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 | Procedure PrfStartTimer;
|
---|
| 181 | Begin
|
---|
| 182 | startTime := GetSystemMSCount;
|
---|
| 183 | lastTime := startTime;
|
---|
| 184 | End;
|
---|
| 185 |
|
---|
[76] | 186 |
|
---|
[43] | 187 | Procedure PrfStopTimer;
|
---|
| 188 | Begin
|
---|
| 189 | End;
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 | Procedure PrfTraceEvent(const anEventDescription: String);
|
---|
| 193 | Var
|
---|
| 194 | tmpTime : ULONG;
|
---|
| 195 | tmpMessage : String;
|
---|
| 196 | Begin
|
---|
| 197 | tmpTime := GetSystemMSCount;
|
---|
| 198 | tmpMessage := 'Prf: ' + IntToStr(tmpTime - lastTime) + 'ms ' + anEventDescription + IntToStr(tmpTime - startTime) + 'ms since start';
|
---|
| 199 |
|
---|
[76] | 200 | PMPrintf(tmpMessage);
|
---|
[43] | 201 |
|
---|
| 202 | lastTime := GetSystemMSCount;
|
---|
| 203 | end;
|
---|
[76] | 204 |
|
---|
| 205 |
|
---|
| 206 | Initialization
|
---|
| 207 | LoadPMPrinfFLib;
|
---|
[43] | 208 | END.
|
---|