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