ce1fdb2f3e552e7805d752f8afd0eb71f6f2c933
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / logging / ControlCommandLogger.java
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;
15 import java.io.FileWriter;
16 import java.io.IOException;
17
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 /**
23 * <b><u>ControlCommandLogger</u></b>
24 * <p>
25 * Class to log control commands.
26 * </p>
27 */
28 public class ControlCommandLogger {
29
30 // ------------------------------------------------------------------------
31 // Attributes
32 // ------------------------------------------------------------------------
33 private static BufferedWriter fTraceLog = null;
34
35 // ------------------------------------------------------------------------
36 // Operations
37 // ------------------------------------------------------------------------
38 /**
39 * Initializes the logger class and opens the log file with the given parameter.
40 * @param filename - file name of logger output
41 * @param append - true to open log file in append mode else false (overwrite)
42 */
43 public static void init(String filename, boolean append) {
44 if (fTraceLog != null) {
45 close();
46 }
47 fTraceLog = openLogFile(filename, append);
48 }
49
50 /**
51 * Closes the log file if open.
52 */
53 public static void close() {
54 if (fTraceLog == null)
55 return;
56
57 try {
58 fTraceLog.close();
59 fTraceLog = null;
60 } catch (IOException e) {
61 Activator.getDefault().getLog().log(new Status(IStatus.WARNING, Activator.PLUGIN_ID, IStatus.WARNING,
62 "Can't close log file of the trace control", e)); //$NON-NLS-1$
63 }
64 }
65
66 /**
67 * Logs a message to the log file.
68 * @param msg - message (e.g. command or command result) to log
69 */
70 @SuppressWarnings("nls")
71 public static void log(String msg) {
72 long currentTime = System.currentTimeMillis();
73 StringBuilder message = new StringBuilder("[");
74 message.append(currentTime / 1000);
75 message.append(".");
76 message.append(String.format("%1$03d", currentTime % 1000));
77 message.append("] ");
78 message.append(msg);
79 if (fTraceLog != null) {
80 try {
81 fTraceLog.write(message.toString());
82 fTraceLog.newLine();
83 fTraceLog.flush();
84 } catch (IOException e) {
85 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
86 "Can't log message in log file of the tracer control", e)); //$NON-NLS-1$
87 }
88 }
89 }
90
91 // ------------------------------------------------------------------------
92 // Helper methods
93 // ------------------------------------------------------------------------
94 /**
95 * Opens the trace log file with given name in the workspace root directory
96 * @param filename - file name of logger output
97 * @param append - true to open log file in append mode else false (overwrite)
98 * @return the buffer writer class or null if not successful
99 */
100 private static BufferedWriter openLogFile(String filename, boolean append) {
101 BufferedWriter outfile = null;
102 try {
103 outfile = new BufferedWriter(new FileWriter(filename, append));
104 } catch (IOException e) {
105 Activator.getDefault().getLog().log(new Status(IStatus.ERROR, Activator.PLUGIN_ID, IStatus.ERROR,
106 "Can't open log file for logging of tracer control commands: " + filename, e)); //$NON-NLS-1$
107 }
108 return outfile;
109 }
110 }
111
This page took 0.033508 seconds and 5 git commands to generate.