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
;
16 import java
.io
.FileWriter
;
17 import java
.io
.IOException
;
19 import org
.eclipse
.core
.resources
.IWorkspaceRoot
;
20 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
21 import org
.eclipse
.core
.runtime
.IPath
;
22 import org
.eclipse
.core
.runtime
.IStatus
;
23 import org
.eclipse
.core
.runtime
.Status
;
24 import org
.eclipse
.linuxtools
.internal
.lttng2
.ui
.Activator
;
27 * <b><u>ControlCommandLogger</u></b>
29 * Class to log control commands.
32 public class ControlCommandLogger
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
37 private static BufferedWriter fTraceLog
= null;
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
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)
47 public static void init(String filename
, boolean append
) {
48 if (fTraceLog
!= null) {
51 fTraceLog
= openLogFile(filename
, append
);
55 * Closes the log file if open.
57 public static void close() {
58 if (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$
71 * Logs a message to the log file.
72 * @param msg - message (e.g. command or command result) to log
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);
80 message
.append(String
.format("%1$03d", currentTime
% 1000));
83 if (fTraceLog
!= null) {
85 fTraceLog
.write(message
.toString());
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$
95 // ------------------------------------------------------------------------
97 // ------------------------------------------------------------------------
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
104 private static BufferedWriter
openLogFile(String filename
, boolean append
) {
105 IWorkspaceRoot root
= ResourcesPlugin
.getWorkspace().getRoot();
106 IPath newFile
= root
.getLocation().append(filename
);
107 File file
= newFile
.toFile();
108 BufferedWriter outfile
= null;
110 if (!file
.exists()) {
111 file
.createNewFile();
112 file
.setWritable(true, false);
114 outfile
= new BufferedWriter(new FileWriter(file
, append
));
115 } catch (IOException e
) {
116 Activator
.getDefault().getLog().log(new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, IStatus
.ERROR
,
117 "Can't open log file for logging of tracer control commands", e
)); //$NON-NLS-1$
This page took 0.040115 seconds and 5 git commands to generate.