KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > base > ErrorLogger


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

22 package org.aspectj.debugger.base;
23
24 import java.io.*;
25 import java.util.*;
26 import java.text.*;
27
28 public class ErrorLogger extends AbstractErrorLogger {
29
30     public ErrorLogger(Debugger debugger) {
31         super(debugger);
32     }
33
34     public ErrorLogger(Adapter adapter) {
35         super(adapter);
36     }
37
38     public ErrorLogger() {
39         super(new Adapter());
40     }
41
42     public static class Adapter implements ErrorLoggable {
43         public Options getOptions() {
44             return new Options();
45         }
46         public void fatal(String JavaDoc str) {
47             System.err.println(str);
48             System.exit(-1);
49         }
50     }
51
52     public static void main(String JavaDoc[] args) {
53         AbstractErrorLogger logger = new AbstractErrorLogger(
54             new ErrorLoggable() {
55                 public Options getOptions() {
56                     return new Options();
57                 }
58                 public void fatal(String JavaDoc str) {
59                     System.err.println(str);
60                 }
61             }
62         );
63         try {
64             String JavaDoc s = null; s.toString();
65         } catch (Exception JavaDoc e) {
66             logger.log(e);
67         }
68     }
69
70     public static void error(Object JavaDoc o) {
71         AbstractErrorLogger logger = new AbstractErrorLogger(
72             new ErrorLoggable() {
73                 public Options getOptions() {
74                     return new Options();
75                 }
76                 public void fatal(String JavaDoc str) {
77                     System.err.println(str);
78                 }
79             }
80         );
81         if (o instanceof String JavaDoc) {
82             logger.log((String JavaDoc) o);
83         } else if (o instanceof Throwable JavaDoc) {
84             logger.log((Throwable JavaDoc) o);
85         } else {
86             logger.log(o + "");
87         }
88     }
89 }
90
91 interface ErrorLoggable {
92     public Options getOptions();
93     public void fatal(String JavaDoc str);
94 }
95
96 class AbstractErrorLogger {
97
98     private ErrorLoggable loggable;
99     private String JavaDoc errorFileName = ".ajdbErrors";
100
101     public AbstractErrorLogger(ErrorLoggable loggable) {
102         this.loggable = loggable;
103         Options options = loggable.getOptions();
104         Object JavaDoc userErrorFileName = options.get("errorfile");
105         if (userErrorFileName != null) {
106             errorFileName = userErrorFileName + "";
107         }
108     }
109
110     public void log(Throwable JavaDoc t) {
111         log("No message", t);
112     }
113
114     public void log(String JavaDoc msg) {
115         log(msg, new Throwable JavaDoc(msg));
116     }
117
118     public void log(String JavaDoc msg, Throwable JavaDoc t) {
119         try {
120             PrintWriter printWriter = new PrintWriter(getOutputStream());
121             printWriter.println(heading());
122             printWriter.println(msg);
123             t.printStackTrace(printWriter);
124             printWriter.close();
125         } catch (IOException ioe) {
126             loggable.fatal("Could not open error file for writing.");
127         }
128     }
129
130
131     private String JavaDoc heading() {
132         String JavaDoc date = DateFormat.getDateTimeInstance(DateFormat.FULL,
133                                                      DateFormat.FULL).format(new Date());
134         String JavaDoc username = System.getProperty("user.name");
135         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
136         buf.append("# -----------------------------------------------------------\n");
137         buf.append("# Date: " + date + "\n");
138         buf.append("# User: " + username + "\n");
139         buf.append("# -----------------------------------------------------------\n");
140         return buf.toString();
141     }
142
143     public OutputStream getOutputStream() throws IOException {
144         File file = new File(getErrorFileName());
145         boolean append = false;
146         if (file != null && file.exists()) {
147             append = true;
148         }
149         FileOutputStream out = new FileOutputStream(getErrorFileName(), append);
150         return out;
151     }
152
153     public String JavaDoc getErrorFileName() {
154         return errorFileName;
155     }
156
157     public void setErrorFile(String JavaDoc errorFileName) {
158         this.errorFileName = errorFileName;
159     }
160 }
Popular Tags