KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > util > Logger


1 /*****************************************************************************
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is the CVS Client Library.
16  * The Initial Developer of the Original Software is Robert Greig.
17  * Portions created by Robert Greig are Copyright (C) 2000.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Robert Greig.
21  *****************************************************************************/

22 package org.netbeans.lib.cvsclient.util;
23
24 import java.io.*;
25
26 /**
27  * Handles the logging of communication to and from the server
28  *
29  * @author Robert Greig
30  * @author Petr Kuzel rewriten to streams
31  */

32 public final class Logger {
33     /**
34      * The output stream to use to write communication sent to the server
35      */

36     private static OutputStream outLogStream;
37
38     /**
39      * The output stream to use to write communication received from the server
40      */

41     private static OutputStream inLogStream;
42
43     /**
44      * The log files path. If the property is set to the constant "system"
45      * then it uses System.err, otherwise it tries to create a file at the
46      * specified path
47      */

48     private static final String JavaDoc LOG_PROPERTY = "cvsClientLog"; // NOI18N
49

50     /**
51      * Whether we are logging or not
52      */

53     private static boolean logging;
54
55     static {
56         setLogging(System.getProperty(LOG_PROPERTY));
57     }
58
59     public static void setLogging(String JavaDoc logPath) {
60         logging = (logPath != null);
61
62         try {
63             if (logging) {
64                 if (logPath.equals("system")) { // NOI18N
65
outLogStream = System.err;
66                     inLogStream = System.err;
67                 }
68                 else {
69                     outLogStream = new BufferedOutputStream(new FileOutputStream(logPath + ".out")); // NOI18N
70
inLogStream = new BufferedOutputStream(new FileOutputStream(logPath + ".in")); // NOI18N
71
}
72             }
73         }
74         catch (IOException e) {
75             System.err.println("Unable to create log files: " + e); // NOI18N
76
System.err.println("Logging DISABLED"); // NOI18N
77
logging = false;
78             try {
79                 if (outLogStream != null) {
80                     outLogStream.close();
81                 }
82             }
83             catch (IOException ex2) {
84                 // ignore, if we get one here we really are screwed
85
}
86
87             try {
88                 if (inLogStream != null) {
89                     inLogStream.close();
90                 }
91             }
92             catch (IOException ex2) {
93                 // ignore, if we get one here we really are screwed
94
}
95         }
96     }
97
98
99     /**
100      * Log a message received from the server. The message is logged if
101      * logging is enabled
102      * @param received the data received from the server
103      */

104     public static void logInput(byte[] received) {
105         logInput(received, 0 , received.length);
106     }
107
108     /**
109      * Log a message received from the server. The message is logged if
110      * logging is enabled
111      * @param received the data received from the server
112      */

113     public static void logInput(byte[] received, int offset, int len) {
114         if (!logging) {
115             return;
116         }
117
118         try {
119             inLogStream.write(received, offset, len);
120             inLogStream.flush();
121         }
122         catch (IOException ex) {
123             System.err.println("Could not write to log file: " + ex); // NOI18N
124
System.err.println("Logging DISABLED."); // NOI18N
125
logging = false;
126         }
127     }
128
129     /**
130      * Log a character received from the server. The message is logged if
131      * logging is enabled
132      * @param received the data received from the server
133      */

134     public static void logInput(char received) {
135         if (!logging) {
136             return;
137         }
138
139         try {
140             inLogStream.write(received);
141             inLogStream.flush();
142         }
143         catch (IOException ex) {
144             System.err.println("Could not write to log file: " + ex); // NOI18N
145
System.err.println("Logging DISABLED."); // NOI18N
146
logging = false;
147         }
148     }
149
150     /**
151      * Log a message sent to the server. The message is logged if
152      * logging is enabled
153      * @param sent the data sent to the server
154      */

155     public static void logOutput(byte[] sent) {
156         if (!logging) {
157             return;
158         }
159
160         try {
161             outLogStream.write(sent);
162             outLogStream.flush();
163         }
164         catch (IOException ex) {
165             System.err.println("Could not write to log file: " + ex); // NOI18N
166
System.err.println("Logging DISABLED."); // NOI18N
167
logging = false;
168         }
169     }
170 }
171
172
Popular Tags