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