bug fixes
[IRC.git] / Robust / src / Interface / LogFile.java
1 package Interface;
2 //****************************************************************************
3 // Programmer: Duane M. Gran, ragnar@cs.bsu.edu
4 // Program:    JhttpServer
5 // Date:       April 24, 1998
6 //****************************************************************************
7
8
9
10 import java.io.*;
11 import java.util.*;
12 import java.text.SimpleDateFormat;
13 import java.net.*;
14
15 //****************************************************************************
16 // Class:   logFile
17 // Purpose: Handle the behavior for logging connections.  The methods simply
18 //          add to the private data fields.  Has implementation for standard
19 //          output, as well as output to file.
20 //
21 //          An example log entry looks like:
22 //
23 // 1.2.3.4 - - [29/JAN/1998:21:40:30 -06] "GET /file.html HTTP/1.0" 200 472
24 //
25 //****************************************************************************
26
27 public class LogFile
28 {
29     static public final String log_file_name = "server.log";
30
31     static public String write_log(Socket s, String Method, String URI,
32                                    String Protocol, 
33                                    int ReturnCode, long BytesSent){
34
35         // Socket.toString() calls (indirectly) some Hashtable.get
36         // method  - I tool care of it!
37
38         /*
39         String addr = s.toString();     
40         String Address = addr.substring(addr.indexOf('/') + 1,
41                                         addr.indexOf(','));
42         */
43
44         //      SimpleDateFormat sdf =
45         //          new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz");  // RFC 1123
46         //      sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
47         //      String Date = sdf.format(new Date());
48
49         String Entry = 
50             /* Address + */ " - - [" +                      // IP address
51             "Date" + "] \"" +                         // date
52             Method + " " +                            // get, post, head
53             URI + " " +                               // filename
54             Protocol + "\" " +                        // http/1.?
55             ReturnCode + " " +      // 200-500
56             BytesSent + "\n";       // bytes sent
57
58         try{
59             BufferedWriter out = new BufferedWriter(
60                                  new OutputStreamWriter(
61                                  new FileOutputStream(log_file_name, true)));
62             
63             out.write(Entry,0,Entry.length());
64             out.flush();
65             out.close();
66         }
67         catch (IOException e){
68             System.err.println("Gicu " + e);
69         }
70
71         return Entry;
72     }
73 }