-
Notifications
You must be signed in to change notification settings - Fork 2
/
AppLog.pas
158 lines (137 loc) · 4.02 KB
/
AppLog.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
150
151
152
153
154
155
156
157
158
{*
AppLog.pas Functions for sending messages to a runtime log file
----------
Begin: 2007/09/25
Last revision: $Date: 2009-08-11 17:59:07 $ $Author: rhupalo $
Version: $Revision: 1.2 $
Code Documentation Tags: Begin 2009-08-06, Last Revision: 2009-08-06
Project: APHI General Purpose Delphi Library
Website: http://www.naadsm.org/opensource/delphi
Author: Aaron Reeves <Aaron.Reeves@colostate.edu>
--------------------------------------------------
Copyright (C) 2007 Animal Population Health Institute, Colorado State University
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General
Public License as published by the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
}
unit AppLog;
interface
procedure setUseAppLog( const val: boolean );
procedure openAppLog( const logFileName: string );
procedure closeAppLog();
procedure writeToAppLog( const msg: string );
procedure addAppLog( const msg: string );
implementation
uses
SysUtils,
MyStrUtils,
WindowsUtils
;
(*
Documentation generation tags begin with {* or ///
Replacing these with (* or // foils the documentation generator
*)
var
_appLogFile: textFile; /// Text file for log entries
_useAppLog: boolean; /// Flag indicating whether to write entries
_appLogOpen: boolean; /// Flag indicating if the log can be written to
{*
Provides a means to close the log file
@param val If not true the log is closed and a "not use" status is set
}
procedure setUseAppLog( const val: boolean );
begin
if( not( val ) and _useAppLog and _appLogOpen ) then
closeAppLog()
;
_useAppLog := val;
end
;
{*
Opens the log file for writing
@param logFileName filename only, the path is the application's
@throws If the file can not be opened no error message is shown
@comment If the file exists then new entries are appended to the bottom
}
procedure openAppLog( const logFileName: string );
var
logFilePath: string;
begin
if( _useAppLog ) then
begin
try
logFilePath := appDir() + logFileName;
_appLogOpen := true;
assignFile( _appLogFile, logFilePath );
if( fileExists( logFilePath ) ) then
begin
append( _appLogFile );
writeln( _appLogFile, endl + endl );
end
else
rewrite( _appLogFile )
;
writeToAppLog( 'Application started.' );
except
// fail silently... for now.
_appLogOpen := false;
end;
end
;
end
;
{*
Closes the log file
@throws If the file can not be closed no error message is shown
@comment On proper closure last line states 'Application exited normally'
}
procedure closeAppLog();
begin
if( _appLogOpen ) then
begin
try
writeToAppLog( 'Application exited normally.' );
closeFile( _appLogFile );
except
// fail silently, for now.
end;
end
;
_appLogOpen := false;
end
;
{*
Adds text to the log file
@param msg Message for the file
}
procedure writeToAppLog( const msg: string );
begin
addAppLog( msg );
end
;
{*
Helper procedure called by writeToAppLog
@param msg Message for the file
@throws If the file can not be written to no error message is shown
@comment The current date and time are added to the beginning of the message
}
procedure addAppLog( const msg: string );
var
timestamp: string;
begin
if( _useAppLog and _appLogOpen ) then
begin
timestamp := dateTimeToStr( now() ) + ': ';
try
writeLn( _appLogFile, timestamp + msg );
except
// fail silently, for now.
end;
end
;
end
;
initialization
_useAppLog := false;
_appLogOpen := false;
end.