132
|
1 /*******************************************************************************
|
|
2
|
|
3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved
|
|
4
|
|
5 license: BSD style: $(LICENSE)
|
|
6
|
|
7 version: Initial release: May 2004
|
|
8
|
|
9 author: Kris
|
|
10
|
|
11 *******************************************************************************/
|
|
12
|
|
13 module tango.util.log.FileAppender;
|
|
14
|
|
15 private import tango.util.log.Appender;
|
|
16
|
|
17 private import tango.io.Buffer,
|
|
18 tango.io.FileConst,
|
|
19 tango.io.FileConduit;
|
|
20
|
|
21 private import tango.io.model.IConduit;
|
|
22
|
|
23 /*******************************************************************************
|
|
24
|
|
25 Append log messages to a file. This basic version has no rollover
|
|
26 support, so it just keeps on adding to the file. There's also a
|
|
27 RollingFileAppender that may suit your needs.
|
|
28
|
|
29 *******************************************************************************/
|
|
30
|
|
31 public class FileAppender : Appender
|
|
32 {
|
|
33 private Mask mask;
|
|
34 private Buffer buffer;
|
|
35 private IConduit conduit_;
|
|
36
|
|
37 /***********************************************************************
|
|
38
|
|
39 ***********************************************************************/
|
|
40
|
|
41 protected this ()
|
|
42 {
|
|
43 }
|
|
44
|
|
45 /***********************************************************************
|
|
46
|
|
47 Create a basic FileAppender to a file with the specified
|
|
48 path.
|
|
49
|
|
50 ***********************************************************************/
|
|
51
|
|
52 this (FilePath fp, EventLayout layout = null)
|
|
53 {
|
|
54 // Get a unique fingerprint for this instance
|
|
55 mask = register (fp.toString);
|
|
56
|
|
57 // make it shareable for read
|
|
58 auto style = FileConduit.WriteAppending;
|
|
59 style.share = FileConduit.Share.Read;
|
|
60 setConduit (new FileConduit (fp, style));
|
|
61
|
|
62 setLayout (layout);
|
|
63 }
|
|
64
|
|
65 /***********************************************************************
|
|
66
|
|
67 Create a basic FileAppender to a file with the specified
|
|
68 path, and with the given EventLayout
|
|
69
|
|
70 ***********************************************************************/
|
|
71
|
|
72 this (char[] fp, EventLayout layout = null)
|
|
73 {
|
|
74 this (new FilePath (fp), layout);
|
|
75 }
|
|
76
|
|
77 /***********************************************************************
|
|
78
|
|
79 Return the conduit
|
|
80
|
|
81 ***********************************************************************/
|
|
82
|
|
83 IConduit conduit ()
|
|
84 {
|
|
85 return conduit_;
|
|
86 }
|
|
87
|
|
88 /***********************************************************************
|
|
89
|
|
90 Set the conduit
|
|
91
|
|
92 ***********************************************************************/
|
|
93
|
|
94 protected Buffer setConduit (IConduit conduit)
|
|
95 {
|
|
96 // create a new buffer upon this conduit
|
|
97 conduit_ = conduit;
|
|
98 return (buffer = new Buffer(conduit));
|
|
99 }
|
|
100
|
|
101 /***********************************************************************
|
|
102
|
|
103 Return the fingerprint for this class
|
|
104
|
|
105 ***********************************************************************/
|
|
106
|
|
107 Mask getMask ()
|
|
108 {
|
|
109 return mask;
|
|
110 }
|
|
111
|
|
112 /***********************************************************************
|
|
113
|
|
114 Return the name of this class
|
|
115
|
|
116 ***********************************************************************/
|
|
117
|
|
118 char[] getName ()
|
|
119 {
|
|
120 return this.classinfo.name;
|
|
121 }
|
|
122
|
|
123 /***********************************************************************
|
|
124
|
|
125 Append an event to the output.
|
|
126
|
|
127 ***********************************************************************/
|
|
128
|
|
129 synchronized void append (Event event)
|
|
130 {
|
|
131 auto layout = getLayout;
|
|
132 buffer.append (layout.header (event));
|
|
133 buffer.append (layout.content (event));
|
|
134 buffer.append (layout.footer (event))
|
|
135 .append (FileConst.NewlineString)
|
|
136 .flush ();
|
|
137 }
|
|
138
|
|
139 /***********************************************************************
|
|
140
|
|
141 Close the file associated with this Appender
|
|
142
|
|
143 ***********************************************************************/
|
|
144
|
|
145 synchronized void close ()
|
|
146 {
|
|
147 if (conduit_)
|
|
148 {
|
|
149 conduit_.detach;
|
|
150 conduit_ = null;
|
|
151 }
|
|
152 }
|
|
153 }
|
|
154
|
|
155
|