KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > internetcds > util > Logger


1 //
2
// Copyright 1998 CDS Networks, Inc., Medford Oregon
3
//
4
// All rights reserved.
5
//
6
// Redistribution and use in source and binary forms, with or without
7
// modification, are permitted provided that the following conditions are met:
8
// 1. Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// 2. Redistributions in binary form must reproduce the above copyright
11
// notice, this list of conditions and the following disclaimer in the
12
// documentation and/or other materials provided with the distribution.
13
// 3. All advertising materials mentioning features or use of this software
14
// must display the following acknowledgement:
15
// This product includes software developed by CDS Networks, Inc.
16
// 4. The name of CDS Networks, Inc. may not be used to endorse or promote
17
// products derived from this software without specific prior
18
// written permission.
19
//
20
// THIS SOFTWARE IS PROVIDED BY CDS NETWORKS, INC. ``AS IS'' AND
21
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
// ARE DISCLAIMED. IN NO EVENT SHALL CDS NETWORKS, INC. BE LIABLE
24
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
// SUCH DAMAGE.
31
//
32

33
34 package com.internetcds.util;
35
36 import java.io.*;
37
38
39 /**
40  * This class will log messages into a file.
41  *
42  * @version $Id: Logger.java,v 1.1 2006/06/23 10:39:30 sinisa Exp $
43  * @author Craig Spannring
44  */

45 public class Logger
46 {
47    public static final String JavaDoc cvsVersion = "$Id: Logger.java,v 1.1 2006/06/23 10:39:30 sinisa Exp $";
48
49    private static String JavaDoc filename = "log.out";
50    private static boolean active = false;
51    private static PrintStream out = null;
52
53    /**
54     * Initialize the logger facility.
55     * <p>
56     * If the log facility hasn't been initialized yet this routine will
57     * open the log file.
58     * <p>
59     * The routine must be called before any logging takes place.
60     * All of the functions in this class that log messages should
61     * call this routine. It doesn't hurt anything if this is called
62     * multiple times.
63     */

64    synchronized private static void init()
65       throws IOException
66    {
67       // check to see if the file is already open
68
if (out==null)
69       {
70          // open the log file
71
out = new PrintStream(new FileOutputStream(filename));
72       }
73    }
74
75    /**
76     * Turn the logging on or off.
77     * <p>
78     * The first time logging is turned on it will create the log file.
79     *
80     * @param value when value is true it will turn the logging on,
81     * if it is false it will turn the logging off.
82     */

83    synchronized public static void setActive(boolean value)
84       throws IOException
85    {
86       init();
87       active = value;
88    }
89
90    /**
91     * Is logging turned on?
92     */

93    public static boolean isActive()
94    {
95       return active;
96    }
97
98    /**
99     * set the name of the log file.
100     * <p>
101     * This method allows you to set the name of the log file.
102     * <B>Note-</B> Once the log file is open you can not change the
103     * name.
104     *
105     * @param value name of the log file.
106     */

107    public synchronized static void setFilename(String JavaDoc value)
108    {
109       filename = value;
110    }
111
112    /**
113     * return the name of the log file.
114     */

115    public static String JavaDoc getFilename()
116    {
117       return filename;
118    }
119
120    /**
121     * Print a string into the log file if and only if logging is active
122     */

123    synchronized public static void print(String JavaDoc msg)
124       throws IOException
125    {
126       if (active)
127       {
128          init();
129          out.print(msg);
130       }
131    }
132
133    /**
134     * Print a string into the log file if and only if logging is active
135     */

136    synchronized public static void println(String JavaDoc msg)
137       throws IOException
138    {
139       if (active)
140       {
141          init();
142          out.println(msg);
143       }
144    }
145 }
146
Popular Tags