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 trace capabilities
 *
 * Owner:
 * Version: 1.0
 */
import java.text.DateFormat;   
import java.util.Date;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
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.                                                        */
  static String debugFileName = (System.getProperty("debug") != null) ?
                                 System.getProperty("debug") : null;
  /* Method to query if run-time trace is turned on */
  public static boolean isRuntimeDebugOn()
  {
    return (debugFileName!=null ? true : false);
  }
  /* Method to query run-time trace file */
  private static String debugFileName()
  {
    return debugFileName;
  }
   /* 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 */
   
   /* This method outputs all trace statements to a file.  It will */
   /* create the file if it does not exist, or append to the file  */
   /* if it does exist.
   
   /* This is just a sample to illustrate a concept.  In practice  */
   /* more error checking and error recovery should be added.      */
   /* Additionally, if you anticipate a large volume of trace      */
   /* statements, implement a file limit size.                     */
   /* You may wish to externalize your trace capabilities to your  */
   /* customer through your application's user interface. Your     */
   /* interface could allow the user to choose trace granularity,  */
   /* trace output location, file name, file limit, file           */
   /* truncation mechanism, and so on.                             */
   private static void print(String msg, boolean newLine)
   {
      BufferedWriter mBufWriter = null;
      try
      {
         File file = new File(debugFileName());
         if (file.exists()) /* does file exist? If so, can it be written to */
         {
            if (file.canWrite() == false)
               return;
         }
         else
         {
           String path = null;  /* Does not exist.  Create the directories */
           int firstSlash = debugFileName().indexOf(File.separatorChar);
           int finalSlash = debugFileName().lastIndexOf(File.separatorChar);
           if (finalSlash == 0) { /* error, not valid path */ }
           else if (finalSlash == 1) /* UNIX root dir */
           { 
             path = File.separator;
           }
           else if (firstSlash == finalSlash) 
           { /* for example c:\  Then make sure slash is part of path */
             path = debugFileName().substring(0,finalSlash+1);
           }
           else 
           { path = debugFileName().substring(0,finalSlash); }
           File dir = new File(path);
           dir.mkdirs();
         }
         FileWriter fileWriter = new FileWriter(debugFileName(), true);
         mBufWriter = new BufferedWriter(fileWriter);
         mBufWriter.write(msg);   
         if (newLine) mBufWriter.newLine();          
         mBufWriter.flush();
         mBufWriter.close();
      }
      catch (Throwable e)
      {
         /* Attempt to clean up open files, but throw away error messages */
         try { mBufWriter.close(); } catch (Throwable t) {};
      }
      return;
   }
   private static final String COPYRIGHT = SharedConstants.COPYRIGHT;
}
      

Return to the article.

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