source: tags/MetisMQIDemo/src/main/java/weka/core/logging/FileLogger.java

Last change on this file was 29, checked in by gnappo, 15 years ago

Taggata versione per la demo e aggiunto branch.

File size: 3.6 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 *    GNU General Public License for more details.
11 *
12 *    You should have received a copy of the GNU General Public License
13 *    along with this program; if not, write to the Free Software
14 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15 */
16
17/*
18 * FileLogger.java
19 * Copyright (C) 2008 University of Waikato, Hamilton, New Zealand
20 */
21
22package weka.core.logging;
23
24import weka.core.RevisionUtils;
25
26import java.io.BufferedWriter;
27import java.io.File;
28import java.io.FileWriter;
29import java.util.Date;
30
31/**
32 * A simple file logger, that just logs to a single file. Deletes the file
33 * when an object gets instantiated.
34 *
35 * @author  fracpete (fracpete at waikato dot ac dot nz)
36 * @version $Revision: 5219 $
37 */
38public class FileLogger
39  extends ConsoleLogger {
40
41  /** the log file. */
42  protected File m_LogFile;
43 
44  /** the line feed. */
45  protected String m_LineFeed;
46 
47  /**
48   * Initializes the logger.
49   */
50  protected void initialize() {
51    super.initialize();
52
53    // log file
54    m_LogFile = getLogFile();
55    // try to remove file
56    try {
57      if ((m_LogFile != null) && m_LogFile.exists())
58        m_LogFile.delete();
59    }
60    catch (Exception e) {
61      e.printStackTrace();
62    }
63   
64    // the line feed
65    m_LineFeed = System.getProperty("line.separator");
66  }
67 
68  /**
69   * Returns the log file to use.
70   *
71   * @return            the log file
72   */
73  protected File getLogFile() {
74    String      filename;
75    File        result;
76   
77    filename = m_Properties.getProperty("LogFile", "%h/weka.log");
78    filename = filename.replaceAll("%t", System.getProperty("java.io.tmpdir"));
79    filename = filename.replaceAll("%h", System.getProperty("user.home"));
80    filename = filename.replaceAll("%c", System.getProperty("user.dir"));
81    filename = filename.replaceAll("%%", System.getProperty("%"));
82   
83    result = new File(filename);
84   
85    return result;
86  }
87 
88  /**
89   * Appends the given string to the log file (without new line!).
90   *
91   * @param s           the string to append
92   */
93  protected void append(String s) {
94    BufferedWriter      writer;
95   
96    if (m_LogFile == null)
97      return;
98   
99    // append output to file
100    try {
101      writer = new BufferedWriter(new FileWriter(m_LogFile, true));
102      writer.write(s);
103      writer.flush();
104      writer.close();
105    }
106    catch (Exception e) {
107      // ignored
108    }
109  }
110
111  /**
112   * Performs the actual logging.
113   *
114   * @param level       the level of the message
115   * @param msg         the message to log
116   * @param cls         the classname originating the log event
117   * @param method      the method originating the log event
118   * @param lineno      the line number originating the log event
119   */
120  protected void doLog(Level level, String msg, String cls, String method, int lineno) {
121    // output to console
122    super.doLog(level, msg, cls, method, lineno);
123   
124    // append output to file
125    append(
126        m_DateFormat.format(new Date()) + " " + cls + " " + method + m_LineFeed
127        + level + ": " + msg + m_LineFeed);
128  }
129 
130  /**
131   * Returns the revision string.
132   *
133   * @return            the revision
134   */
135  public String getRevision() {
136    return RevisionUtils.extract("$Revision: 5219 $");
137  }
138}
Note: See TracBrowser for help on using the repository browser.