001: /*
002: * Log.java
003: *
004: *
005: * Copyright (c) 2003 Rimfaxe ApS (www.rimfaxe.com).
006: * All rights reserved.
007: *
008: * This package is written by Lars Andersen <lars@rimfaxe.com>
009: * and licensed by Rimfaxe ApS.
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions
013: * are met:
014: *
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * 2. Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in
020: * the documentation and/or other materials provided with the
021: * distribution.
022: *
023: * 3. The end-user documentation included with the redistribution, if
024: * any, must include the following acknowlegement:
025: * "This product includes software developed by Rimfaxe ApS
026: (www.rimfaxe.com)"
027: * Alternately, this acknowlegement may appear in the software itself,
028: * if and wherever such third-party acknowlegements normally appear.
029: *
030: * 4. The names "Rimfaxe", "Rimfaxe Software", "Lars Andersen" and
031: * "Rimfaxe WebServer" must not be used to endorse or promote products
032: * derived from this software without prior written permission. For written
033: * permission, please contact info@rimfaxe.com
034: *
035: * 5. Products derived from this software may not be called "Rimfaxe"
036: * nor may "Rimfaxe" appear in their names without prior written
037: * permission of the Rimfaxe ApS.
038: *
039: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
040: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
041: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
042: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
043: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
044: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
045: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
046: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
047: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
048: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049: * SUCH DAMAGE.
050: *
051: */
052:
053: package com.rimfaxe.util;
054:
055: import java.lang.*;
056: import java.util.*;
057: import java.text.*;
058: import java.io.*;
059:
060: /**
061: *
062: * @author Lars Andersen
063: * @version 1.1
064: */
065: public class Log {
066: private static BufferedWriter bw = null;
067: private static FileWriter fw;
068: private static String logFileName = "log/rlb.log";
069: private static long logFileSize = 10 * 1024 * 1024;
070:
071: private final static int numberOfEntries = 100;
072: private static Integer level = new Integer(0);
073:
074: public final static long DEBUG = 0;
075: public final static long ERROR = 1;
076: public final static long FATAL = 2;
077: public final static long STARTUP = 3;
078:
079: private static String[] lastEntries = new String[numberOfEntries];
080: private static int index = 0;
081: private static boolean firstTime = true;
082: private static int firstEntry = 0;
083:
084: public static void setLogFile(String name) {
085: logFileName = name;
086: }
087:
088: private static void newLogFile() {
089: try {
090: fw = new FileWriter(logFileName, true); // append to old file
091: bw = new BufferedWriter(fw);
092: } catch (Exception e) {
093: String str = new String(DateFormat.getDateTimeInstance()
094: .format(new Date())
095: + "Exception occured in Log.write: " + e);
096: try {
097: bw.write(str, 0, str.length());
098: bw.newLine();
099: } catch (Exception ex) {
100: //ex.printStackTrace();
101: }
102: }
103: }
104:
105: private static void checkFile() {
106: // Check if the stream is open, if not create a new stream.
107: if (bw == null)
108: newLogFile();
109:
110: java.io.File logFile = new java.io.File(logFileName);
111:
112: // If the log file is greater than logFileSize, rename it to "logfile.old"
113: // and create a new log file.
114: try {
115: if (logFile.length() > logFileSize) {
116: java.io.File oldLogFile = new java.io.File(logFileName
117: + ".old");
118:
119: if (oldLogFile.exists())
120: oldLogFile.delete();
121: bw.close(); // closes the current stream
122: bw = null;
123: if (logFile.renameTo(oldLogFile) != true) {
124: String str = new String(
125: DateFormat.getDateTimeInstance().format(
126: new Date())
127: + "Error can't rename logfile to logfile.old");
128: System.err.println(str);
129: }
130: newLogFile();
131: }
132: } catch (Exception e) {
133: //e.printStackTrace();
134: }
135: }
136:
137: private static synchronized void write(String logString) {
138: try {
139: checkFile();
140: bw.write(logString, 0, logString.length());
141: bw.newLine();
142: bw.flush();
143: lastEntries[index] = logString;
144: if (!firstTime) {
145: if (index == numberOfEntries - 1)
146: firstEntry = 0;
147: else
148: firstEntry = index + 1;
149: }
150: index++;
151: if (index >= numberOfEntries) {
152: firstTime = false;
153: index = 0;
154: }
155:
156: } catch (IOException e) {
157: try {
158: String str = new String(DateFormat
159: .getDateTimeInstance().format(new Date())
160: + "IOException occured in Log.write: " + e);
161: bw.write(str, 0, str.length());
162: bw.newLine();
163: } catch (Exception ex) {
164: //ex.printStackTrace();
165: }
166: } catch (Exception e) {
167: try {
168: String str = new String(DateFormat
169: .getDateTimeInstance().format(new Date())
170: + "Some other Exception occured in Log.write: "
171: + e);
172: bw.write(str, 0, str.length());
173: bw.newLine();
174: } catch (Exception ex) {
175: //ex.printStackTrace();
176: }
177: }
178: }
179:
180: private static String getMiliSec() {
181: String t = Long.toString(new Date().getTime());
182: return ("." + t.substring((t.length() - 3), t.length()));
183: }
184:
185: /**
186: *Exampel: 11-02-99 22:11:48.123 -> Write test string
187: */
188: public static void log(String txt) {
189: log(txt, DEBUG);
190: }
191:
192: public static void log(String txt, long logLevel) {
193: if (logLevel >= level.intValue())
194: write(""
195: + DateFormat.getDateTimeInstance().format(
196: new Date()) + " -> " + txt);
197: }
198:
199: /**
200: *Exampel: 11-02-99 22:11:48.123 -> 12 Write test string
201: */
202: public static void log(int no, String txt) {
203: log(no, txt, DEBUG);
204: }
205:
206: public static void log(int no, String txt, long logLevel) {
207: if (logLevel >= level.intValue())
208: write(""
209: + DateFormat.getDateTimeInstance().format(
210: new Date()) + " -> " + no + " " + txt);
211: }
212:
213: /**
214: *Exampel: 11-02-99 22:11:48.234 -> myClass Write test string
215: */
216: public static void log(String str, String txt) {
217: log(str, txt, DEBUG);
218: }
219:
220: public static void log(String str, String txt, long logLevel) {
221: if (logLevel >= level.intValue()) {
222: if (str.length() == 0)
223: write(""
224: + DateFormat.getDateTimeInstance().format(
225: new Date()) + " -> " + txt);
226: else
227: write(""
228: + DateFormat.getDateTimeInstance().format(
229: new Date()) + " -> " + str + " " + txt);
230: }
231: }
232:
233: public static synchronized String[] getLastEntries() {
234: String[] t = lastEntries;
235: int j = firstEntry;
236: String[] temp = new String[numberOfEntries];
237: try {
238: for (int i = 0; i < numberOfEntries; i++) {
239: temp[i] = t[j];
240: j++;
241: if (j >= numberOfEntries)
242: j = 0;
243: }
244: } catch (Exception e) {
245: } finally {
246: return temp;
247: }
248: }
249:
250: public static synchronized com.rimfaxe.xml.datamodel.Container getData() {
251: com.rimfaxe.xml.datamodel.Container co = new com.rimfaxe.xml.datamodel.Container();
252: com.rimfaxe.xml.datamodel.Iterator iter = new com.rimfaxe.xml.datamodel.Iterator();
253:
254: String[] t = getLastEntries();
255:
256: try {
257: for (int i = 0; i < t.length; i++) {
258: com.rimfaxe.xml.datamodel.Container item = new com.rimfaxe.xml.datamodel.Container();
259: if (t[i] != null) {
260: item.addLiteral("logitem", t[i]);
261: iter.addContainer(item);
262:
263: }
264: }
265: } catch (Exception e) {
266: }
267:
268: co.addIterator("loglist", iter);
269: return co;
270:
271: }
272:
273: }
|