1   package util;
2   
3   import java.io.*;
4   import java.util.*;
5   
6   /**
7   * Creates a log file and provides methods to add messages to it 
8   *
9   * @author Graham Ritchie
10  */
11  public class IntellegoLog
12  {
13      private File log;       // the log file
14      private FileWriter fw;  // the filewriter
15      
16      /**
17      * Creates the log file and writes a header message
18      */
19      public IntellegoLog()
20      {
21          // create the log file
22          log=new File("logs/Intellego.log");
23          
24          // get the current time
25          Date date=new Date();
26          String temp=date.toString();
27          String time=temp.substring(0,19);
28          
29          // try to write a header to the file
30          try
31          {
32              log.createNewFile();
33              fw=new FileWriter(log);
34              fw.write("========================\n  Intellego Log File\n========================\n\n");
35              fw.write("System started on: "+time+"\n\nMessages:\n\n");
36              fw.flush();
37          }
38          catch (Exception e)
39          {
40              System.out.println("IntellegoLog.init(): Failed to create log file: "+e);
41          }
42      }
43      
44      /**
45      * Adds a message string to the log file
46      *
47      * @param message the message to be added
48      */
49      public synchronized void addMessage(String message)
50      {
51          // try to write the message to the file
52          try
53          {
54              fw.write(message+"\n");
55              fw.flush();
56          }
57          catch (Exception e)
58          {
59              System.out.println("IntellegoLog.addMessage(): Failed to add log message: "+e);
60          }
61      }
62  }
63