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.javax.sip.stack;
 
 
 import java.io.File;
 
 
Log file wrapper class. Log messages into the message trace file and also write the log into the debug file if needed. This class keeps an XML formatted trace around for later access via RMI. The trace can be viewed with a trace viewer (see tools.traceviewerapp).

Author(s):
M. Ranganathan
Version:
1.2 $Revision: 1.39.2.1 $ $Date: 2010-11-23 19:23:12 $
 
 public class ServerLog implements ServerLogger {
 
     private boolean logContent;
 
     protected StackLogger stackLogger;

    
Name of the log file in which the trace is written out (default is null)
 
     private String logFileName;

    
Print writer that is used to write out the log file.
 
     private PrintWriter printWriter;

    
Set auxililary information to log with this trace.
 
     private String auxInfo;
 
     private String description;
 
     private String stackIpAddress;
 
     private SIPTransactionStack sipStack;
 
 
     public ServerLog() {
         // Debug log file. Whatever gets logged by us also makes its way into debug log.
     }
 
     private void setProperties(Properties configurationProperties) {
         this. = configurationProperties;
         // Set a descriptive name for the message trace logger.
         this. = configurationProperties.getProperty("javax.sip.STACK_NAME");
         this. = configurationProperties.getProperty("javax.sip.IP_ADDRESS");
        this. = configurationProperties.getProperty("gov.nist.javax.sip.SERVER_LOG");
        String logLevel = configurationProperties.getProperty("gov.nist.javax.sip.TRACE_LEVEL");
        String logContent = configurationProperties
                .getProperty("gov.nist.javax.sip.LOG_MESSAGE_CONTENT");
        this. = (logContent != null && logContent.equals("true"));
        if (logLevel != null) {
            if (logLevel.equals("LOG4J")) {
                // if TRACE_LEVEL property is specified as
                // "LOG4J" then, set the traceLevel based on
                // the log4j effective log level.
                // check whether a Log4j logger name has been
                // specified. if not, use the stack name as the default
                // logger name.
                Logger logger = Logger.getLogger(configurationProperties.getProperty(
                        "gov.nist.javax.sip.LOG4J_LOGGER_NAME"this.));
                Level level = logger.getEffectiveLevel();
                if (level == .) {
                    this.setTraceLevel(0);
                } else if (level.isGreaterOrEqual(.)) {
                    this.setTraceLevel();
                } else if (level.isGreaterOrEqual(.)) {
                    this.setTraceLevel();
                } else if (level.isGreaterOrEqual(.)) {
                    this.setTraceLevel();
                }
            } else {
                try {
                    int ll;
                    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);
                    }
                    this.setTraceLevel(ll);
                } catch (NumberFormatException ex) {
                    ..println("ServerLog: WARNING Bad integer " + logLevel);
                    ..println("logging dislabled ");
                    this.setTraceLevel(0);
                }
            }
        }
        checkLogFile();
    }
    public void setStackIpAddress(String ipAddress) {
        this. = ipAddress;
    }
    // public static boolean isWebTesterCatchException=false;
    // public static String webTesterLogFile=null;

    
default trace level
    protected int traceLevel = ;
    public synchronized void closeLogFile() {
        if ( != null) {
            .close();
             = null;
        }
    }
    public void checkLogFile() {
        if ( == null ||  < ) {
            // Dont create a log file if tracing is
            // disabled.
            return;
        }
        try {
            File logFile = new File();
            if (!logFile.exists()) {
                logFile.createNewFile();
                 = null;
            }
            // Append buffer to the end of the file unless otherwise specified
            // by the user.
            if ( == null) {
                boolean overwrite = Boolean.valueOf(
                    .getProperty(
                        "gov.nist.javax.sip.SERVER_LOG_OVERWRITE"));
                FileWriter fw = new FileWriter(, !overwrite);
                 = new PrintWriter(fwtrue);
                .println("<!-- "
                        + "Use the  Trace Viewer in src/tools/tracesviewer to"
                        + " view this  trace  \n"
                        + "Here are the stack configuration properties \n"
                        + "javax.sip.IP_ADDRESS= "
                        + .getProperty("javax.sip.IP_ADDRESS") + "\n"
                        + "javax.sip.STACK_NAME= "
                        + .getProperty("javax.sip.STACK_NAME") + "\n"
                        + "javax.sip.ROUTER_PATH= "
                        + .getProperty("javax.sip.ROUTER_PATH") + "\n"
                        + "javax.sip.OUTBOUND_PROXY= "
                        + .getProperty("javax.sip.OUTBOUND_PROXY") + "\n"
                        + "-->");
                .println("<description\n logDescription=\"" + 
                        + "\"\n name=\""
                        + .getProperty("javax.sip.STACK_NAME")
                        + "\"\n auxInfo=\"" +  + "\"/>\n ");
                if ( != null) {
                    if (.isLoggingEnabled(.)) {
                        
                                .logDebug("Here are the stack configuration properties \n"
                                        + "javax.sip.IP_ADDRESS= "
                                        + 
                                                .getProperty("javax.sip.IP_ADDRESS")
                                        + "\n"
                                        + "javax.sip.ROUTER_PATH= "
                                        + 
                                                .getProperty("javax.sip.ROUTER_PATH")
                                        + "\n"
                                        + "javax.sip.OUTBOUND_PROXY= "
                                        + 
                                                .getProperty("javax.sip.OUTBOUND_PROXY")
                                        + "\n"
                                        + "gov.nist.javax.sip.CACHE_CLIENT_CONNECTIONS= "
                                        + 
                                                .getProperty("gov.nist.javax.sip.CACHE_CLIENT_CONNECTIONS")
                                        + "\n"
                                        + "gov.nist.javax.sip.CACHE_SERVER_CONNECTIONS= "
                                        + 
                                                .getProperty("gov.nist.javax.sip.CACHE_SERVER_CONNECTIONS")
                                        + "\n"
                                        + "gov.nist.javax.sip.REENTRANT_LISTENER= "
                                        + 
                                                .getProperty("gov.nist.javax.sip.REENTRANT_LISTENER")
                                        + "gov.nist.javax.sip.THREAD_POOL_SIZE= "
                                        + 
                                                .getProperty("gov.nist.javax.sip.THREAD_POOL_SIZE")
                                        + "\n");
                        .logDebug(" ]]> ");
                        .logDebug("</debug>");
                        .logDebug("<description\n logDescription=\"" + 
                                + "\"\n name=\"" +  + "\"\n auxInfo=\"" + 
                                + "\"/>\n ");
                        .logDebug("<debug>");
                        .logDebug("<![CDATA[ ");
                    }
                } else {
                    if (.isLoggingEnabled(.)) {
                        .logDebug("Here are the stack configuration properties \n"
                                +  + "\n");
                        .logDebug(" ]]>");
                        .logDebug("</debug>");
                        .logDebug("<description\n logDescription=\"" + 
                                + "\"\n name=\"" +  + "\" />\n");
                        .logDebug("<debug>");
                        .logDebug("<![CDATA[ ");
                    }
                }
            }
        } catch (IOException ex) {
        }
    }

    
Global check for whether to log or not. To minimize the time return false here.

Returns:
true -- if logging is globally enabled and false otherwise.
    public boolean needsLogging() {
        return  != null;
    }

    
Set the log file name

Parameters:
name is the name of the log file to set.
    public void setLogFileName(String name) {
         = name;
    }

    
return the name of the log file.
    public String getLogFileName() {
        return ;
    }

    
Log a message into the log file.

Parameters:
message message to log into the log file.
    private void logMessage(String message) {
        // String tname = Thread.currentThread().getName();
        checkLogFile();
        String logInfo = message;
        if ( != null) {
            .println(logInfo);
        }
            .logInfo(logInfo);
        }
    }
    private void logMessage(String messageString fromString toboolean sender,
            String callIdString firstLineString statusString tidlong time,
            long timestampVal) {
        LogRecord log = this...createLogRecord(messagefromtotime,
                senderfirstLinetidcallIdtimestampVal);
        if (log != null)
            logMessage(log.toString());
    }

    
Log a message into the log directory.

Parameters:
message a SIPMessage to log
from from header of the message to log into the log directory
to to header of the message to log into the log directory
sender is the server the sender
time is the time to associate with the message.
    public void logMessage(SIPMessage messageString fromString toboolean senderlong time) {
        checkLogFile();
        if (message.getFirstLine() == null)
            return;
        CallID cid = (CallIDmessage.getCallId();
        String callId = null;
        if (cid != null)
            callId = cid.getCallId();
        String firstLine = message.getFirstLine().trim();
        String inputText = ( ? message.encode() : message.encodeMessage());
        String tid = message.getTransactionId();
        TimeStampHeader tsHdr = (TimeStampHeadermessage.getHeader(.);
        long tsval = tsHdr == null ? 0 : tsHdr.getTime();
        logMessage(inputTextfromtosendercallIdfirstLinenulltidtimetsval);
    }

    
Log a message into the log directory.

Parameters:
message a SIPMessage to log
from from header of the message to log into the log directory
to to header of the message to log into the log directory
status the status to log.
sender is the server the sender or receiver (true if sender).
time is the reception time.
    public void logMessage(SIPMessage messageString fromString toString status,
            boolean senderlong time) {
        checkLogFile();
        CallID cid = (CallIDmessage.getCallId();
        String callId = null;
        if (cid != null)
            callId = cid.getCallId();
        String firstLine = message.getFirstLine().trim();
        String encoded = ( ? message.encode() : message.encodeMessage());
        String tid = message.getTransactionId();
        TimeStampHeader tshdr = (TimeStampHeadermessage.getHeader(.);
        long tsval = tshdr == null ? 0 : tshdr.getTime();
        logMessage(encodedfromtosendercallIdfirstLinestatustidtimetsval);
    }

    
Log a message into the log directory. Time stamp associated with the message is the current time.

Parameters:
message a SIPMessage to log
from from header of the message to log into the log directory
to to header of the message to log into the log directory
status the status to log.
sender is the server the sender or receiver (true if sender).
    public void logMessage(SIPMessage messageString fromString toString status,
            boolean sender) {
        logMessage(messagefromtostatussender, System.currentTimeMillis());
    }

    
Log an exception stack trace.

Parameters:
ex Exception to log into the log file
    public void logException(Exception ex) {
        if ( >= ) {
            checkLogFile();
            ex.printStackTrace();
            if ( != null)
                ex.printStackTrace();
        }
    }

    
Set the trace level for the stack.

Parameters:
level -- the trace level to set. The following trace levels are supported:
  • 0 -- no tracing
  • 16 -- trace messages only
  • 32 Full tracing including debug messages.
    public void setTraceLevel(int level) {
         = level;
    }

    
Get the trace level for the stack.

Returns:
the trace level
    public int getTraceLevel() {
        return ;
    }

    
Set aux information. Auxiliary information may be associated with the log file. This is useful for remote logs.

Parameters:
auxInfo -- auxiliary information.
    public void setAuxInfo(String auxInfo) {
        this. = auxInfo;
    }
	public void setSipStack(SipStack sipStack) {
		if(sipStack instanceof SIPTransactionStack) {
			this. = (SIPTransactionStack)sipStack;
	        this. = this..getStackLogger();
		}
		else
			throw new IllegalArgumentException("sipStack must be a SIPTransactionStack");
	}
	public void setStackProperties(Properties stackProperties) {
		setProperties(stackProperties);
	}
	public void setLevel(int jsipLoggingLevel) {
	    
	}
New to GrepCode? Check out our FAQ X