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