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