-
Notifications
You must be signed in to change notification settings - Fork 0
/
VVDbg.pas
149 lines (121 loc) · 3.15 KB
/
VVDbg.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
unit VVDbg;
//Various functions to log and debug.
{$UNDEF LogToTS}
interface
uses
sysutils,
{$IFDEF LogToTS}
classes,
{$ENDIF}
windows;
procedure VVDbgWriteln(S:String);
procedure VVDbgOut(S:String);
procedure WriteLogToEventLogger(const Msg: string; const SvcName: string);
procedure VVriteLogToEventLogger(const Msg: string; const SvcName: string);
procedure AddToLog(S: string);
procedure VVDbgAndLogString(S: string);
implementation
uses
VCL.SvcMgr;
{$IFDEF DEBUG}
{$IFDEF LogToTS}
var
TS: TStringList;
szBuff: array[0..MAX_PATH] of char;
{$ENDIF}
{$ENDIF}
// This functions writes an event of type "Information" to Windows Event Log
procedure VVriteLogToEventLogger(const Msg: string; const SvcName: string);
var
FEventLogger: TEventLogger;
begin
FEventLogger := TEventLogger.Create(SvcName);
try
FEventLogger.LogMessage(Msg, EVENTLOG_INFORMATION_TYPE, 0, 0);
finally
FreeAndNil(FEventLogger);
end;
end;
// This functions writes an event of type "Error" to Windows Event Log
procedure WriteLogToEventLogger(const Msg: string; const SvcName: string);
{$IFDEF DEBUG}
var
FEventLogger: TEventLogger;
{$ENDIF}
begin
{$IFDEF DEBUG}
FEventLogger := TEventLogger.Create(SvcName);
try
FEventLogger.LogMessage(Msg, EVENTLOG_ERROR_TYPE, 0, 0);
finally
FreeAndNil(FEventLogger);
end;
{$ENDIF}
end;
//This function logs a string into a file located in %TEMP% folder which name is the exe file name + ".log"
//If LogToTS is defined, the string is added to a TStringList object TS which is saved to the log file when the program exits
//If LogToTS is NOT defined, the string is added directly to the log file.
procedure AddToLog(S: string);
{$IFDEF DEBUG}
{$IFNDEF LogToTS}
var
LogFile:TextFile;
szBuff: array[0..MAX_PATH] of char;
LogFileName: string;
{$ENDIF}
{$ENDIF}
begin
{$IFDEF DEBUG}
{$IFDEF LogToTS}
TS.Add(S);
{$ELSE}
try
GetTempPath(MAX_PATH,szBuff);
LogFileName:=string(szBuff)+'\'+ExtractFileName(ParamStr(0))+'.log';
AssignFile(LogFile,LogFileName);
try
if FileExists(LogFileName) then Append(LogFile)
else rewrite(LogFile);
writeln(LogFile, S);
finally
closefile(Logfile);
end;
except
on e:Exception do VVDbgOut('!! Exception in AddToLog: '+E.Classname+': '+E.Message);
end;
{$ENDIF}
{$ENDIF}
end;
// This functions logs a string to the DebugOutput. Can be seen with SysSinternals "DbgView" for instance
Procedure VVDbgOut(S: string);
begin
{$IFDEF DEBUG}
OutputDebugString(PChar(ExtractFileName(ParamStr(0))+': '+S));
{$ENDIF}
end;
procedure VVDbgAndLogString(S: string);
begin
VVDbgOut(S);
AddToLog(S);
end;
//Write to stdout only if teh target is DEBUG
procedure VVDbgWriteln(S:String);
begin
{$IFDEF DEBUG}
writeln(S);
{$ENDIF}
end;
{$IFDEF DEBUG}
{$IFDEF LogToTS}
initialization
TS:=TStringList.CREATE;
TS.Sorted:=false;
TS.Clear;
finalization
GetTempPath(MAX_PATH,szBuff);
TS.Add('(Log created with LogToTS defined');
TS.SaveToFile(string(szBuff)+'\'+ExtractFileName(ParamStr(0))+'.log');
TS.Free;
{$ENDIF}
{$ENDIF}
end.