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