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.AppendFile;
14 
15 private import  tango.util.log.Log;
16 
17 private import  tango.io.device.File;
18 
19 private import  tango.io.stream.Buffered;
20 
21 private import  tango.io.model.IFile,
22                 tango.io.model.IConduit;
23 
24 /*******************************************************************************
25 
26         Append log messages to a file. This basic version has no rollover 
27         support, so it just keeps on adding to the file. There is also an
28         AppendFiles that may suit your needs.
29 
30 *******************************************************************************/
31 
32 class AppendFile : Filer
33 {
34         private Mask    mask_;
35 
36         /***********************************************************************
37                 
38                 Create a basic FileAppender to a file with the specified 
39                 path.
40 
41         ***********************************************************************/
42 
43         this (const(char[]) fp, Appender.Layout how = null)
44         {
45                 // Get a unique fingerprint for this instance
46                 mask_ = register (fp);
47         
48                 // make it shareable for read
49                 auto style = File.WriteAppending;
50                 style.share = File.Share.Read;
51                 configure (new File (fp, style));
52                 layout (how);
53         }
54 
55         /***********************************************************************
56                 
57                 Return the fingerprint for this class
58 
59         ***********************************************************************/
60 
61         @property override final const Mask mask ()
62         {
63                 return mask_;
64         }
65 
66         /***********************************************************************
67                 
68                 Return the name of this class
69 
70         ***********************************************************************/
71 
72         @property override final const const(char)[] name ()
73         {
74                 return this.classinfo.name;
75         }
76                 
77         /***********************************************************************
78                 
79                 Append an event to the output.
80                  
81         ***********************************************************************/
82 
83         override final void append (LogEvent event)
84         {
85                 synchronized(this)
86                 {
87                     layout.format (event, &buffer.write);
88                     buffer.append (FileConst.NewlineString)
89                           .flush();
90                 }
91         }
92 }
93 
94 
95 /*******************************************************************************
96 
97         Base class for file appenders
98 
99 *******************************************************************************/
100 
101 class Filer : Appender
102 {
103         package Bout            buffer;
104         private IConduit        conduit_;
105 
106         /***********************************************************************
107                 
108                 Return the conduit
109 
110         ***********************************************************************/
111 
112         @property final IConduit conduit ()
113         {
114                 return conduit_;
115         }
116 
117         /***********************************************************************
118                 
119                 Close the file associated with this Appender
120 
121         ***********************************************************************/
122 
123         override final void close ()
124         {
125                 synchronized(this)
126                 {
127                     if (conduit_)
128                        {
129                        conduit_.detach();
130                        conduit_ = null;
131                        }
132                 }
133         }
134 
135         /***********************************************************************
136                 
137                 Set the conduit
138 
139         ***********************************************************************/
140 
141         package final Bout configure (IConduit conduit)
142         {
143                 // create a new buffer upon this conduit
144                 conduit_ = conduit;
145                 return (buffer = new Bout(conduit));
146         }
147 }
148 
149