MathgeomGLS
103 строки · 3.9 Кб
1{---------------------------------------------------------------------------}
2{ }
3{ File: Velthuis.Loggers.pas }
4{ Function: Very simple logger type }
5{ Language: Delphi version XE3 or later }
6{ Author: Rudy Velthuis }
7{ Copyright: (c) 2016 Rudy Velthuis }
8{ }
9{ License: Redistribution and use in source and binary forms, with or }
10{ without modification, are permitted provided that the }
11{ following conditions are met: }
12{ }
13{ * Redistributions of source code must retain the above }
14{ copyright notice, this list of conditions and the following }
15{ disclaimer. }
16{ * Redistributions in binary form must reproduce the above }
17{ copyright notice, this list of conditions and the following }
18{ disclaimer in the documentation and/or other materials }
19{ provided with the distribution. }
20{ }
21{ Disclaimer: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" }
22{ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT }
23{ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND }
24{ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO }
25{ EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE }
26{ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, }
27{ OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, }
28{ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, }
29{ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED }
30{ AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT }
31{ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) }
32{ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF }
33{ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. }
34{ }
35{---------------------------------------------------------------------------}
36
37unit Velthuis.Loggers;
38
39interface
40
41uses
42System.Classes;
43
44type
45ILogger = interface
46['{B6821CA6-64F8-48B0-89D0-A9A3E6304D82}']
47procedure Log(Msg: string); overload;
48procedure Log(Format: string; Args: array of const); overload;
49end;
50
51TLogger = class(TInterfacedObject, ILogger)
52private
53FStream: TStream;
54FWriter: TStreamWriter;
55public
56constructor Create(S: TStream); overload;
57constructor Create(LogFileName: string); overload;
58destructor Destroy; override;
59procedure Log(Msg: string); overload;
60procedure Log(Format: string; Args: array of const); overload;
61end;
62
63var
64Logger: TLogger = nil;
65
66implementation
67
68uses
69System.SysUtils;
70
71{ TLogger }
72
73constructor TLogger.Create(S: TStream);
74begin
75FStream := S;
76FWriter := TStreamWriter.Create(S);
77end;
78
79constructor TLogger.Create(LogFileName: string);
80var
81F: TFileStream;
82begin
83F := TFileStream.Create(LogFileName, fmCreate);
84Create(F);
85end;
86
87destructor TLogger.Destroy;
88begin
89FWriter.Free;
90FStream.Free;
91end;
92
93procedure TLogger.Log(Msg: string);
94begin
95FWriter.WriteLine(Msg);
96end;
97
98procedure TLogger.Log(Format: string; Args: array of const);
99begin
100FWriter.WriteLine(System.SysUtils.Format(Format, Args));
101end;
102
103end.
104