~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to lttng/org.eclipse.linuxtools.lttng.ui/src/org/eclipse/linuxtools/internal/lttng/ui/TraceDebug.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.eclipse.linuxtools.internal.lttng.ui;
 
2
 
 
3
 
 
4
import java.io.FileWriter;
 
5
import java.io.IOException;
 
6
import java.io.PrintWriter;
 
7
import java.text.SimpleDateFormat;
 
8
import java.util.Date;
 
9
 
 
10
import org.eclipse.core.runtime.ILog;
 
11
import org.eclipse.core.runtime.IStatus;
 
12
import org.eclipse.core.runtime.Platform;
 
13
import org.eclipse.core.runtime.Plugin;
 
14
import org.eclipse.core.runtime.Status;
 
15
 
 
16
@SuppressWarnings("nls")
 
17
public class TraceDebug {
 
18
    static boolean DEBUG = false;
 
19
    static boolean INFO = false;
 
20
    static boolean WARN = false;
 
21
 
 
22
    static boolean CFV = false;
 
23
    static boolean RV = false;
 
24
    static boolean SV = false;
 
25
 
 
26
    private static Plugin plugin = Activator.getDefault();
 
27
    private static String pluginID = Activator.PLUGIN_ID;
 
28
    private static SimpleDateFormat stimeformat = new SimpleDateFormat("HH:mm:ss:SSS");
 
29
 
 
30
    // Note: files are created in $HOME
 
31
    static private PrintWriter fCFVfile = null;
 
32
    static private PrintWriter fRVfile = null;
 
33
    static private PrintWriter fSVfile = null;
 
34
 
 
35
    public static void init() {
 
36
        // Update Trace configuration options
 
37
        String debugTrace = Platform.getDebugOption(pluginID + "/debug");
 
38
        String infoTrace = Platform.getDebugOption(pluginID + "/info");
 
39
        String warnTrace = Platform.getDebugOption(pluginID + "/warn");
 
40
 
 
41
        if (debugTrace != null) {
 
42
            DEBUG = Boolean.valueOf(debugTrace);
 
43
        }
 
44
 
 
45
        if (infoTrace != null) {
 
46
            INFO = Boolean.valueOf(infoTrace);
 
47
        }
 
48
 
 
49
        if (warnTrace != null) {
 
50
            WARN = Boolean.valueOf(warnTrace);
 
51
        }
 
52
 
 
53
        String cfvTrace = Platform.getDebugOption(pluginID + "/cfv");
 
54
        if (cfvTrace != null) {
 
55
            CFV = Boolean.valueOf(cfvTrace);
 
56
            if (CFV) {
 
57
                try {
 
58
                    fCFVfile = new PrintWriter(new FileWriter("CFVTrace.txt"));
 
59
                } catch (IOException e) {
 
60
                    e.printStackTrace();
 
61
                }
 
62
            }
 
63
        }
 
64
 
 
65
        String rvTrace = Platform.getDebugOption(pluginID + "/rv");
 
66
        if (rvTrace != null) {
 
67
            RV = Boolean.valueOf(rvTrace);
 
68
            if (RV) {
 
69
                try {
 
70
                    fRVfile = new PrintWriter(new FileWriter("RVTrace.txt"));
 
71
                } catch (IOException e) {
 
72
                    e.printStackTrace();
 
73
                }
 
74
            }
 
75
        }
 
76
 
 
77
        String svTrace = Platform.getDebugOption(pluginID + "/sv");
 
78
        if (svTrace != null) {
 
79
            SV = Boolean.valueOf(svTrace);
 
80
            if (SV) {
 
81
                try {
 
82
                    fSVfile = new PrintWriter(new FileWriter("SVTrace.txt"));
 
83
                } catch (IOException e) {
 
84
                    e.printStackTrace();
 
85
                }
 
86
            }
 
87
        }
 
88
    }
 
89
 
 
90
    public static void stop() {
 
91
        if (fCFVfile != null) {
 
92
            fCFVfile.close();
 
93
            fCFVfile = null;
 
94
        }
 
95
 
 
96
        if (fRVfile != null) {
 
97
            fRVfile.close();
 
98
            fRVfile = null;
 
99
        }
 
100
 
 
101
        if (fSVfile != null) {
 
102
            fSVfile.close();
 
103
            fSVfile = null;
 
104
        }
 
105
    }
 
106
 
 
107
    public static void traceCFV(String trace) {
 
108
        if (CFV && fCFVfile != null) {
 
109
            fCFVfile.println(trace);
 
110
            fCFVfile.flush();
 
111
        }
 
112
    }
 
113
 
 
114
    public static void traceRV(String trace) {
 
115
        if (RV && fRVfile != null) {
 
116
            fRVfile.println(trace);
 
117
            fRVfile.flush();
 
118
        }
 
119
    }
 
120
 
 
121
    public static void traceSV(String trace) {
 
122
        if (SV && fSVfile != null) {
 
123
            fSVfile.println(trace);
 
124
            fSVfile.flush();
 
125
        }
 
126
    }
 
127
 
 
128
    public static void info(String message) {
 
129
        if (INFO) {
 
130
            ILog logger = plugin.getLog();
 
131
            logger.log(new Status(IStatus.INFO, Activator.PLUGIN_ID, IStatus.OK, message, null));
 
132
        }
 
133
    }
 
134
 
 
135
    public static void warn(String message) {
 
136
        if (WARN) {
 
137
            ILog logger = plugin.getLog();
 
138
            logger.log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, IStatus.WARNING, message, null));
 
139
        }
 
140
    }
 
141
 
 
142
    public static void debug(String message) {
 
143
        if (DEBUG) {
 
144
            String location = getCallingLocation();
 
145
            System.out.println(location + "\n\t-> " + message);
 
146
 
 
147
        }
 
148
    }
 
149
 
 
150
    public static void debug(String message, int additionalStackLines) {
 
151
        if (DEBUG) {
 
152
            String location = getCallingLocation(additionalStackLines);
 
153
            System.out.println(location + "\n\t-> " + message);
 
154
        }
 
155
    }
 
156
 
 
157
    public static void throwException(String message) {
 
158
        if (DEBUG) {
 
159
            try {
 
160
                triggerException(message);
 
161
            } catch (Exception e) {
 
162
                e.printStackTrace();
 
163
            }
 
164
        }
 
165
    }
 
166
 
 
167
    private static void triggerException(String message) throws Exception {
 
168
        throw new Exception(message);
 
169
    }
 
170
 
 
171
    private static String getCallingLocation() {
 
172
        StringBuilder sb = new StringBuilder();
 
173
        sb.append(trace(Thread.currentThread().getStackTrace(), 4));
 
174
        sb.append("\n" + trace(Thread.currentThread().getStackTrace(), 3));
 
175
        return sb.toString();
 
176
    }
 
177
 
 
178
    private static String getCallingLocation(int numOfStackLines) {
 
179
        int stackCalledFromIdx = 3;
 
180
        int earliestRequested = numOfStackLines > 0 ? stackCalledFromIdx + numOfStackLines : stackCalledFromIdx;
 
181
        StringBuilder sb = new StringBuilder();
 
182
        for (int i = earliestRequested; i >= stackCalledFromIdx; i--) {
 
183
            sb.append(trace(Thread.currentThread().getStackTrace(), i) + "\n");
 
184
        }
 
185
        return sb.toString();
 
186
    }
 
187
 
 
188
    private static String trace(StackTraceElement e[], int level) {
 
189
        if (e != null) {
 
190
            level = level >= e.length ? e.length - 1 : level;
 
191
            StackTraceElement s = e[level];
 
192
            if (s != null) {
 
193
                String simpleClassName = s.getClassName();
 
194
                String[] clsNameSegs = simpleClassName.split("\\.");
 
195
                if (clsNameSegs.length > 0)
 
196
                    simpleClassName = clsNameSegs[clsNameSegs.length - 1];
 
197
                return stimeformat.format(new Date()) + " " + simpleClassName + "." + s.getLineNumber() + "." + s.getMethodName();
 
198
            }
 
199
        }
 
200
 
 
201
        return null;
 
202
    }
 
203
 
 
204
    public static boolean isDEBUG() {
 
205
        return DEBUG;
 
206
    }
 
207
 
 
208
    public static boolean isINFO() {
 
209
        return INFO;
 
210
    }
 
211
 
 
212
    public static boolean isWARN() {
 
213
        return WARN;
 
214
    }
 
215
 
 
216
    public static boolean isCFV() {
 
217
        return CFV;
 
218
    }
 
219
 
 
220
    public static boolean isRV() {
 
221
        return RV;
 
222
    }
 
223
 
 
224
    public static boolean isSV() {
 
225
        return SV;
 
226
    }
 
227
}