LogWriter.java revision 600c7a4bbc7348167293eac928192e695b4ad5ba
1/*
2 * Conditions Of Use
3 *
4 * This software was developed by employees of the National Institute of
5 * Standards and Technology (NIST), an agency of the Federal Government.
6 * Pursuant to title 15 Untied States Code Section 105, works of NIST
7 * employees are not subject to copyright protection in the United States
8 * and are considered to be in the public domain.  As a result, a formal
9 * license is not needed to use the software.
10 *
11 * This software is provided by NIST as a service and is expressly
12 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
13 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
14 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
15 * AND DATA ACCURACY.  NIST does not warrant or make any representations
16 * regarding the use of the software or the results thereof, including but
17 * not limited to the correctness, accuracy, reliability or usefulness of
18 * the software.
19 *
20 * Permission to use this software is contingent upon your acceptance
21 * of the terms of this agreement.
22 *
23 */
24/***************************************************************************
25 * Product of NIST/ITL Advanced Networking Technologies Division (ANTD).    *
26 ***************************************************************************/
27package gov.nist.core;
28
29import java.io.*;
30import java.util.Properties;
31
32// BEGIN ANDROID-added
33// TODO: this class should be replaced by android logging mechanism.
34public class LogWriter implements StackLogger {
35    public void logStackTrace() { }
36    public void logStackTrace(int traceLevel) { }
37    public int getLineCount() { return 1; }
38    public void logException(Throwable ex) { }
39    public void logDebug(String message) { }
40    public void logTrace(String message) { }
41    public int getTraceLevel() { return 0; }
42    public void logFatalError(String message) { }
43    public void logError(String message) { }
44    public LogWriter() { }
45	public void setStackProperties(Properties configurationProperties) { }
46    public boolean isLoggingEnabled() { return false; }
47    public boolean isLoggingEnabled(int logLevel) { return false; }
48    public void logError(String message, Exception ex) { }
49    public void logWarning(String string) { }
50    public void logInfo(String string) { }
51    public void disableLogging() { }
52    public void enableLogging() { }
53    public void setBuildTimeStamp(String buildTimeStamp) { }
54	public String getLoggerName() { return "HELLO"; }
55}
56// END android-added
57
58// BEGIN android-deleted
59
60//import org.apache.log4j.Appender;
61//import org.apache.log4j.FileAppender;
62//import org.apache.log4j.Level;
63//import org.apache.log4j.Logger;
64//import org.apache.log4j.Priority;
65//import org.apache.log4j.SimpleLayout;
66//
67///**
68// * A wrapper around log4j that is used for logging debug and errors. You can
69// * replace this file if you want to change the way in which messages are logged.
70// *
71// * @version 1.2
72// *
73// * @author M. Ranganathan <br/>
74// * @author M.Andrews
75// * @author Jeroen van Bemmel
76// * @author Jean Deruelle
77// *
78// */
79//
80//public class LogWriter implements StackLogger {
81//
82//    /**
83//     * The logger to which we will write our logging output.
84//     */
85//    private Logger logger;
86//
87//    /**
88//     * The stack name.
89//     */
90//    private String stackName;
91//
92//    /**
93//     * Name of the log file in which the trace is written out (default is
94//     * /tmp/sipserverlog.txt)
95//     */
96//    private String logFileName = null;
97//
98//    /**
99//     * Flag to indicate that logging is enabled.
100//     */
101//    private volatile boolean needsLogging = false;
102//
103//    private int lineCount;
104//
105//    /**
106//     * trace level
107//     */
108//
109//    protected int traceLevel = TRACE_NONE;
110//
111//    private String buildTimeStamp;
112//
113//    private Properties configurationProperties;
114//
115//    /**
116//     * log a stack trace. This helps to look at the stack frame.
117//     */
118//    public void logStackTrace() {
119//        this.logStackTrace(TRACE_DEBUG);
120//
121//    }
122//
123//    public void logStackTrace(int traceLevel) {
124//        if (needsLogging) {
125//            StringWriter sw = new StringWriter();
126//            PrintWriter pw = new PrintWriter(sw);
127//            StackTraceElement[] ste = new Exception().getStackTrace();
128//            // Skip the log writer frame and log all the other stack frames.
129//            for (int i = 1; i < ste.length; i++) {
130//                String callFrame = "[" + ste[i].getFileName() + ":"
131//                        + ste[i].getLineNumber() + "]";
132//                pw.print(callFrame);
133//            }
134//            pw.close();
135//            String stackTrace = sw.getBuffer().toString();
136//            Level level = this.getLevel(traceLevel);
137//            Priority priority = this.getLogPriority();
138//            if ( level.isGreaterOrEqual(priority)) {
139//                logger.log(level,stackTrace);
140//            }
141//
142//        }
143//    }
144//
145//    /**
146//     * Get the line count in the log stream.
147//     *
148//     * @return
149//     */
150//    public int getLineCount() {
151//        return lineCount;
152//    }
153//
154//    /**
155//     * Get the logger.
156//     *
157//     * @return
158//     */
159//    public Logger getLogger() {
160//        return logger;
161//    }
162//
163//
164//    /**
165//     * This method allows you to add an external appender.
166//     * This is useful for the case when you want to log to
167//     * a different log stream than a file.
168//     *
169//     * @param appender
170//     */
171//    public void addAppender(Appender appender) {
172//
173//        this.logger.addAppender(appender);
174//
175//    }
176//
177//    /**
178//     * Log an exception.
179//     *
180//     * @param ex
181//     */
182//    public void logException(Throwable ex) {
183//
184//        if (needsLogging) {
185//
186//            this.getLogger().error(ex.getMessage(), ex);
187//        }
188//    }
189//
190//
191//
192//
193//    /**
194//     * Counts the line number so that the debug log can be correlated to the
195//     * message trace.
196//     *
197//     * @param message --
198//     *            message to count the lines for.
199//     */
200//    private void countLines(String message) {
201//        char[] chars = message.toCharArray();
202//        for (int i = 0; i < chars.length; i++) {
203//            if (chars[i] == '\n')
204//                lineCount++;
205//        }
206//
207//    }
208//
209//    /**
210//     * Prepend the line and file where this message originated from
211//     *
212//     * @param message
213//     * @return re-written message.
214//     */
215//    private String enhanceMessage(String message) {
216//
217//        StackTraceElement[] stackTrace = new Exception().getStackTrace();
218//        StackTraceElement elem = stackTrace[2];
219//        String className = elem.getClassName();
220//        String methodName = elem.getMethodName();
221//        String fileName = elem.getFileName();
222//        int lineNumber = elem.getLineNumber();
223//        String newMessage = className + "." + methodName + "(" + fileName + ":"
224//                + lineNumber + ") [" + message + "]";
225//        return newMessage;
226//
227//    }
228//
229//    /**
230//     * Log a message into the log file.
231//     *
232//     * @param message
233//     *            message to log into the log file.
234//     */
235//    public void logDebug(String message) {
236//        if (needsLogging) {
237//            String newMessage = this.enhanceMessage(message);
238//            if ( this.lineCount == 0) {
239//                getLogger().debug("BUILD TIMESTAMP = " + this.buildTimeStamp);
240//                getLogger().debug("Config Propeties = " + this.configurationProperties);
241//            }
242//            countLines(newMessage);
243//            getLogger().debug(newMessage);
244//        }
245//
246//    }
247//
248//    /**
249//     * Log a message into the log file.
250//     *
251//     * @param message
252//     *            message to log into the log file.
253//     */
254//    public void logTrace(String message) {
255//        if (needsLogging) {
256//            String newMessage = this.enhanceMessage(message);
257//            if ( this.lineCount == 0) {
258//                getLogger().debug("BUILD TIMESTAMP = " + this.buildTimeStamp);
259//                getLogger().debug("Config Propeties = " + this.configurationProperties);
260//            }
261//            countLines(newMessage);
262//            getLogger().trace(newMessage);
263//        }
264//
265//    }
266//
267//    /**
268//     * Set the trace level for the stack.
269//     */
270//    private void setTraceLevel(int level) {
271//        traceLevel = level;
272//    }
273//
274//    /**
275//     * Get the trace level for the stack.
276//     */
277//    public int getTraceLevel() {
278//        return traceLevel;
279//    }
280//
281//    /**
282//     * Log an error message.
283//     *
284//     * @param message --
285//     *            error message to log.
286//     */
287//    public void logFatalError(String message) {
288//        Logger logger = this.getLogger();
289//        String newMsg = this.enhanceMessage(message);
290//        countLines(newMsg);
291//        logger.fatal(newMsg);
292//
293//    }
294//
295//    /**
296//     * Log an error message.
297//     *
298//     * @param message --
299//     *            error message to log.
300//     *
301//     */
302//    public void logError(String message) {
303//        Logger logger = this.getLogger();
304//        String newMsg = this.enhanceMessage(message);
305//        countLines(newMsg);
306//        logger.error(newMsg);
307//
308//    }
309//
310//    public LogWriter() {
311//    }
312//
313//	public void setStackProperties(Properties configurationProperties) {
314//
315//        this.configurationProperties = configurationProperties;
316//
317//        String logLevel = configurationProperties
318//                .getProperty("gov.nist.javax.sip.TRACE_LEVEL");
319//
320//        this.logFileName = configurationProperties
321//                .getProperty("gov.nist.javax.sip.DEBUG_LOG");
322//
323//        this.stackName = configurationProperties
324//                .getProperty("javax.sip.STACK_NAME");
325//
326//        //check whether a Log4j logger name has been
327//        //specified. if not, use the stack name as the default
328//        //logger name.
329//        String category = configurationProperties
330//                                .getProperty("gov.nist.javax.sip.LOG4J_LOGGER_NAME", this.stackName);
331//
332//
333//        logger = Logger.getLogger(category);
334//        if (logLevel != null) {
335//            if (logLevel.equals("LOG4J")) {
336//                //if TRACE_LEVEL property is specified as
337//                //"LOG4J" then, set the traceLevel based on
338//                //the log4j effective log level.
339//                Level level = logger.getEffectiveLevel();
340//                this.needsLogging = true;
341//                if (level == Level.OFF)
342//                    this.needsLogging = false;
343//                this.traceLevel = TRACE_NONE;
344//                if (level.isGreaterOrEqual(Level.DEBUG)) {
345//                    this.traceLevel = TRACE_DEBUG;
346//                } else if (level.isGreaterOrEqual(Level.INFO)) {
347//                    this.traceLevel = TRACE_INFO;
348//                } else if (level.isGreaterOrEqual(Level.WARN)) {
349//                    this.traceLevel = TRACE_ERROR;
350//                }
351//            }
352//            else {
353//                try {
354//                    int ll = 0;
355//                    if (logLevel.equals("TRACE")) {
356//                        ll = TRACE_DEBUG;
357//                        Debug.debug = true;
358//                        Debug.setStackLogger(this);
359//                    } else if (logLevel.equals("DEBUG")) {
360//                        ll = TRACE_DEBUG;
361//                    } else if ( logLevel.equals("INFO")) {
362//                        ll = TRACE_INFO;
363//                    } else if (logLevel.equals("ERROR")) {
364//                        ll = TRACE_ERROR;
365//                    } else if (logLevel.equals("NONE") || logLevel.equals("OFF")) {
366//                        ll = TRACE_NONE;
367//                    } else {
368//                        ll = Integer.parseInt(logLevel);
369//                        if ( ll > 32 ) {
370//                            Debug.debug = true;
371//                            Debug.setStackLogger(this);
372//                        }
373//                    }
374//
375//                    this.setTraceLevel(ll);
376//                    this.needsLogging = true;
377//                    if (traceLevel == TRACE_DEBUG) {
378//                        logger.setLevel(Level.DEBUG);
379//                    } else if (traceLevel == TRACE_INFO) {
380//                        logger.setLevel(Level.INFO);
381//                    } else if (traceLevel == TRACE_ERROR) {
382//                        logger.setLevel(Level.ERROR);
383//                    } else if (traceLevel == TRACE_NONE) {
384//                        logger.setLevel(Level.OFF);
385//                        this.needsLogging = false;
386//                    }
387//
388//                    /*
389//                     * If user specifies a logging file as part of the startup
390//                     * properties then we try to create the appender.
391//                     */
392//                    if (this.needsLogging && this.logFileName != null) {
393//
394//                        boolean overwrite = Boolean.valueOf(
395//                                configurationProperties.getProperty(
396//                                "gov.nist.javax.sip.DEBUG_LOG_OVERWRITE"));
397//
398//                        FileAppender fa = null;
399//                        try {
400//                            fa = new FileAppender(new SimpleLayout(),
401//                                    this.logFileName, !overwrite);
402//                        } catch (FileNotFoundException fnf) {
403//
404//                            // Likely due to some directoy not existing. Create
405//                            // them
406//                            File logfile = new File(this.logFileName);
407//                            logfile.getParentFile().mkdirs();
408//                            logfile.delete();
409//
410//                            try {
411//                                fa = new FileAppender(new SimpleLayout(),
412//                                        this.logFileName);
413//                            } catch (IOException ioe) {
414//                                ioe.printStackTrace(); // give up
415//                            }
416//                        } catch (IOException ex) {
417//                            ex.printStackTrace();
418//                        }
419//
420//                        if (fa != null)
421//                            logger.addAppender(fa);
422//                    }
423//
424//                } catch (NumberFormatException ex) {
425//                    ex.printStackTrace();
426//                    System.err.println("LogWriter: Bad integer " + logLevel);
427//                    System.err.println("logging dislabled ");
428//                    needsLogging = false;
429//                }
430//            }
431//        } else {
432//            this.needsLogging = false;
433//
434//        }
435//
436//
437//    }
438//
439//    /**
440//     * @return flag to indicate if logging is enabled.
441//     */
442//    public boolean isLoggingEnabled() {
443//
444//        return this.needsLogging;
445//    }
446//
447//    /**
448//     * Return true/false if loging is enabled at a given level.
449//     *
450//     * @param logLevel
451//     */
452//    public boolean isLoggingEnabled(int logLevel) {
453//        return this.needsLogging && logLevel <= traceLevel;
454//    }
455//
456//
457//    /**
458//     * Log an error message.
459//     *
460//     * @param message
461//     * @param ex
462//     */
463//    public void logError(String message, Exception ex) {
464//        Logger logger = this.getLogger();
465//        logger.error(message, ex);
466//
467//    }
468//
469//    /**
470//     * Log a warning mesasge.
471//     *
472//     * @param string
473//     */
474//    public void logWarning(String string) {
475//        getLogger().warn(string);
476//
477//    }
478//
479//    /**
480//     * Log an info message.
481//     *
482//     * @param string
483//     */
484//    public void logInfo(String string) {
485//        getLogger().info(string);
486//    }
487//
488//    /**
489//     * Disable logging altogether.
490//     *
491//     */
492//    public void disableLogging() {
493//        this.needsLogging = false;
494//    }
495//
496//    /**
497//     * Enable logging (globally).
498//     */
499//    public void enableLogging() {
500//        this.needsLogging = true;
501//
502//    }
503//
504//    public void setBuildTimeStamp(String buildTimeStamp) {
505//        this.buildTimeStamp = buildTimeStamp;
506//
507//    }
508//
509//    public Priority getLogPriority() {
510//         if ( this.traceLevel == TRACE_INFO ) {
511//            return Priority.INFO;
512//        } else if ( this.traceLevel == TRACE_ERROR ) {
513//            return Priority.ERROR;
514//        } else if ( this.traceLevel == TRACE_DEBUG) {
515//            return Priority.DEBUG;
516//        } else if ( this.traceLevel == TRACE_TRACE) {
517//            return Priority.DEBUG;
518//        } else {
519//            return Priority.FATAL;
520//        }
521//    }
522//
523//    public Level getLevel(int traceLevel) {
524//        if ( traceLevel == TRACE_INFO ) {
525//           return Level.INFO;
526//       } else if ( traceLevel == TRACE_ERROR ) {
527//           return Level.ERROR;
528//       } else if ( traceLevel == TRACE_DEBUG) {
529//           return Level.DEBUG;
530//       } else if (traceLevel == TRACE_TRACE) {
531//           return Level.ALL;
532//       } else {
533//           return Level.OFF;
534//       }
535//   }
536//
537//	public String getLoggerName() {
538//	    if ( this.logger != null ) {
539//	        return logger.getName();
540//	    } else {
541//	        return null;
542//	    }
543//	}
544//
545//
546//}
547
548// END android-deleted
549
550