IBM®
跳转到主要内容
    中国 [选择]    使用条款
 
 
Select a scope: Search for:    
    首页    产品    服务与解决方案     支持与下载    个性化服务    
跳转到主要内容

developerWorks 中国  >  Java technology  >

Techniques for adding trace statements to your Java application

Use tracing during development as well as after your application is deployed

developerWorks


File PrintMessage.java:
				
/*                                               
 * Class Name:   PrintMessage
 *
 * Purpose:  Provide run-time time trace capabilities
 *
 * Owner:
 * Version: 1.0
 */
import java.text.DateFormat;   
import java.util.Date;                 
public class PrintMessage
{
  static DateFormat df = DateFormat.getDateTimeInstance(DateFormat.SHORT,
                                                        DateFormat.MEDIUM);
  /*  The following static variable and function are needed to implement  */
  /*  run-time tracing.  The function isRunTimeDebugOn() should be called */
  /*  by your application code prior to making use of any of the output   */
  /*  trace calls.                                                        */
  /*  Retrieve customer run-time trace on/off indication */
  static boolean debugOn = ((System.getProperty("debug") != null) 
                    (System.getProperty("debug").equals("1"))) ? true : false;
  /* Method to query if run-time trace is turned on */
  public static boolean isRuntimeDebugOn()
  {
    return debugOn;
  }
   /* The following set of functions are made available for your application */
   /* code.  Write a re-usable set of methods allows implementation of trace */
   /* output to be controlled from one central place.  Later, the output     */
   /* location can be changed, the format of the output message can be       */
   /* changed, etc.                                                          */
   /* Output trace functions */
   public static void printMessageWithDateTime(String msg)
   {
      msg = new String(df.format(new Date()) + ":  " + msg);
      print(msg, true);
      return;
   }
   public static void printMessage(String msg)
   {
      print(msg, true);
      return;
   }
   public static void printMessageNoNewLine(String msg)
   {
      print(msg, false);
      return;
   }
   /* private method that all public print messages call to output msg */
   private static void print(String msg, boolean newLine)
   {
      if (newLine)
         System.out.println(msg);
      else
         System.out.print(msg);
      return;
   }
   private static final String COPYRIGHT = SharedConstants.COPYRIGHT;
}
      

Return to the article.

    关于 IBM 隐私条约 联系 IBM 使用条款