Commit | Line | Data |
---|---|---|
afe13e7a BH |
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.File; | |
16 | import java.io.FileWriter; | |
17 | import java.io.IOException; | |
18 | ||
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; | |
25 | ||
26 | /** | |
27 | * <b><u>ControlCommandLogger</u></b> | |
28 | * <p> | |
29 | * Class to log control commands. | |
30 | * </p> | |
31 | */ | |
32 | public class ControlCommandLogger { | |
33 | ||
34 | // ------------------------------------------------------------------------ | |
35 | // Attributes | |
36 | // ------------------------------------------------------------------------ | |
37 | private static BufferedWriter fTraceLog = null; | |
38 | ||
39 | // ------------------------------------------------------------------------ | |
40 | // Operations | |
41 | // ------------------------------------------------------------------------ | |
42 | /** | |
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) | |
46 | */ | |
47 | public static void init(String filename, boolean append) { | |
48 | if (fTraceLog != null) { | |
49 | close(); | |
50 | } | |
51 | fTraceLog = openLogFile(filename, append); | |
52 | } | |
53 | ||
54 | /** | |
55 | * Closes the log file if open. | |
56 | */ | |
57 | public static void close() { | |
58 | if (fTraceLog == null) | |
59 | return; | |
60 | ||
61 | try { | |
62 | fTraceLog.close(); | |
63 | 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$ | |
67 | } | |
68 | } | |
69 | ||
70 | /** | |
71 | * Logs a message to the log file. | |
72 | * @param msg - message (e.g. command or command result) to log | |
73 | */ | |
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); | |
79 | message.append("."); | |
80 | message.append(String.format("%1$03d", currentTime % 1000)); | |
81 | message.append("] "); | |
82 | message.append(msg); | |
83 | if (fTraceLog != null) { | |
84 | try { | |
85 | fTraceLog.write(message.toString()); | |
86 | fTraceLog.newLine(); | |
87 | fTraceLog.flush(); | |
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$ | |
91 | } | |
92 | } | |
93 | } | |
94 | ||
95 | // ------------------------------------------------------------------------ | |
96 | // Helper methods | |
97 | // ------------------------------------------------------------------------ | |
98 | /** | |
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 | |
103 | */ | |
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; | |
109 | try { | |
110 | if (!file.exists()) { | |
111 | file.createNewFile(); | |
112 | file.setWritable(true, false); | |
113 | } | |
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$ | |
118 | } | |
119 | return outfile; | |
120 | } | |
121 | ||
122 | } |