Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Conditions Of Use
   *
   * This software was developed by employees of the National Institute of
   * Standards and Technology (NIST), an agency of the Federal Government.
   * Pursuant to title 15 Untied States Code Section 105, works of NIST
   * employees are not subject to copyright protection in the United States
   * and are considered to be in the public domain.  As a result, a formal
   * license is not needed to use the software.
  *
  * This software is provided by NIST as a service and is expressly
  * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
  * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
  * AND DATA ACCURACY.  NIST does not warrant or make any representations
  * regarding the use of the software or the results thereof, including but
  * not limited to the correctness, accuracy, reliability or usefulness of
  * the software.
  *
  * Permission to use this software is contingent upon your acceptance
  * of the terms of this agreement.
  *
  */
Product of NIST/ITL Advanced Networking Technologies Division (ANTD). * /
 
 
 package gov.nist.core;
 
 import java.io.*;
 
A wrapper around log4j that is used for logging debug and errors. You can replace this file if you want to change the way in which messages are logged.

Author(s):
M. Ranganathan
M.Andrews
Jeroen van Bemmel
Jean Deruelle
Version:
1.2
 
 
 public class LogWriter implements StackLogger {

    
The logger to which we will write our logging output.
 
     private Logger logger;

    
The stack name.
 
     private String stackName;

    
Name of the log file in which the trace is written out (default is /tmp/sipserverlog.txt)
 
     private String logFileName = null;

    
Flag to indicate that logging is enabled.
 
     private volatile boolean needsLogging = false;
 
     private int lineCount;

    
trace level
 
 
     protected int traceLevel = ;
 
     private String buildTimeStamp;
 
     private Properties configurationProperties;

    
log a stack trace. This helps to look at the stack frame.
 
     public void logStackTrace() {
         this.logStackTrace();
 
     }
 
     public void logStackTrace(int traceLevel) {
         if () {
             StringWriter sw = new StringWriter();
             PrintWriter pw = new PrintWriter(sw);
            StackTraceElement[] ste = new Exception().getStackTrace();
            // Skip the log writer frame and log all the other stack frames.
            for (int i = 1; i < ste.lengthi++) {
                String callFrame = "[" + ste[i].getFileName() + ":"
                        + ste[i].getLineNumber() + "]";
                pw.print(callFrame);
            }
            pw.close();
            String stackTrace = sw.getBuffer().toString();
            Level level = this.getLevel(traceLevel);
            Priority priority = this.getLogPriority();
            if ( level.isGreaterOrEqual(priority)) {
                .log(level,stackTrace);
            }
        }
    }

    
Get the line count in the log stream.

Returns:
    public int getLineCount() {
        return ;
    }

    
Get the logger.

Returns:
    public Logger getLogger() {
        return ;
    }


    
This method allows you to add an external appender. This is useful for the case when you want to log to a different log stream than a file.

Parameters:
appender
    public void addAppender(Appender appender) {
        this..addAppender(appender);
    }

    
Log an exception.

Parameters:
ex
    public void logException(Throwable ex) {
        if () {
            this.getLogger().error(ex.getMessage(), ex);
        }
    }




    
Counts the line number so that the debug log can be correlated to the message trace.

Parameters:
message -- message to count the lines for.
    private void countLines(String message) {
        char[] chars = message.toCharArray();
        for (int i = 0; i < chars.lengthi++) {
            if (chars[i] == '\n')
                ++;
        }
    }

    
Prepend the line and file where this message originated from

Parameters:
message
Returns:
re-written message.
    private String enhanceMessage(String message) {
        StackTraceElement[] stackTrace = new Exception().getStackTrace();
        StackTraceElement elem = stackTrace[3];
        String className = elem.getClassName();
        String methodName = elem.getMethodName();
        String fileName = elem.getFileName();
        int lineNumber = elem.getLineNumber();
        String newMessage = className + "." + methodName + "(" + fileName + ":"
                + lineNumber + ") [" + message + "]";
        return newMessage;
    }

    
Log a message into the log file.

Parameters:
message message to log into the log file.
    public void logDebug(String message) {
        if () {
            String newMessage = this.enhanceMessage(message);
            if ( this. == 0) {
                getLogger().debug("BUILD TIMESTAMP = " + this.);
                getLogger().debug("Config Propeties = " + this.);
            }
            countLines(newMessage);
            getLogger().debug(newMessage);
        }
    }
    
    
Log a message into the log file.

Parameters:
message message to log into the log file.
    public void logTrace(String message) {
        if () {
            String newMessage = this.enhanceMessage(message);
            if ( this. == 0) {
                getLogger().debug("BUILD TIMESTAMP = " + this.);
                getLogger().debug("Config Propeties = " + this.);
            }
            countLines(newMessage);
            getLogger().trace(newMessage);
        }
    }

    
Set the trace level for the stack.
    private void setTraceLevel(int level) {
         = level;
    }

    
Get the trace level for the stack.
    public int getTraceLevel() {
        return ;
    }

    
Log an error message.

Parameters:
message -- error message to log.
    public void logFatalError(String message) {
        Logger logger = this.getLogger();
        String newMsg = this.enhanceMessage(message);
        countLines(newMsg);
        logger.fatal(newMsg);
    }

    
Log an error message.

Parameters:
message -- error message to log.
    public void logError(String message) {
        Logger logger = this.getLogger();
        String newMsg = this.enhanceMessage(message);
        countLines(newMsg);
        logger.error(newMsg);
    }
    public LogWriter() {
    }
    
	public void setStackProperties(Properties configurationProperties) {
        this. = configurationProperties;
        String logLevel = configurationProperties
                .getProperty("gov.nist.javax.sip.TRACE_LEVEL");
        this. = configurationProperties
                .getProperty("gov.nist.javax.sip.DEBUG_LOG");
        this. = configurationProperties
                .getProperty("javax.sip.STACK_NAME");
        //check whether a Log4j logger name has been
        //specified. if not, use the stack name as the default
        //logger name.
        String category = configurationProperties
                                .getProperty("gov.nist.javax.sip.LOG4J_LOGGER_NAME"this.);
         = Logger.getLogger(category);
        if (logLevel != null) {
            if (logLevel.equals("LOG4J")) {
                . = false;
                
            }
            else {
                try {
                    int ll = 0;
                    if (logLevel.equals("TRACE")) {
                        ll = ;
                        . = true;
                        Debug.setStackLogger(this);
                    } else if (logLevel.equals("DEBUG")) {
                        ll = ;
                    } else if ( logLevel.equals("INFO")) {
                        ll = ;
                    } else if (logLevel.equals("ERROR")) {
                        ll = ;
                    } else if (logLevel.equals("NONE") || logLevel.equals("OFF")) {
                        ll = ;
                    } else {
                        ll = Integer.parseInt(logLevel);
                        if ( ll > 32 ) {
                            . = true;
                            Debug.setStackLogger(this);
                        }
                    }
                    this.setTraceLevel(ll);
                    this. = true;
                    if ( == ) {
                        .setLevel(.);
                    } else if ( == ) {
                        .setLevel(.);
                    } else if ( == ) {
                        .setLevel(.);
                    } else if ( == ) {
                        .setLevel(.);
                        this. = false;
                    }
                    /*
                     * If user specifies a logging file as part of the startup
                     * properties then we try to create the appender.
                     */
                    if (this. && this. != null) {
                        boolean overwrite = Boolean.valueOf(
                                configurationProperties.getProperty(
                                "gov.nist.javax.sip.DEBUG_LOG_OVERWRITE"));
                        FileAppender fa = null;
                        try {
                            fa = new FileAppender(new SimpleLayout(),
                                    this., !overwrite);
                        } catch (FileNotFoundException fnf) {
                            // Likely due to some directoy not existing. Create
                            // them
                            File logfile = new File(this.);
                            logfile.getParentFile().mkdirs();
                            logfile.delete();
                            try {
                                fa = new FileAppender(new SimpleLayout(),
                                        this.);
                            } catch (IOException ioe) {
                                ioe.printStackTrace(); // give up
                            }
                        } catch (IOException ex) {
                            ex.printStackTrace();
                        }
                        if (fa != null)
                            .addAppender(fa);
                    }
                } catch (NumberFormatException ex) {
                    ex.printStackTrace();
                    ..println("LogWriter: Bad integer " + logLevel);
                    ..println("logging dislabled ");
                     = false;
                }
            }
        } else {
            this. = false;
        }
        
    }

    

Returns:
flag to indicate if logging is enabled.
    public boolean isLoggingEnabled() {
        return this.;
    }

    
Return true/false if loging is enabled at a given level.

Parameters:
logLevel
    public boolean isLoggingEnabled(int logLevel) {
        return this. && logLevel <= ;
    }


    
Log an error message.

Parameters:
message
ex
    public void logError(String messageException ex) {
        Logger logger = this.getLogger();
        logger.error(messageex);
    }

    
Log a warning mesasge.

Parameters:
string
    public void logWarning(String string) {
        getLogger().warn(string);
    }

    
Log an info message.

Parameters:
string
    public void logInfo(String string) {
        getLogger().info(string);
    }

    
Disable logging altogether.
    public void disableLogging() {
        this. = false;
    }

    
Enable logging (globally).
    public void enableLogging() {
        this. = true;
    }
    public void setBuildTimeStamp(String buildTimeStamp) {
        this. = buildTimeStamp;
    }
    public Priority getLogPriority() {
         if ( this. ==  ) {
            return .;
        } else if ( this. ==  ) {
            return .;
        } else if ( this. == ) {
            return .;
        } else if ( this. == ) {
            return .;
        } else {
            return .;
        }
    }
    public Level getLevel(int traceLevel) {
        if ( traceLevel ==  ) {
           return .;
       } else if ( traceLevel ==  ) {
           return .;
       } else if ( traceLevel == ) {
           return .;
       } else if (traceLevel == ) {
           return .;
       } else {
           return .;
       }
   }
	public String getLoggerName() {
	    if ( this. != null ) {
	        return .getName();
	    } else {
	        return null;
	    }
	}
  
New to GrepCode? Check out our FAQ X