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