~ubuntu-branches/ubuntu/utopic/eclipse-linuxtools/utopic

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2014-05-12 18:11:40 UTC
  • mfrom: (3.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20140512181140-w237r3vsah1tmybz
Tags: 2.2.1-1
* New upstream release.
* Refreshed d/patches.
* Removed eclipse-cdt-valgrind-remote package, all its functionality
  is now provided by eclipse-cdt-profiling-framework-remote.
* Added remove-license-feature.patch.
* Bump Standards-Version to 3.9.5.
* Enable eclipse-changelog package.
* Enable eclipse-rpm-editor package.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2012, 2013 Ericsson
 
3
 * All rights reserved. This program and the accompanying materials
 
4
 * are made available under the terms of the Eclipse Public License v1.0
 
5
 * which accompanies this distribution, and is available at
 
6
 * http://www.eclipse.org/legal/epl-v10.html
 
7
 *
 
8
 *******************************************************************************/
 
9
 
1
10
package org.eclipse.linuxtools.internal.tmf.ui;
2
11
 
3
12
import java.io.BufferedWriter;
6
15
 
7
16
import org.eclipse.core.runtime.Platform;
8
17
 
 
18
/**
 
19
 * Tracer class for the tmf.ui plugin
 
20
 */
9
21
@SuppressWarnings("nls")
10
22
public class TmfUiTracer {
11
23
 
12
 
        private static String pluginID = Activator.PLUGIN_ID;
13
 
 
14
 
        static Boolean ERROR     = Boolean.FALSE;
15
 
        static Boolean WARNING   = Boolean.FALSE;
16
 
        static Boolean INFO      = Boolean.FALSE;
17
 
 
18
 
        static Boolean INDEX         = Boolean.FALSE;
19
 
        static Boolean DISPLAY       = Boolean.FALSE;
20
 
        static Boolean SORTING       = Boolean.FALSE;
21
 
 
22
 
        private static String LOGNAME = "traceUI.log";
23
 
        private static BufferedWriter fTraceLog = null;
24
 
 
25
 
        private static BufferedWriter openLogFile(String filename) {
26
 
                BufferedWriter outfile = null;
27
 
                try {
28
 
                        outfile = new BufferedWriter(new FileWriter(filename));
29
 
                } catch (IOException e) {
30
 
                    Activator.getDefault().logError("Error creating log file " + LOGNAME, e); //$NON-NLS-1$
31
 
                }
32
 
                return outfile;
33
 
        }
34
 
 
35
 
        public static void init() {
36
 
 
37
 
                String traceKey;
38
 
                boolean isTracing = false;
39
 
                
40
 
                traceKey = Platform.getDebugOption(pluginID + "/error");
41
 
                if (traceKey != null) {
42
 
                        ERROR = (Boolean.valueOf(traceKey)).booleanValue();
43
 
                        isTracing |= ERROR;
44
 
                }
45
 
 
46
 
                traceKey = Platform.getDebugOption(pluginID + "/warning");
47
 
                if (traceKey != null) {
48
 
                        WARNING = (Boolean.valueOf(traceKey)).booleanValue();
49
 
                        isTracing |= WARNING;
50
 
                }
51
 
 
52
 
                traceKey = Platform.getDebugOption(pluginID + "/info");
53
 
                if (traceKey != null) {
54
 
                        INFO = (Boolean.valueOf(traceKey)).booleanValue();
55
 
                        isTracing |= INFO;
56
 
                }
57
 
 
58
 
                traceKey = Platform.getDebugOption(pluginID + "/updateindex");
59
 
                if (traceKey != null) {
60
 
                    INDEX = (Boolean.valueOf(traceKey)).booleanValue();
61
 
                        isTracing |= INDEX;
62
 
                }
63
 
 
64
 
                traceKey = Platform.getDebugOption(pluginID + "/display");
65
 
                if (traceKey != null) {
66
 
                    DISPLAY = (Boolean.valueOf(traceKey)).booleanValue();
67
 
                        isTracing |= DISPLAY;
68
 
                }
69
 
 
70
 
                traceKey = Platform.getDebugOption(pluginID + "/sorting");
71
 
                if (traceKey != null) {
72
 
                    SORTING = (Boolean.valueOf(traceKey)).booleanValue();
73
 
                        isTracing |= SORTING;
74
 
                }
75
 
 
76
 
                // Create trace log file if needed
77
 
                if (isTracing) {
78
 
                        fTraceLog = openLogFile(LOGNAME);
79
 
                }
80
 
        }
81
 
 
82
 
        public static void stop() {
83
 
                if (fTraceLog == null)
84
 
                        return;
85
 
 
86
 
                try {
87
 
                        fTraceLog.close();
88
 
                        fTraceLog = null;
89
 
                } catch (IOException e) {
90
 
                  Activator.getDefault().logError("Error closing log file " + LOGNAME, e); //$NON-NLS-1$
91
 
                }
92
 
        }
93
 
 
94
 
        // Predicates
95
 
        public static boolean isErrorTraced() {
96
 
                return ERROR;
97
 
        }
98
 
 
99
 
        public static boolean isIndexTraced() {
100
 
                return INDEX;
101
 
        }
102
 
        
103
 
        public static boolean isDisplayTraced() {
104
 
                return DISPLAY;
105
 
        }
106
 
        
107
 
        public static boolean isSortingTraced() {
108
 
                return SORTING;
109
 
        }
110
 
 
111
 
        // Tracers
112
 
        public static void trace(String msg) {
113
 
                long currentTime = System.currentTimeMillis();
114
 
                StringBuilder message = new StringBuilder("[");
115
 
                message.append(currentTime / 1000);
116
 
                message.append(".");
117
 
                message.append(String.format("%1$03d", currentTime % 1000));
118
 
                message.append("] ");
119
 
                message.append(msg);
120
 
 
121
 
                if (fTraceLog != null) {
122
 
                        try {
123
 
                                fTraceLog.write(message.toString());
124
 
                                fTraceLog.newLine();
125
 
                                fTraceLog.flush();
126
 
                        } catch (IOException e) {
127
 
                         Activator.getDefault().logError("Error writing to log file " + LOGNAME, e); //$NON-NLS-1$
128
 
                        }
129
 
                }
130
 
        }
131
 
 
132
 
        public static void traceIndex(String msg) {
133
 
            String message = ("[INDEX] " + msg);
134
 
            trace(message);
135
 
        }
136
 
        
137
 
        public static void traceDisplay(String msg) {
138
 
                String message = ("[DISPLAY]" + msg);
139
 
                trace(message);
140
 
        }
141
 
 
142
 
        public static void traceSorting(String msg) {
143
 
                String message = ("[SORT] " + msg);
144
 
                trace(message);
145
 
        }
146
 
 
147
 
        public static void traceError(String msg) {
148
 
                String message = ("[ERR] Thread=" + Thread.currentThread().getId() + " " + msg);
149
 
                trace(message);
150
 
        }
151
 
 
152
 
        public static void traceWarning(String msg) {
153
 
            String message = ("[WARN] Thread=" + Thread.currentThread().getId() + " " + msg);
154
 
            trace(message);
155
 
        }
156
 
        
157
 
        public static void traceInfo(String msg) {
158
 
                String message = ("[INF] Thread=" + Thread.currentThread().getId() + " " + msg);
159
 
                trace(message);
160
 
        }
161
 
        
162
 
        
 
24
    private static String pluginID = Activator.PLUGIN_ID;
 
25
 
 
26
    static Boolean ERROR   = Boolean.FALSE;
 
27
    static Boolean WARNING = Boolean.FALSE;
 
28
    static Boolean INFO    = Boolean.FALSE;
 
29
 
 
30
    static Boolean INDEX   = Boolean.FALSE;
 
31
    static Boolean DISPLAY = Boolean.FALSE;
 
32
    static Boolean SORTING = Boolean.FALSE;
 
33
 
 
34
    private static String LOGNAME = "traceUI.log";
 
35
    private static BufferedWriter fTraceLog = null;
 
36
 
 
37
    private static BufferedWriter openLogFile(String filename) {
 
38
        BufferedWriter outfile = null;
 
39
        try {
 
40
            outfile = new BufferedWriter(new FileWriter(filename));
 
41
        } catch (IOException e) {
 
42
            Activator.getDefault().logError("Error creating log file " + LOGNAME, e); //$NON-NLS-1$
 
43
        }
 
44
        return outfile;
 
45
    }
 
46
 
 
47
    /**
 
48
     * Initialize tracing
 
49
     */
 
50
    public static void init() {
 
51
 
 
52
        String traceKey;
 
53
        boolean isTracing = false;
 
54
 
 
55
        traceKey = Platform.getDebugOption(pluginID + "/error");
 
56
        if (traceKey != null) {
 
57
            ERROR = (Boolean.valueOf(traceKey)).booleanValue();
 
58
            isTracing |= ERROR;
 
59
        }
 
60
 
 
61
        traceKey = Platform.getDebugOption(pluginID + "/warning");
 
62
        if (traceKey != null) {
 
63
            WARNING = (Boolean.valueOf(traceKey)).booleanValue();
 
64
            isTracing |= WARNING;
 
65
        }
 
66
 
 
67
        traceKey = Platform.getDebugOption(pluginID + "/info");
 
68
        if (traceKey != null) {
 
69
            INFO = (Boolean.valueOf(traceKey)).booleanValue();
 
70
            isTracing |= INFO;
 
71
        }
 
72
 
 
73
        traceKey = Platform.getDebugOption(pluginID + "/updateindex");
 
74
        if (traceKey != null) {
 
75
            INDEX = (Boolean.valueOf(traceKey)).booleanValue();
 
76
            isTracing |= INDEX;
 
77
        }
 
78
 
 
79
        traceKey = Platform.getDebugOption(pluginID + "/display");
 
80
        if (traceKey != null) {
 
81
            DISPLAY = (Boolean.valueOf(traceKey)).booleanValue();
 
82
            isTracing |= DISPLAY;
 
83
        }
 
84
 
 
85
        traceKey = Platform.getDebugOption(pluginID + "/sorting");
 
86
        if (traceKey != null) {
 
87
            SORTING = (Boolean.valueOf(traceKey)).booleanValue();
 
88
            isTracing |= SORTING;
 
89
        }
 
90
 
 
91
        // Create trace log file if needed
 
92
        if (isTracing) {
 
93
            fTraceLog = openLogFile(LOGNAME);
 
94
        }
 
95
    }
 
96
 
 
97
    /**
 
98
     * Stop tracing
 
99
     */
 
100
    public static void stop() {
 
101
        if (fTraceLog == null) {
 
102
            return;
 
103
        }
 
104
 
 
105
        try {
 
106
            fTraceLog.close();
 
107
            fTraceLog = null;
 
108
        } catch (IOException e) {
 
109
            Activator.getDefault().logError("Error closing log file " + LOGNAME, e); //$NON-NLS-1$
 
110
        }
 
111
    }
 
112
 
 
113
    // ------------------------------------------------------------------------
 
114
    // Predicates
 
115
    // ------------------------------------------------------------------------
 
116
 
 
117
    /**
 
118
     * @return If ERROR messages are traced
 
119
     */
 
120
    public static boolean isErrorTraced() {
 
121
        return ERROR;
 
122
    }
 
123
 
 
124
    /**
 
125
     * @return If INDEX messages are traced
 
126
     */
 
127
    public static boolean isIndexTraced() {
 
128
        return INDEX;
 
129
    }
 
130
 
 
131
    /**
 
132
     * @return If DISPLAY messages are traced
 
133
     */
 
134
    public static boolean isDisplayTraced() {
 
135
        return DISPLAY;
 
136
    }
 
137
 
 
138
    /**
 
139
     * @return If SORTING messages are traced
 
140
     */
 
141
    public static boolean isSortingTraced() {
 
142
        return SORTING;
 
143
    }
 
144
 
 
145
 
 
146
    // ------------------------------------------------------------------------
 
147
    // Tracing methods
 
148
    // ------------------------------------------------------------------------
 
149
 
 
150
    /**
 
151
     * Trace a generic event
 
152
     *
 
153
     * @param msg
 
154
     *            The event's message
 
155
     */
 
156
    public static void trace(String msg) {
 
157
        long currentTime = System.currentTimeMillis();
 
158
        StringBuilder message = new StringBuilder("[");
 
159
        message.append(currentTime / 1000);
 
160
        message.append(".");
 
161
        message.append(String.format("%1$03d", currentTime % 1000));
 
162
        message.append("] ");
 
163
        message.append(msg);
 
164
 
 
165
        if (fTraceLog != null) {
 
166
            try {
 
167
                fTraceLog.write(message.toString());
 
168
                fTraceLog.newLine();
 
169
                fTraceLog.flush();
 
170
            } catch (IOException e) {
 
171
                Activator.getDefault().logError("Error writing to log file " + LOGNAME, e); //$NON-NLS-1$
 
172
            }
 
173
        }
 
174
    }
 
175
 
 
176
    /**
 
177
     * Trace an INDEX event
 
178
     *
 
179
     * @param msg
 
180
     *            The event's message
 
181
     */
 
182
    public static void traceIndex(String msg) {
 
183
        String message = ("[INDEX] " + msg);
 
184
        trace(message);
 
185
    }
 
186
 
 
187
    /**
 
188
     * Trace a DISPLAY event
 
189
     *
 
190
     * @param msg
 
191
     *            The event's message
 
192
     */
 
193
    public static void traceDisplay(String msg) {
 
194
        String message = ("[DISPLAY]" + msg);
 
195
        trace(message);
 
196
    }
 
197
 
 
198
    /**
 
199
     * Trace a SORTING event
 
200
     *
 
201
     * @param msg
 
202
     *            The event's message
 
203
     */
 
204
    public static void traceSorting(String msg) {
 
205
        String message = ("[SORT] " + msg);
 
206
        trace(message);
 
207
    }
 
208
 
 
209
    /**
 
210
     * Trace an ERROR event
 
211
     *
 
212
     * @param msg
 
213
     *            The event's message
 
214
     */
 
215
    public static void traceError(String msg) {
 
216
        String message = ("[ERR] Thread=" + Thread.currentThread().getId() + " " + msg);
 
217
        trace(message);
 
218
    }
 
219
 
 
220
    /**
 
221
     * Trace a WARNING event
 
222
     *
 
223
     * @param msg
 
224
     *            The event's message
 
225
     */
 
226
    public static void traceWarning(String msg) {
 
227
        String message = ("[WARN] Thread=" + Thread.currentThread().getId() + " " + msg);
 
228
        trace(message);
 
229
    }
 
230
 
 
231
    /**
 
232
     * Trace an INFO event
 
233
     *
 
234
     * @param msg
 
235
     *            The event's message
 
236
     */
 
237
    public static void traceInfo(String msg) {
 
238
        String message = ("[INF] Thread=" + Thread.currentThread().getId() + " " + msg);
 
239
        trace(message);
 
240
    }
163
241
 
164
242
}