c308483c0bf5fd794799ff25b7c18547e622a168
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.control.ui / src / org / eclipse / linuxtools / internal / lttng2 / control / ui / views / logging / ControlCommandLogger.java
1 /**********************************************************************
2 * Copyright (c) 2012, 2013 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.control.ui.views.logging;
13
14 import java.io.BufferedWriter;
15 import java.io.FileWriter;
16 import java.io.IOException;
17
18 import org.eclipse.linuxtools.internal.lttng2.control.ui.Activator;
19
20 /**
21 * <p>
22 * Class to log control commands.
23 * </p>
24 *
25 * @author Bernd Hufmann
26 */
27 final public class ControlCommandLogger {
28
29 // ------------------------------------------------------------------------
30 // Attributes
31 // ------------------------------------------------------------------------
32 /**
33 * The bufferd writer reference
34 */
35 private static BufferedWriter fTraceLog = null;
36
37 // ------------------------------------------------------------------------
38 // Constructor
39 // ------------------------------------------------------------------------
40 private ControlCommandLogger() {
41 }
42
43 // ------------------------------------------------------------------------
44 // Operations
45 // ------------------------------------------------------------------------
46 /**
47 * Initializes the logger class and opens the log file with the given parameter.
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 }
57
58 /**
59 * Closes the log file if open.
60 */
61 public static void close() {
62 if (fTraceLog == null) {
63 return;
64 }
65
66 try {
67 fTraceLog.close();
68 fTraceLog = null;
69 } catch (IOException e) {
70 Activator.getDefault().logWarning("Can't close log file of the trace control", e); //$NON-NLS-1$
71 }
72 }
73
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) {
93 Activator.getDefault().logError("Can't log message in log file of the tracer control", e); //$NON-NLS-1$
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) {
108 BufferedWriter outfile = null;
109 try {
110 outfile = new BufferedWriter(new FileWriter(filename, append));
111 } catch (IOException e) {
112 Activator.getDefault().logError("Can't open log file for logging of tracer control commands: " + filename, e); //$NON-NLS-1$
113 }
114 return outfile;
115 }
116 }
117
This page took 0.043071 seconds and 4 git commands to generate.