source: branches/2.20_branch/Library/DebugUnit.pas@ 461

Last change on this file since 461 was 357, checked in by RBRi, 16 years ago

copyright fix

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