ce1fdb2f3e552e7805d752f8afd0eb71f6f2c933
1 /**********************************************************************
2 * Copyright (c) 2012 Ericsson
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
10 * Bernd Hufmann - Initial API and implementation
11 **********************************************************************/
12 package org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.views
.control
.logging
;
14 import java
.io
.BufferedWriter
;
15 import java
.io
.FileWriter
;
16 import java
.io
.IOException
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.core
.runtime
.Status
;
20 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.Activator
;
23 * <b><u>ControlCommandLogger</u></b>
25 * Class to log control commands.
28 public class ControlCommandLogger
{
30 // ------------------------------------------------------------------------
32 // ------------------------------------------------------------------------
33 private static BufferedWriter fTraceLog
= null;
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
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)
43 public static void init(String filename
, boolean append
) {
44 if (fTraceLog
!= null) {
47 fTraceLog
= openLogFile(filename
, append
);
51 * Closes the log file if open.
53 public static void close() {
54 if (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$
67 * Logs a message to the log file.
68 * @param msg - message (e.g. command or command result) to log
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);
76 message
.append(String
.format("%1$03d", currentTime
% 1000));
79 if (fTraceLog
!= null) {
81 fTraceLog
.write(message
.toString());
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$
91 // ------------------------------------------------------------------------
93 // ------------------------------------------------------------------------
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
100 private static BufferedWriter
openLogFile(String filename
, boolean append
) {
101 BufferedWriter outfile
= null;
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$
This page took 0.033508 seconds and 5 git commands to generate.