Source code: com/synchrona/util/Log.java
1 /*
2 **************************************************************************
3 ** $Header: /cvsroot/jred/jred/src/com/synchrona/util/Log.java,v 1.1.1.1 2000/07/05 04:41:53 mpatters Exp $
4 **
5 ** Copyright (C) 2000 Synchrona, Inc. All rights reserved.
6 **
7 ** This file is part of JRed, a 100% Java implementation of the IrDA
8 ** infrared communications protocols.
9 **
10 ** This file may be distributed under the terms of the Synchrona Public
11 ** License as defined by Synchrona, Inc. and appearing in the file
12 ** LICENSE included in the packaging of this file. The Synchrona Public
13 ** License is based on the Q Public License as defined by Troll Tech AS
14 ** of Norway; it differs only in its use of the courts of Florida, USA
15 ** rather than those of Oslo, Norway.
16 **************************************************************************
17 */
18 package com.synchrona.util;
19
20 import java.io.PrintWriter;
21 import java.text.SimpleDateFormat;
22 import java.util.Date;
23
24 /**
25 * Offers the usual logging capabilities. Can also cache a single instance
26 * so it can be shared within a JVM. This is useful when logging output
27 * should be directed to a single location.
28 */
29 public class Log {
30 public static final String DEFAULT_TIMESTAMP_FORMAT = "MM/dd/yy kk:mm:ss.SSS z";
31
32 /**
33 * Name of the property from whose value Log instances determine
34 * whether or not debug messages should be printed. Debug messages
35 * are disabled by default.
36 */
37 public static final String LOG_DEBUG_PROPERTY = "Log.debug";
38
39 /**
40 * Name of the property from whose value Log instances determine
41 * whether or not error messages should be printed. Error messages
42 * are enabled by default.
43 */
44 public static final String LOG_ERROR_PROPERTY = "Log.errors";
45
46 /**
47 * Name of the property from whose value Log instances determine
48 * whether or not warning messages should be printed. Warnings are
49 * enabled by default.
50 */
51 public static final String LOG_WARN_PROPERTY = "Log.warnings";
52
53 private boolean _debug = initState(LOG_DEBUG_PROPERTY, false);
54 private boolean _error = initState(LOG_ERROR_PROPERTY, true);
55 private boolean _warn = initState(LOG_WARN_PROPERTY, true);
56 private SimpleDateFormat _format = new SimpleDateFormat(DEFAULT_TIMESTAMP_FORMAT);
57 private PrintWriter _out = null;
58
59 /**
60 * Create a Log instance. All messages will be written to the
61 * given PrintWriter.
62 */
63 public Log(PrintWriter out) {
64 _out = out;
65 }
66
67 /**
68 * Print debugging messages, if debugging is enabled.
69 */
70 public final void debug(String strCategory, String strMessage) {
71 log(_debug, "debug", strCategory, strMessage);
72 }
73
74 public final void debugBytes(String strCategory, String strMessage, byte [] ayBytes, int nOffset, int nLength) {
75 logBytes(_debug, "debug", strCategory, strMessage, ayBytes, nOffset, nLength);
76 }
77
78 /**
79 * Returns true if debugging messages are enabled.
80 */
81 public boolean debugEnabled() {
82 return _debug;
83 }
84
85 /**
86 * Print error messages, if errors is enabled. Errors are enabled by default.
87 */
88 public final void error(String strCategory, String strMessage) {
89 log(_error, "error", strCategory, strMessage);
90 }
91
92 public final void errorBytes(String strCategory, String strMessage, byte [] ayBytes, int nOffset, int nLength) {
93 logBytes(_error, "error", strCategory, strMessage, ayBytes, nOffset, nLength);
94 }
95
96 public boolean errorsEnabled() {
97 return _error;
98 }
99
100 public final void warn(String strCategory, String strMessage) {
101 log(_warn, "warning", strCategory, strMessage);
102 }
103
104 public final void warnBytes(String strCategory, String strMessage, byte [] ayBytes, int nOffset, int nLength) {
105 logBytes(_warn, "warning", strCategory, strMessage, ayBytes, nOffset, nLength);
106 }
107
108 /**
109 * Returns true if warning messages are enabled.
110 */
111 public final boolean warningsEnabled() {
112 return _warn;
113 }
114
115 /**
116 *** Returns true if logging is enabled for this combination
117 *** of type and category.
118 ***
119 *** @param type Log type (debug, error, warn)
120 *** @param category User-defined logging category
121 **/
122 private final boolean isEnabled(String type, boolean typeDefault, String category) {
123 String property = "Log." + type + "." + category;
124 String value = System.getProperty(property);
125 Boolean enabled;
126
127 // If no override is defined for this property,
128 // use the default value for this type of logging
129 // message. This breaks if the default value changes
130 // during program execution...how to handle this?
131 if ( null == value ) {
132 enabled = new Boolean(typeDefault);
133 } else {
134 enabled = new Boolean(value);
135 }
136
137 return enabled.booleanValue();
138 }
139
140 /**
141 * Looks for a property that defines the initial state (enabled/
142 * disabled) of a log level. If the property is not defined,
143 * initState() returns the given default value.
144 */
145 private final boolean initState(String strLevel, boolean bDefault) {
146 String strState = System.getProperty(strLevel);
147 boolean bValue = false;
148 if ( null == strState ) {
149 bValue = bDefault;
150 } else {
151 bValue = Boolean.getBoolean(strLevel);
152 }
153 return bValue;
154 }
155
156 /**
157 * If bFlag is true, this method formats its arguments and
158 * prints them to the log stream. This method is used by
159 * <a href="#debug">debug()</a>, <a href="#error">error()</a>,
160 * and <a href="#warn">warn().</a>
161 */
162 private void log(boolean bFlag, String strType, String strCategory, String strMessage) {
163 if ( isEnabled(strType, bFlag, strCategory) ) {
164 StringBuffer buf = new StringBuffer(128);
165 buf.append(strType);
166 buf.append(" ");
167 buf.append(_format.format(new Date()));
168 buf.append(" [");
169 buf.append(strCategory);
170 buf.append("] ");
171 buf.append(strMessage);
172 _out.println(buf.toString());
173 }
174 }
175
176 /**
177 * If bFlag is true, this method formats its arguments and
178 * prints them to the log stream. This method is used by
179 * <a href="#debug">debug()</a>, <a href="#error">error()</a>,
180 * and <a href="#warn">warn().</a>
181 */
182 private void logBytes(boolean bFlag, String strType, String strCategory, String strMessage, byte [] ayBytes, int nOffset, int nLength) {
183 if ( isEnabled(strType, bFlag, strCategory) ) {
184 StringBuffer buf = new StringBuffer(128);
185 buf.append(strType);
186 buf.append(" ");
187 buf.append(_format.format(new Date()));
188 buf.append(" [");
189 buf.append(strCategory);
190 buf.append("] ");
191 buf.append(strMessage);
192 buf.append(" ");
193 buf.append(Utilities.bytesToString(ayBytes, nOffset, nLength));
194 _out.println(buf.toString());
195 }
196 }
197 }