View Javadoc
1   //******************************************************************************
2   //
3   // File:    PrintStreamLogger.java
4   // Package: edu.rit.util
5   // Unit:    Class edu.rit.util.PrintStreamLogger
6   //
7   // This Java source file is copyright (C) 2008 by Alan Kaminsky. All rights
8   // reserved. For further information, contact the author, Alan Kaminsky, at
9   // ark@cs.rit.edu.
10  //
11  // This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
12  // software; you can redistribute it and/or modify it under the terms of the GNU
13  // General Public License as published by the Free Software Foundation; either
14  // version 3 of the License, or (at your option) any later version.
15  //
16  // PJ is distributed in the hope that it will be useful, but WITHOUT ANY
17  // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18  // A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19  //
20  // Linking this library statically or dynamically with other modules is making a
21  // combined work based on this library. Thus, the terms and conditions of the GNU
22  // General Public License cover the whole combination.
23  //
24  // As a special exception, the copyright holders of this library give you
25  // permission to link this library with independent modules to produce an
26  // executable, regardless of the license terms of these independent modules, and
27  // to copy and distribute the resulting executable under terms of your choice,
28  // provided that you also meet, for each linked independent module, the terms
29  // and conditions of the license of that module. An independent module is a module
30  // which is not derived from or based on this library. If you modify this library,
31  // you may extend this exception to your version of the library, but you are not
32  // obligated to do so. If you do not wish to do so, delete this exception
33  // statement from your version.
34  //
35  // A copy of the GNU General Public License is provided in the file gpl.txt. You
36  // may also obtain a copy of the GNU General Public License on the World Wide
37  // Web at http://www.gnu.org/licenses/gpl.html.
38  //
39  //******************************************************************************
40  package edu.rit.util;
41  
42  import java.io.PrintStream;
43  import java.util.Date;
44  
45  /**
46   * Class PrintStreamLogger provides an object that logs messages to a print
47   * stream.
48   *
49   * @author Alan Kaminsky
50   * @version 16-Apr-2008
51   */
52  public class PrintStreamLogger
53          implements Logger {
54  
55  // Hidden operations.
56      private PrintStream out;
57  
58  // Exported constructors.
59      /**
60       * Construct a new print stream logger that logs to <code>System.err</code>.
61       */
62      public PrintStreamLogger() {
63          this.out = System.err;
64      }
65  
66      /**
67       * Construct a new print stream logger that logs to the given print stream.
68       *
69       * @param out Print stream.
70       * @exception NullPointerException (unchecked exception) Thrown if
71       * <code>out</code> is null.
72       */
73      public PrintStreamLogger(PrintStream out) {
74          if (out == null) {
75              throw new NullPointerException("PrintStreamLogger(): Print stream is null");
76          }
77          this.out = out;
78      }
79  
80  // Exported operations.
81      /**
82       * Log the given message.
83       *
84       * @param msg Message.
85       */
86      public void log(String msg) {
87          log(System.currentTimeMillis(), msg, null);
88      }
89  
90      /**
91       * {@inheritDoc}
92       *
93       * Log the given exception.
94       */
95      public void log(Throwable exc) {
96          log(System.currentTimeMillis(), null, exc);
97      }
98  
99      /**
100      * {@inheritDoc}
101      *
102      * Log the given message and exception.
103      */
104     public void log(String msg,
105             Throwable exc) {
106         log(System.currentTimeMillis(), msg, exc);
107     }
108 
109     /**
110      * Log the given date and message.
111      *
112      * @param date Date and time in milliseconds since midnight 01-Jan-1970 UTC.
113      * @param msg Message.
114      */
115     public void log(long date,
116             String msg) {
117         log(date, msg, null);
118     }
119 
120     /**
121      * {@inheritDoc}
122      *
123      * Log the given date and exception.
124      */
125     public void log(long date,
126             Throwable exc) {
127         log(date, null, exc);
128     }
129 
130     /**
131      * {@inheritDoc}
132      *
133      * Log the given date, message, and exception.
134      */
135     public void log(long date,
136             String msg,
137             Throwable exc) {
138         synchronized (out) {
139             out.print(new Date(date));
140             if (msg != null) {
141                 out.print(' ');
142                 out.print(msg);
143             }
144             out.println();
145             if (exc != null) {
146                 exc.printStackTrace(out);
147             }
148         }
149     }
150 
151 }