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