Advertisement
irishstorm

Debug.java

Feb 29th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. /*  Class : Debug,
  2.     Date : 29/02/2016,
  3.     Author : Christopher Cullen,
  4.     Description : output debug messages to a File.      */
  5. import java.io.FileWriter;
  6. import java.io.BufferedWriter;
  7. import java.io.PrintWriter;
  8. import java.io.File;
  9. import java.io.IOException;
  10. import java.text.SimpleDateFormat;
  11. import java.util.Calendar;
  12.  
  13. public class Debug
  14. {
  15.     //  Overload the method
  16.     public static void log(String funcName, int lineNumber, String msg){
  17.         PrintToFile("[Debug] " + funcName + " Function, Line Number: " + lineNumber + " - " + msg);
  18.     }
  19.     public static void log(String funcName, String msg){
  20.         PrintToFile("[Debug] " + funcName + " Function - " + msg);
  21.     }
  22.     public static void log(String funcName, int lineNumber){
  23.         PrintToFile("[Debug] " + funcName + " Function, Line Number: " + lineNumber);
  24.     }
  25.     public static void log(String msg){
  26.         PrintToFile("[Debug] " + msg);
  27.     }
  28.     public static void log(int lineNumber){
  29.         PrintToFile("[Debug] Line Number: " + lineNumber);
  30.     }
  31.  
  32.     public static void PrintToFile(String msg){
  33.         try{  
  34.             //  Get the Date and Time
  35.             Calendar calendar = Calendar.getInstance();
  36.             SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss dd-mm-yyyy");
  37.             String time = simpleDateFormat.format(calendar.getTime());
  38.  
  39.             //  Create a folder
  40.             File folder = new File("Debug Logs");
  41.             folder.mkdir();
  42.  
  43.             //  Setup the file name and path
  44.             String fileName = "Logs";
  45.             String filePath = "C://" + folder +"/" + fileName;
  46.  
  47.             //  Check to see if the file exists
  48.             File file = new File(filePath + ".txt");
  49.             if(!file.exists())
  50.                 file.createNewFile();
  51.  
  52.             //  Convenience class for writing character files.
  53.             FileWriter fileWriter = new FileWriter(file, true);
  54.            
  55.             //  Writes text to a character-output stream, buffering characters so as to provide for the efficient writing of single characters, arrays, and strings.
  56.             BufferedWriter bufferWriter = new BufferedWriter(fileWriter);
  57.            
  58.             //  Prints formatted representations of objects to a text-output stream.
  59.             PrintWriter printWriter = new PrintWriter(bufferWriter);
  60.  
  61.             //  Print it to the log
  62.             printWriter.println("[" + time + "]" + msg);
  63.             printWriter.close();
  64.         }
  65.         catch(IOException ioe){
  66.            System.out.println(ioe);
  67.            ioe.printStackTrace();
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement