Commit | Line | Data |
---|---|---|
afe13e7a BH |
1 | /********************************************************************** |
2 | * Copyright (c) 2012 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Bernd Hufmann - Initial API and implementation | |
11 | **********************************************************************/ | |
12 | package org.eclipse.linuxtools.internal.lttng2.ui.views.control.logging; | |
13 | ||
14 | import java.io.BufferedWriter; | |
afe13e7a BH |
15 | import java.io.FileWriter; |
16 | import java.io.IOException; | |
17 | ||
afe13e7a BH |
18 | import org.eclipse.core.runtime.IStatus; |
19 | import org.eclipse.core.runtime.Status; | |
20 | import org.eclipse.linuxtools.internal.lttng2.ui.Activator; | |
21 | ||
22 | /** | |
afe13e7a BH |
23 | * <p> |
24 | * Class to log control commands. | |
25 | * </p> | |
dbd4432d BH |
26 | * |
27 | * @author Bernd Hufmann | |
afe13e7a BH |
28 | */ |
29 | public class ControlCommandLogger { | |
30 | ||
31 | // ------------------------------------------------------------------------ | |
32 | // Attributes | |
33 | // ------------------------------------------------------------------------ | |
dbd4432d BH |
34 | /** |
35 | * The bufferd writer reference | |
36 | */ | |
afe13e7a BH |
37 | private static BufferedWriter fTraceLog = null; |
38 | ||
39 | // ------------------------------------------------------------------------ | |
40 | // Operations | |
41 | // ------------------------------------------------------------------------ | |
42 | /** | |
43 | * Initializes the logger class and opens the log file with the given parameter. | |
44 | * @param filename - file name of logger output | |
45 | * @param append - true to open log file in append mode else false (overwrite) | |
46 | */ | |
47 | public static void init(String filename, boolean append) { | |
48 | if (fTraceLog != null) { | |
49 | close(); | |
50 | } | |
51 | fTraceLog = openLogFile(filename, append); | |
52 | } | |
53 | ||
54 | /** | |
55 | * Closes the log file if open. | |
56 | */ | |
57 | public static void close() { | |
58 | if (fTraceLog == null) | |
59 | return; | |
60 | ||
61 | try { | |
62 | fTraceLog.close(); | |
63 | fTraceLog = null; | |
64 | } catch (IOException e) { | |
65 | Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, IStatus.WARNING, | |
66 | "Can't close log file of the trace control", e)); //$NON-NLS-1$ | |
67 | } | |
68 | } | |
69 | ||
70 | /** | |
71 | * Logs a message to the log file. | |
72 | * @param msg - message (e.g. command or command result) to log | |
73 | */ | |
74 | @SuppressWarnings("nls") | |
75 | public static void log(String msg) { | |
76 | long currentTime = System.currentTimeMillis(); | |
77 | StringBuilder message = new StringBuilder("["); | |
78 | message.append(currentTime / 1000); | |
79 | message.append("."); | |
80 | message.append(String.format("%1$03d", currentTime % 1000)); | |
81 | message.append("] "); | |
82 | message.append(msg); | |
83 | if (fTraceLog != null) { | |
84 | try { | |
85 | fTraceLog.write(message.toString()); | |
86 | fTraceLog.newLine(); | |
87 | fTraceLog.flush(); | |
88 | } catch (IOException e) { | |
89 | Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, | |
90 | "Can't log message in log file of the tracer control", e)); //$NON-NLS-1$ | |
91 | } | |
92 | } | |
93 | } | |
94 | ||
95 | // ------------------------------------------------------------------------ | |
96 | // Helper methods | |
97 | // ------------------------------------------------------------------------ | |
98 | /** | |
99 | * Opens the trace log file with given name in the workspace root directory | |
100 | * @param filename - file name of logger output | |
101 | * @param append - true to open log file in append mode else false (overwrite) | |
102 | * @return the buffer writer class or null if not successful | |
103 | */ | |
104 | private static BufferedWriter openLogFile(String filename, boolean append) { | |
afe13e7a BH |
105 | BufferedWriter outfile = null; |
106 | try { | |
0886cf00 | 107 | outfile = new BufferedWriter(new FileWriter(filename, append)); |
afe13e7a BH |
108 | } catch (IOException e) { |
109 | Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR, | |
0886cf00 | 110 | "Can't open log file for logging of tracer control commands: " + filename, e)); //$NON-NLS-1$ |
afe13e7a BH |
111 | } |
112 | return outfile; | |
113 | } | |
afe13e7a | 114 | } |
0886cf00 | 115 |