MathgeomGLS

Форк
0
/
Velthuis.Loggers.pas 
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

37
unit Velthuis.Loggers;
38

39
interface
40

41
uses
42
  System.Classes;
43

44
type
45
  ILogger = interface
46
    ['{B6821CA6-64F8-48B0-89D0-A9A3E6304D82}']
47
    procedure Log(Msg: string); overload;
48
    procedure Log(Format: string; Args: array of const); overload;
49
  end;
50

51
  TLogger = class(TInterfacedObject, ILogger)
52
  private
53
    FStream: TStream;
54
    FWriter: TStreamWriter;
55
  public
56
    constructor Create(S: TStream); overload;
57
    constructor Create(LogFileName: string); overload;
58
    destructor Destroy; override;
59
    procedure Log(Msg: string); overload;
60
    procedure Log(Format: string; Args: array of const); overload;
61
  end;
62

63
var
64
  Logger: TLogger = nil;
65

66
implementation
67

68
uses
69
  System.SysUtils;
70

71
{ TLogger }
72

73
constructor TLogger.Create(S: TStream);
74
begin
75
  FStream := S;
76
  FWriter := TStreamWriter.Create(S);
77
end;
78

79
constructor TLogger.Create(LogFileName: string);
80
var
81
  F: TFileStream;
82
begin
83
  F := TFileStream.Create(LogFileName, fmCreate);
84
  Create(F);
85
end;
86

87
destructor TLogger.Destroy;
88
begin
89
  FWriter.Free;
90
  FStream.Free;
91
end;
92

93
procedure TLogger.Log(Msg: string);
94
begin
95
  FWriter.WriteLine(Msg);
96
end;
97

98
procedure TLogger.Log(Format: string; Args: array of const);
99
begin
100
  FWriter.WriteLine(System.SysUtils.Format(Format, Args));
101
end;
102

103
end.
104

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.