Advertisement
syndarin

Untitled

Mar 31st, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. public class Logger {
  2.  
  3. private final static String FILENAME = "regall.log";
  4.  
  5. private static Context mContext;
  6.  
  7. private static String mId;
  8.  
  9. private static PrintWriter mLogWriter;
  10.  
  11. public static void init(Context context, String id){
  12. mContext = context;
  13. mId = id;
  14.  
  15. if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
  16. File storageDir = Environment.getExternalStorageDirectory();
  17. File logFile = new File(storageDir, FILENAME);
  18. try {
  19. if(!logFile.exists()){
  20. logFile.createNewFile();
  21. }
  22.  
  23. mLogWriter = new PrintWriter(new FileOutputStream(logFile), true);
  24. mLogWriter.write("-------------------------------------------------------------------------\n");
  25. mLogWriter.write("setup debug session " + DateFormat.format("dd.MM.yyyy kk:mm:ss", Calendar.getInstance()) + "\n");
  26. mLogWriter.write("id - " + mId + "\n");
  27. mLogWriter.write("-------------------------------------------------------------------------\n");
  28. } catch (FileNotFoundException e) {
  29. e.printStackTrace();
  30. Toast.makeText(mContext, "Cannot open local log file!", Toast.LENGTH_LONG).show();
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. Toast.makeText(mContext, "Cannot create log file!", Toast.LENGTH_LONG).show();
  34. }
  35. } else {
  36. Toast.makeText(mContext, "Logging unavailable due to incorrect storage state!", Toast.LENGTH_LONG).show();
  37. }
  38. }
  39.  
  40. public static void shutdown(){
  41. mContext = null;
  42. mLogWriter.flush();
  43. mLogWriter.close();
  44. }
  45.  
  46. public static void logDebug(String tag, String message){
  47. String record = String.format("%s - %s - %s\n", "debug", tag, message);
  48. mLogWriter.write(record);
  49. }
  50.  
  51. public static void logError(String tag, String message){
  52. String record = String.format("%s - %s - %s\n", "error", tag, message);
  53. mLogWriter.write(record);
  54. }
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement