You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Logger.java 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package com.feemers.android.fftdrawer.SignalProcessing;
  2. import android.app.Activity;
  3. import android.util.Log;
  4. import android.widget.TextView;
  5. import java.io.PrintWriter;
  6. import java.io.StringWriter;
  7. public class Logger
  8. {
  9. private TextView textView;
  10. private StringBuffer sb = new StringBuffer();
  11. private String tag;
  12. private Activity activity;
  13. public Logger(String tag, String logInitText)
  14. {
  15. this.activity = null;
  16. this.tag = tag;
  17. sb.append(logInitText);
  18. }
  19. public Logger(Activity activity, String tag, TextView textView, String logInitText)
  20. {
  21. this.activity = activity;
  22. this.tag = tag;
  23. this.textView = textView;
  24. sb.append(logInitText);
  25. }
  26. public void log(String s)
  27. {
  28. Log.d(tag, s);
  29. sb.append(s).append("\n");
  30. if (textView != null)
  31. {
  32. if(activity !=null)
  33. {
  34. activity.runOnUiThread(new Runnable()
  35. {
  36. @Override
  37. public void run()
  38. {
  39. textView.setText(sb.toString());
  40. }
  41. });
  42. }
  43. else
  44. {
  45. textView.setText(sb.toString());
  46. }
  47. }
  48. }
  49. public void log(Exception e)
  50. {
  51. StringWriter sw = new StringWriter();
  52. e.printStackTrace(new PrintWriter(sw));
  53. log(sw.toString());
  54. }
  55. public void clearLog()
  56. {
  57. sb.setLength(0);
  58. if (textView != null)
  59. {
  60. textView.setText("");
  61. }
  62. }
  63. public String getLoggedText()
  64. {
  65. return sb.toString();
  66. }
  67. }