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