001: package com.jat.core.log;
002:
003: import java.io.File;
004: import java.io.FileOutputStream;
005: import java.io.IOException;
006: import java.io.RandomAccessFile;
007: import java.text.SimpleDateFormat;
008: import java.util.Calendar;
009: import java.util.Date;
010: import java.util.GregorianCalendar;
011: import java.util.TimeZone;
012: import java.util.Vector;
013:
014: import com.jat.core.config.Config;
015:
016: /**
017: * <p>Title: JAT</p>
018: * <p>Description: </p>
019: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
020: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p> * @author stf
021: * @version 1.1
022: */
023:
024: public class Log {
025: /**
026: * @param timeZone
027: * Init the timezone used in FILE_DATE_FORMAT_
028: */
029: public static void init(TimeZone timeZone) throws Exception {
030: FILE_DATE_FORMAT_.setTimeZone(timeZone);
031: }
032:
033: public synchronized String[] getLast(int _numRow) throws Exception {
034: RandomAccessFile file = new RandomAccessFile(filename_, "r");
035: Vector vect = new Vector();
036: file.seek(0);
037: try {
038: String line = file.readLine();
039: int numLine = 0;
040: while (line != null) {
041: numLine++;
042: line = file.readLine();
043: }
044: file.seek(0);
045: int start = numLine > _numRow ? numLine - _numRow : 0;
046: line = file.readLine();
047: numLine = 0;
048: while (line != null) {
049: numLine++;
050: if (numLine > start)
051: vect.addElement(line);
052: line = file.readLine();
053: }
054: } finally {
055: file.close();
056: }
057: String[] ret = new String[vect.size()];
058: for (int i = 0; i < vect.size(); i++) {
059: ret[i] = (String) vect.elementAt(i);
060: }
061: return ret;
062: }
063:
064: /**
065: * @return returns true output on file or on HTML
066: */
067: public boolean isActivated() {
068: return active_;
069: }
070:
071: public void setActive(boolean valore) {
072: this .active_ = valore;
073: }
074:
075: protected Log(String _path, String _name) throws Exception {
076: path_ = _path;
077: name_ = _name;
078: setFile();
079:
080: GregorianCalendar calendar = new GregorianCalendar();
081: dayOfYear_ = calendar.get(Calendar.DAY_OF_YEAR);
082: String act = Config.getCurrent().getValue("log", _name);
083: if (act != null)
084: active_ = act.equalsIgnoreCase("active");
085: //if (Project.DEBUG) System.out.println("log file '"+_name+"' inited: "+(active_?"active":"not active"));
086: }
087:
088: /**
089: * @param _messageString formated text of the message
090: */
091: protected synchronized void send(StringBuffer _messageString)
092: throws IOException {
093: /*
094: * send
095: */
096: updateFile();
097: _messageString.append("\n");
098: file_.write(_messageString.toString().getBytes());
099: }
100:
101: /**
102: * @exception IOException
103: * Update the file : close and reopen if
104: * the current day is different from dayOfYear_
105: */
106: protected void updateFile() throws java.io.IOException {
107: GregorianCalendar calendar = new GregorianCalendar();
108: int dayOfYear = calendar.get(Calendar.DAY_OF_YEAR);
109: if (dayOfYear != dayOfYear_) {
110: try {
111: file_.close();
112: } catch (java.io.IOException ignored) {
113: }
114: setFile();
115: dayOfYear_ = dayOfYear;
116: }
117: }
118:
119: private void setFile() throws java.io.IOException {
120: try {
121: File dir = new File(path_);
122: if (!dir.exists())
123: dir.mkdirs();
124: filename_ = path_ + FILE_DATE_FORMAT_.format(new Date())
125: + "_" + name_ + ".log";
126: file_ = new FileOutputStream(filename_, true);
127: } catch (java.io.IOException ex) {
128: throw new java.io.IOException(
129: "Log::setFile:: exception for log '" + this .name_
130: + "': " + ex);
131: }
132: }
133:
134: /**
135: * Name of message file format
136: */
137: private static final SimpleDateFormat FILE_DATE_FORMAT_ = new SimpleDateFormat(
138: "yyyy_MM_dd");
139:
140: /**
141: * output directory
142: */
143: private String path_;
144:
145: /**
146: * log name
147: */
148: private String name_;
149:
150: /**
151: * output file
152: */
153: private FileOutputStream file_;
154:
155: /**
156: * log file name
157: */
158: private String filename_;
159:
160: /**
161: * current day of CGMessageOutput object
162: */
163: private int dayOfYear_;
164:
165: private boolean active_ = false;
166:
167: }
|