KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ixenon > free > util > LogFile


1 /* $Id$
2  *
3  * Copyright (c) 1999 Xenonsoft Limited. All Rights Reserved.
4  *
5  * This software is protected by copyright. You are hereby notified from
6  * now by reading this message. This software is also the confidential
7  * and proprietary information of Xenonsoft Limited. ("Confidential
8  * Information").
9  *
10  * This software is distributed under the Xenonsoft Public end user
11  * License ("XPeL"), where the machine-readable source code is provided
12  * under the "Open Source" model.
13  * For more information, please read the file "LICENSE-XPL.txt"
14  */

15
16 // File: `LogFile.java'
17

18 //
19
// Description:
20
// A log file object class
21
//
22
// Author:
23
// Peter Pilgrim Mon Jan 25 22:12:12 GMT 1999
24
//
25
// RCS HEADER ``LogFile.java''
26
//
27
// $Author$
28
// $Date$
29
// $Source$
30
// $Revision$ $State$ $Locker$
31
//
32
// --------------------------------------------------------------------------------
33
// $History$
34
//
35

36 package ixenon.free.util;
37
38 import java.io.File JavaDoc;
39 import java.io.IOException JavaDoc;
40 import java.io.OutputStream JavaDoc;
41 import java.io.FileOutputStream JavaDoc;
42 import java.io.PrintWriter JavaDoc;
43 import java.util.Date JavaDoc;
44 import java.text.*; // for `SimpleDataFormat' class
45

46 /**
47  * A object class which implements date/timestamped log file line.
48  */

49 public class LogFile {
50
51     protected File JavaDoc logfile = null;
52     protected PrintWriter JavaDoc pwriter = null;
53     
54     //
55
// Create the date formatter for date/time stamping the log
56
//
57
protected SimpleDateFormat stamper = new SimpleDateFormat( "ddMMMyyyy--HH:mm:ss -- " );
58     protected String JavaDoc newline = System.getProperty( "line.separator" );
59
60     /** Constructs a default log file object from the standard output
61      * stream <B>System.out</B>
62      * @param logile the log file
63      */

64     public LogFile()
65     throws IOException JavaDoc
66     {
67     this.logfile = new File JavaDoc("<STDOUT>");
68     pwriter = new PrintWriter JavaDoc( System.out );
69     }
70
71     /** Constructs a log file object from a supplied file
72      * If the log file already exists then it will be appended.
73      * @param logile the log filename
74      */

75     public LogFile( String JavaDoc logfile ) throws IOException JavaDoc
76     {
77     this( new File JavaDoc(logfile));
78     }
79
80     /** Constructs a log file object from a supplied file
81      * If the log file already exists then it will be appended.
82      * @param logile the log file
83      */

84     public LogFile( File JavaDoc logfile )
85     throws IOException JavaDoc
86     {
87     this.logfile = logfile;
88     pwriter = new PrintWriter JavaDoc(
89         new FileOutputStream JavaDoc( logfile.getAbsolutePath(), true ));
90     }
91
92     /** Returns the logfile handle object */
93     public File JavaDoc getFile()
94     {
95     return logfile;
96     }
97     
98     /** Returns the log filename */
99     public String JavaDoc getFilename()
100     {
101     return logfile.getAbsolutePath();
102     }
103     
104     /**
105      * Writes as string message to the log output stream with date/time stamp.
106      * No new line separator is appended.
107      */

108     public synchronized void dtprint( String JavaDoc message )
109     {
110     Date JavaDoc now = new Date JavaDoc();
111     String JavaDoc dtstr = stamper.format( now );
112     pwriter.print( dtstr + message );
113     pwriter.flush();
114     }
115
116     /**
117      * Writes as string message to the log output stream with date/time stamp.
118      * A new line separator is appended.
119      */

120     public void dtprintln( String JavaDoc message )
121     {
122     dtprint( message + newline );
123     }
124     
125     /**
126      * Writes as new line separator to the log output stream with date/time stamp.
127      */

128     public void dtprintln()
129     {
130     dtprint( newline );
131     }
132
133     /**
134      * Write as string message to the output stream <EM>without</EM>
135      * a date/time stamp and do <EM>not</EM> append newline separator.
136      */

137     public synchronized void print( String JavaDoc message )
138     {
139     pwriter.print( message );
140     pwriter.flush();
141     }
142     
143     /**
144      * Write as string message to the output stream <EM>without</EM>
145      * a date/time stamp and append with a newline separator.
146      */

147     public synchronized void println( String JavaDoc message )
148     {
149     pwriter.print( message + newline );
150     pwriter.flush();
151     }
152     
153     /**
154      * Write a new line separator to the output stream <EM>without</EM>
155      * a date/time stamp.
156      */

157     public synchronized void println()
158     {
159     pwriter.println();
160     pwriter.flush();
161     }
162 }
163
164 // fini
165
Popular Tags