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.

Log.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.vaadin.automatedtests.util;
  5. import java.io.Serializable;
  6. import java.text.DateFormat;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. import java.util.HashMap;
  10. import java.util.Iterator;
  11. /**
  12. *
  13. * Execution output and error messages should be handled through this class. It
  14. * is likely that we need these messages back to TT Server at some point just to
  15. * figure out what went wrong.
  16. *
  17. */
  18. public class Log implements Serializable {
  19. // 3 (errors only)
  20. // 2 (+ warnings)
  21. // 1 (+logs)
  22. // 0 (all, print messages also to System.out)
  23. public static final int debug = 0;
  24. // Should class.method() and it's call times be told on debug?
  25. public static final boolean showClassInformation = true;
  26. public static DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  27. public static int DEBUG = 0;
  28. public static int LOG = 1;
  29. public static int WARN = 2;
  30. public static int ERROR = 3;
  31. private static Log log;
  32. public static HashMap classMethodCallCounter = new HashMap();
  33. static {
  34. log = new Log();
  35. }
  36. public static void reset() {
  37. classMethodCallCounter = new HashMap();
  38. }
  39. public static String getNow() {
  40. return df.format(new Date());
  41. }
  42. private Log() {
  43. }
  44. public static String getSource() {
  45. StackTraceElement[] st = new Throwable().fillInStackTrace()
  46. .getStackTrace();
  47. try {
  48. String key = "";
  49. String methodName = st[3].getMethodName();
  50. int line = st[3].getLineNumber();
  51. String clazz = st[3].getClassName() + ".java";
  52. key = "(" + clazz + ":" + line + ")" + " " + methodName;
  53. Integer value = (Integer) classMethodCallCounter.get(key);
  54. if (value == null) {
  55. value = new Integer(1);
  56. } else {
  57. value = new Integer(value.intValue() + 1);
  58. }
  59. classMethodCallCounter.put(key, value);
  60. return value.intValue() + ": " + key;
  61. } catch (Exception e) {
  62. return "unknown class.method";
  63. }
  64. }
  65. public static String getClassMethodCounters() {
  66. String result = "";
  67. for (final Iterator it = classMethodCallCounter.keySet().iterator(); it
  68. .hasNext();) {
  69. String key = (String) it.next();
  70. result += classMethodCallCounter.get(key) + ": " + key + "\n";
  71. }
  72. return result;
  73. }
  74. public void add(int type, String message) {
  75. String source = getSource();
  76. if (type >= debug) {
  77. if (showClassInformation) {
  78. System.out.println(source + ": " + message);
  79. } else {
  80. System.out.println(message);
  81. }
  82. }
  83. }
  84. public static void debug(String message) {
  85. log.add(DEBUG, message);
  86. }
  87. public static void log(String message) {
  88. log.add(LOG, message);
  89. }
  90. public static void warn(String message) {
  91. log.add(WARN, message);
  92. }
  93. public static void error(String message) {
  94. log.add(ERROR, message);
  95. }
  96. /**
  97. * Simple way to check for memory consumption without profiler.
  98. */
  99. public static String getMemoryStatistics() {
  100. // You should call gc before printing statistics (if you are not using a
  101. // profiler)
  102. System.gc();
  103. long inUse = (Runtime.getRuntime().totalMemory() - Runtime.getRuntime()
  104. .freeMemory());
  105. return "Memory:\n" + inUse + " (Used)\n"
  106. + Runtime.getRuntime().totalMemory() + " (Total)\n"
  107. + Runtime.getRuntime().freeMemory() + " (Free)\n";
  108. }
  109. }