KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > log > TimestampFilter


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.log;
30
31 import com.caucho.server.util.CauchoSystem;
32 import com.caucho.util.Alarm;
33 import com.caucho.util.QDate;
34 import com.caucho.vfs.Path;
35 import com.caucho.vfs.StreamImpl;
36 import com.caucho.vfs.WriteStream;
37
38 import java.io.IOException JavaDoc;
39
40 /**
41  * Automatically-rotating streams. Normally, clients will call
42  * getStream instead of using the StreamImpl interface.
43  */

44 public class TimestampFilter extends StreamImpl {
45   private WriteStream _stream;
46   
47   private String JavaDoc _timestamp;
48
49   private QDate _calendar = new QDate(true);
50
51   private boolean _isLineBegin = true;
52
53   /**
54    * Create listener.
55    *
56    * @param path underlying log path
57    */

58   public TimestampFilter()
59   {
60   }
61
62   /**
63    * Create listener.
64    *
65    * @param path underlying log path
66    */

67   public TimestampFilter(WriteStream out, String JavaDoc timestamp)
68   {
69     _stream = out;
70     _timestamp = timestamp;
71   }
72
73   public void setTimestamp(String JavaDoc timestamp)
74   {
75     _timestamp = timestamp;
76   }
77
78   public void setStream(WriteStream stream)
79   {
80     _stream = stream;
81   }
82
83   public Path getPath()
84   {
85     if (_stream != null)
86       return _stream.getPath();
87     else
88       return super.getPath();
89   }
90
91   /**
92    * Returns true if the stream can write.
93    */

94   public boolean canWrite()
95   {
96     return _stream != null && _stream.canWrite();
97   }
98
99   /**
100    * Write data to the stream.
101    */

102   public void write(byte []buffer, int offset, int length, boolean isEnd)
103     throws IOException JavaDoc
104   {
105     if (_stream == null)
106       return;
107
108     if (_timestamp == null) {
109       _stream.write(buffer, offset, length);
110       return;
111     }
112     
113     long now;
114
115     if (CauchoSystem.isTesting())
116       now = Alarm.getCurrentTime();
117     else
118       now = System.currentTimeMillis();
119     
120     for (int i = 0; i < length; i++) {
121       if (_isLineBegin) {
122         _stream.print(_calendar.formatLocal(now, _timestamp));
123         _isLineBegin = false;
124       }
125
126       int ch = buffer[offset + i];
127       _stream.write(ch);
128       
129       if (ch == '\n' ||
130           ch == '\r' && i + 1 < length && buffer[offset + i + 1] != '\n')
131         _isLineBegin = true;
132     }
133   }
134
135   /**
136    * Flushes the data.
137    */

138   public void flush()
139     throws IOException JavaDoc
140   {
141     if (_stream != null)
142       _stream.flush();
143   }
144
145   /**
146    * Flushes the data.
147    */

148   public void close()
149     throws IOException JavaDoc
150   {
151     if (_stream != null)
152       _stream.close();
153   }
154 }
155
Popular Tags