FileDocCategorySizeDatePackage
TraceImplementation.javaAPI DocJava SE 5 API6620Fri Aug 26 14:55:06 BST 2005com.sun.jmx.trace

TraceImplementation

public class TraceImplementation extends Object implements TraceDestination
Example implementation of the {@link TraceDestination} interface.
This implementation sends log records to a file (specified by the value of the com.sun.jmx.trace.file system property) or to the system console if no file is specified.
The log level is specified by the value of system property com.sun.jmx.trace.level, which can be : DEBUG, TRACE, ERROR. If no trace level is specified, the default is ERROR.
Note that this implementation does not provide any filtering based on the log types. More precisely, the implementation of method {@link #isSelected} only checks the provided log level.
since
1.5

Fields Summary
private PrintWriter
out
private int
level
Constructors Summary
public TraceImplementation()
Constructor.

exception
IOException may be thrown when creating the new log file based on the value of system property com.sun.jmx.trace.file

    String filename;
    if ((filename = System.getProperty("com.sun.jmx.trace.file")) != null)
    {
      // Output sent to the specified log file
      //
      this.out = new PrintWriter(new FileOutputStream(filename), true);
    }
    else
    {
      // Output sent to the system console
      //
      this.out = new PrintWriter(System.err, true);
    }

    String level;
    if ((level = System.getProperty("com.sun.jmx.trace.level")) != null)
    {
      // Read log level from value of system property
      //
      if (level.equals("DEBUG"))
      {
        this.level = TraceTags.LEVEL_DEBUG;
      }
      else if (level.equals("TRACE"))
      {
        this.level = TraceTags.LEVEL_TRACE;
      }
      else 
      {
        this.level = TraceTags.LEVEL_ERROR;
      }
    }
    else
    {
      // Log level defaults to ERROR
      //
      this.level = TraceTags.LEVEL_ERROR;
    }
  
Methods Summary
private static java.lang.StringgetLevel(int level)
Return the string representation of a trace level, as defined in {@link TraceTags}

    
    switch (level) {
    
    case TraceTags.LEVEL_ERROR:
      return "LEVEL_ERROR";

    case TraceTags.LEVEL_TRACE:
      return "LEVEL_TRACE";

    case TraceTags.LEVEL_DEBUG:
      return "LEVEL_DEBUG";

    default :
      return "UNKNOWN_TRACE_LEVEL";
    }
  
private static java.lang.StringgetType(int type)
Return the string representation of a trace type, as defined in {@link TraceTags}

       
    switch (type) {
    
    case TraceTags.INFO_MBEANSERVER:
      return "INFO_MBEANSERVER";

    case TraceTags.INFO_ADAPTOR_SNMP:
      return "INFO_ADAPTOR_SNMP";

    case TraceTags.INFO_SNMP:
      return "INFO_SNMP";

    case TraceTags.INFO_MLET:
      return "INFO_MLET";

    case TraceTags.INFO_MONITOR:
      return "INFO_MONITOR";

    case TraceTags.INFO_TIMER:
      return "INFO_TIMER";

    case TraceTags.INFO_MISC:
      return "INFO_MISC";

    case TraceTags.INFO_NOTIFICATION:
      return "INFO_NOTIFICATION";

    case TraceTags.INFO_RELATION:
      return "INFO_RELATION";
   
    case TraceTags.INFO_MODELMBEAN:
      return "INFO_MODELMBEAN";

    default:
      return "UNKNOWN_TRACE_TYPE";
    }
  
public static voidinit()
Registers a new instance of class {@link TraceImplementation} as the trace destination in class {@link Trace}.

exception
IOException may be thrown when creating the new log file based on the value of system property com.sun.jmx.trace.file
see
Trace#setDestination

    Trace.setDestination(new TraceImplementation());
  
public static voidinit(int level)
Registers a new instance of class {@link TraceImplementation} as the trace destination in class {@link Trace}.

param
level Initial trace level (see {@link TraceTags})
exception
IOException may be thrown when creating the new log file based on the value of system property com.sun.jmx.trace.file
see
Trace#setDestination

      final TraceImplementation impl = new TraceImplementation();
      impl.level = level;
      Trace.setDestination(impl);
  
public booleanisSelected(int level, int type)
Only tests whether the provided log level will generate some output if used as an argument to {@link #send(int, int, String, String, Throwable)} or {@link #send(int, int, String, String, String)}

see
TraceDestination#isSelected

    return (level <= this.level);
  
static com.sun.jmx.trace.TraceImplementationnewDestination(int level)

      try {
	  final TraceImplementation impl = new TraceImplementation();
	  impl.level = level;
	  return impl;
      } catch (IOException x) {
	  return null;
      }
  
public voidreset()
Not implemented.

see
TraceDestination#reset


  
public booleansend(int level, int type, java.lang.String className, java.lang.String methodName, java.lang.String info)

see
TraceDestination#send(int, int, String, String, String)

    if (isSelected(level, type))
    {
      out.println(((className!=null)?"Class:  " + className:"")+ 
                  ((methodName!=null)?"\nMethod: " + methodName:"") +
                  "\n\tlevel:   " + getLevel(level) +
                  "\n\ttype:    " + getType(type) +
                  "\n\tmessage: " + info);
      //out.flush();
      return true;
    }
    return false;
  
public booleansend(int level, int type, java.lang.String className, java.lang.String methodName, java.lang.Throwable exception)

see
TraceDestination#send(int, int, String, String, Throwable)

      final boolean result = send(level, type, className, methodName, 
				  exception.toString());
      if (result) 
	  exception.printStackTrace(out);
      
      return result;