001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: /*
026: */
027:
028: package gov.nist.siplite.stack;
029:
030: import gov.nist.siplite.message.*;
031: import gov.nist.siplite.header.*;
032: import gov.nist.core.*;
033: import java.io.PrintStream;
034: import java.util.Enumeration;
035:
036: import com.sun.midp.log.Logging;
037: import com.sun.midp.log.LogChannels;
038:
039: /**
040: * Log file wrapper class.
041: * Log messages into the message trace file and also write the log into the
042: * debug file if needed. This class keeps an XML formatted trace around for
043: * later access via RMI. The trace can be viewed with a trace viewer (see
044: * tools.traceviewerapp).
045: *
046: * @version JAIN-SIP-1.1
047: *
048: *
049: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
050: *
051: *
052: */
053: public class ServerLog {
054: /** Dont trace */
055: public static int TRACE_NONE = 0;
056: /** Trace messages. */
057: public static int TRACE_MESSAGES = 16;
058: /** Trace exception processing. */
059: public static int TRACE_EXCEPTION = 17;
060: /** Debug trace level (all tracing enabled). */
061: public static int TRACE_DEBUG = 32;
062: /** Print stream for writing out debug messages. */
063: protected static PrintStream printWriter = null;
064: /** Print stream for writing out tracing messages. */
065: protected static PrintStream traceWriter = null;
066: /** Auxililary information to log with this trace. */
067: protected static String auxInfo;
068: /** Desription for mesasge. */
069: protected static String description;
070: /** Stack pointer for mesasge. */
071: protected static String stackIpAddress;
072: /** Default trace level. */
073: protected static int traceLevel = TRACE_MESSAGES;
074:
075: /**
076: * Checks for valid logging output destination.
077: */
078: public static void checkLogFile() {
079: // Append buffer to the end of the file.
080: if (printWriter == null) {
081: printWriter = traceWriter;
082: if (printWriter == null)
083: printWriter = System.out;
084: /*
085: if (auxInfo != null)
086: printWriter.println
087: (" < description\n logDescription = \""+description+
088: "\"\n name = \"" + stackIpAddress +
089: "\"\n auxInfo = \"" + auxInfo +
090: "\"/ > \n ");
091: else
092: printWriter.println(" < description\n logDescription = \""
093: + description
094: + "\"\n name = \""
095: + stackIpAddress
096: + "\" / > \n");
097: */
098: }
099: }
100:
101: /**
102: * Gets the status header from the requested message.
103: * @param message the message being processed
104: * @return the extracted status line
105: */
106: private static String getStatusHeader(Message message) {
107: // If this message has a "NISTStatus" extension then we extract
108: // it for logging.
109: Enumeration statusHeaders = message.getHeaders("NISTExtension");
110: String status = null;
111: if (statusHeaders.hasMoreElements()) {
112: Header statusHdr = (Header) statusHeaders.nextElement();
113: status = statusHdr.getHeaderValue();
114: }
115: return status;
116: }
117:
118: /**
119: * Checks to see if logging is enabled at a level (avoids
120: * unecessary message formatting.
121: * @param logLevel level at which to check.
122: * @return true if tracing the requested logging level
123: */
124: public static boolean needsLogging(int logLevel) {
125: return traceLevel >= logLevel;
126: }
127:
128: /**
129: * Global check for whether to log or not. ToHeader minimize the time
130: * return false here.
131: * @return true -- if logging is globally enabled and false otherwise.
132: */
133:
134: public static boolean needsLogging() {
135: return traceLevel >= 16;
136: }
137:
138: /**
139: * Sets the log file name. (need revisit)
140: * @param loggerURL is the name of the log file to set.
141: */
142: public static void setLogFileName(String loggerURL) {
143:
144: }
145:
146: /**
147: * Logs a message into the log file.
148: * @param message message to log into the log file.
149: */
150: public static void logMessage(String message) {
151: // String tname = Thread.currentThread().getName();
152: checkLogFile();
153: String logInfo = message;
154: // printWriter.println(logInfo);
155:
156: if (Logging.REPORT_LEVEL <= Logging.INFORMATION) {
157: Logging.report(Logging.INFORMATION, LogChannels.LC_JSR180,
158: logInfo);
159: }
160: }
161:
162: /**
163: * Logs a message into the log directory.
164: * @param message a Message to log
165: * @param from from header of the message to log into the log directory
166: * @param to to header of the message to log into the log directory
167: * @param sender is the server the sender (true if I am the sender).
168: * @param callId CallId of the message to log into the log directory.
169: * @param firstLine First line of the message to display
170: * @param status Status information (generated while processing message).
171: * @param tid is the transaction id for the message.
172: * @param time the reception time (or date).
173: */
174: public synchronized static void logMessage(String message,
175: String from, String to, boolean sender, String callId,
176: String firstLine, String status, String tid, String time) {
177:
178: MessageLog log = new MessageLog(message, from, to, time,
179: sender, firstLine, status, tid, callId);
180: logMessage(log.flush());
181: }
182:
183: /**
184: * Logs a message into the log directory.
185: * @param message a Message to log
186: * @param from from header of the message to log into the log directory
187: * @param to to header of the message to log into the log directory
188: * @param sender is the server the sender (true if I am the sender).
189: * @param callId CallId of the message to log into the log directory.
190: * @param firstLine First line of the message to display
191: * @param status Status information (generated while processing message).
192: * @param tid is the transaction id for the message.
193: * @param time the reception time (or date).
194: */
195: public synchronized static void logMessage(String message,
196: String from, String to, boolean sender, String callId,
197: String firstLine, String status, String tid, long time) {
198:
199: MessageLog log = new MessageLog(message, from, to, time,
200: sender, firstLine, status, tid, callId);
201: logMessage(log.flush());
202: }
203:
204: /**
205: * Logs a message into the log directory.
206: * @param message a Message to log
207: * @param from from header of the message to log into the log directory
208: * @param to to header of the message to log into the log directory
209: * @param sender is the server the sender
210: * @param callId CallId of the message to log into the log directory.
211: * @param firstLine First line of the message to display
212: * @param status Status information (generated while processing message).
213: * @param tid is the transaction id for the message.
214: */
215: public static void logMessage(String message, String from,
216: String to, boolean sender, String callId, String firstLine,
217: String status, String tid) {
218: String time = new Long(System.currentTimeMillis()).toString();
219: logMessage(message, from, to, sender, callId, firstLine,
220: status, tid, time);
221: }
222:
223: /**
224: * Logs a message into the log directory. Status information is extracted
225: * from the NISTExtension Header.
226: * @param message a Message to log
227: * @param from from header of the message to log into the log directory
228: * @param to to header of the message to log into the log directory
229: * @param sender is the server the sender
230: * @param time is the time to associate with the message.
231: */
232: public static void logMessage(Message message, String from,
233: String to, boolean sender, String time) {
234: checkLogFile();
235: CallIdHeader cid = (CallIdHeader) message.getCallId();
236: String callId = null;
237: if (cid != null)
238: callId = ((CallIdHeader) message.getCallId()).getCallId();
239: String firstLine = message.getFirstLine();
240: String inputText = message.encode();
241: String status = getStatusHeader(message);
242: String tid = message.getTransactionId();
243: logMessage(inputText, from, to, sender, callId, firstLine,
244: status, tid, time);
245: }
246:
247: /**
248: * Logs a message into the log directory.
249: * Status information is extracted from the NISTExtension Header.
250: * @param message a Message to log
251: * @param from from header of the message to log into the log directory
252: * @param to to header of the message to log into the log directory
253: * @param sender is the server the sender
254: * @param time is the time to associate with the message.
255: */
256: public static void logMessage(Message message, String from,
257: String to, boolean sender, long time) {
258: checkLogFile();
259: CallIdHeader cid = (CallIdHeader) message.getCallId();
260: String callId = null;
261: if (cid != null)
262: callId = cid.getCallId();
263: String firstLine = message.getFirstLine().trim();
264: String inputText = message.encode();
265: String status = getStatusHeader(message);
266: String tid = message.getTransactionId();
267: logMessage(inputText, from, to, sender, callId, firstLine,
268: status, tid, time);
269: }
270:
271: /**
272: * Logs a message into the log directory. Status information is extracted
273: * from SIPExtension header. The time associated with the message is the
274: * current time.
275: * @param message a Message to log
276: * @param from from header of the message to log into the log directory
277: * @param to to header of the message to log into the log directory
278: * @param sender is the server the sender
279: */
280: public static void logMessage(Message message, String from,
281: String to, boolean sender) {
282: logMessage(message, from, to, sender, new Long(System
283: .currentTimeMillis()).toString());
284: }
285:
286: /**
287: * Logs a message into the log directory.
288: * @param message a Message to log
289: * @param from from header of the message to log into the log directory
290: * @param to to header of the message to log into the log directory
291: * @param status the status to log. This is appended to any NISTExtension
292: * header present in the message.
293: * @param sender is the server the sender or receiver (true if sender).
294: * @param time is the reception time.
295: */
296: public static void logMessage(Message message, String from,
297: String to, String status, boolean sender, String time) {
298: checkLogFile();
299: CallIdHeader cid = (CallIdHeader) message.getCallId();
300: String callId = null;
301: if (cid != null)
302: callId = cid.getCallId();
303: String firstLine = message.getFirstLine().trim();
304: String encoded = message.encode();
305: String tid = message.getTransactionId();
306: String shdr = getStatusHeader(message);
307: if (shdr != null) {
308: status = shdr + "/" + status;
309: }
310: logMessage(encoded, from, to, sender, callId, firstLine,
311: status, tid, time);
312: }
313:
314: /**
315: * Logs a message into the log directory.
316: * @param message a Message to log
317: * @param from from header of the message to log into the log directory
318: * @param to to header of the message to log into the log directory
319: * @param status the status to log. This is appended to any NISTExtension
320: * header present in the message.
321: * @param sender is the server the sender or receiver (true if sender).
322: * @param time is the reception time.
323: */
324: public static void logMessage(Message message, String from,
325: String to, String status, boolean sender, long time) {
326: checkLogFile();
327: CallIdHeader cid = (CallIdHeader) message.getCallId();
328: String callId = null;
329: if (cid != null)
330: callId = cid.getCallId();
331: String firstLine = message.getFirstLine().trim();
332: String encoded = message.encode();
333: String tid = message.getTransactionId();
334: String shdr = getStatusHeader(message);
335: if (shdr != null) {
336: status = shdr + "/" + status;
337: }
338: logMessage(encoded, from, to, sender, callId, firstLine,
339: status, tid, time);
340: }
341:
342: /**
343: * Logs a message into the log directory. Time stamp associated with the
344: * message is the current time.
345: * @param message a Message to log
346: * @param from from header of the message to log into the log directory
347: * @param to to header of the message to log into the log directory
348: * @param status the status to log.
349: * @param sender is the server the sender or receiver (true if sender).
350: */
351: public static void logMessage(Message message, String from,
352: String to, String status, boolean sender) {
353: logMessage(message, from, to, status, sender, System
354: .currentTimeMillis());
355: }
356:
357: /**
358: * Logs an exception stack trace.
359: * @param ex Exception to log into the log file
360: */
361:
362: public static void logException(Exception ex) {
363: if (traceLevel >= TRACE_EXCEPTION) {
364: checkLogFile();
365: if (printWriter != null)
366: ex.printStackTrace();
367: }
368: }
369:
370: /**
371: * Sets the trace level for the stack.
372: *
373: * @param level -- the trace level to set. The following trace levels are
374: * supported:
375: * <ul>
376: * <li>
377: * 0 -- no tracing
378: * </li>
379: *
380: * <li>
381: * 16 -- trace messages only
382: * </li>
383: *
384: * <li>
385: * 32 Full tracing including debug messages.
386: * </li>
387: *
388: * </ul>
389: */
390: public static void setTraceLevel(int level) {
391: traceLevel = level;
392: }
393:
394: /**
395: * Gets the trace level for the stack.
396: *
397: * @return the trace level
398: */
399: public static int getTraceLevel() {
400: return traceLevel;
401: }
402:
403: /**
404: * Sets aux information. Auxiliary information may be associated
405: * with the log file. This is useful for remote logs.
406: * @param auxInfo -- auxiliary information.
407: */
408: public static void setAuxInfo(String auxInfo) {
409: ServerLog.auxInfo = auxInfo;
410: }
411:
412: /**
413: * Sets the descriptive String for the log.
414: * @param desc is the descriptive string.
415: */
416: public static void setDescription(String desc) {
417: description = desc;
418: }
419: }
|