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