Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7501751c391bd51e40ce957d24f0281ebd81c8b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*******************************************************************************
 *  Copyright (c) 2004, 2009 IBM Corporation and others.
 *
 *  This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *  IBM - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.make.internal.core.scannerconfig.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Date;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;

import com.ibm.icu.text.DateFormat;
import com.ibm.icu.text.SimpleDateFormat;

/**
 * Log writer utility
 *
 * @author vhirsl
 */
public class LogWriter {
	protected File logFile = null;
	protected Writer log = null;
	protected boolean newSession = true;

	protected static final String SESSION = "*** SESSION";//$NON-NLS-1$
	protected static final String ENTRY = "ENTRY";//$NON-NLS-1$
	protected static final String SUBENTRY = "SUBENTRY";//$NON-NLS-1$
	protected static final String MESSAGE = "MESSAGE";//$NON-NLS-1$
	protected static final String STACK = "STACK";//$NON-NLS-1$

	protected static final String LINE_SEPARATOR;
	protected static final String TAB_STRING = "\t";//$NON-NLS-1$
	protected static final long MAXLOG_SIZE = 10000000;
	static {
		String s = System.getProperty("line.separator");//$NON-NLS-1$
		LINE_SEPARATOR = s == null ? "\n" : s;//$NON-NLS-1$
	}

	/**
	 *
	 */
	public LogWriter(File log) {
		this.logFile = log;
		if (log.length() > MAXLOG_SIZE) {
			log.delete();
		}
		openLogFile();
	}

	protected void closeLogFile() throws IOException {
		try {
			if (log != null) {
				log.flush();
				log.close();
			}
		} finally {
			log = null;
		}
	}

	protected void openLogFile() {
		try {
			log = new BufferedWriter(
					new OutputStreamWriter(new FileOutputStream(logFile.getAbsolutePath(), true), "UTF-8"));//$NON-NLS-1$
			if (newSession) {
				writeHeader();
				newSession = false;
			}
		} catch (IOException e) {
			// there was a problem opening the log file so log to the console
			//log = logForStream(System.err);
		}
	}

	protected void writeHeader() throws IOException {
		writeln();
		write(SESSION);
		writeSpace();
		String date = getDate();
		write(date);
		writeSpace();
		for (int i = SESSION.length() + date.length(); i < 78; i++) {
			write("-");//$NON-NLS-1$
		}
		writeln();
	}

	protected String getDate() {
		try {
			DateFormat formatter = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss.SS"); //$NON-NLS-1$
			return formatter.format(new Date());
		} catch (Exception e) {
			// If there were problems writing out the date, ignore and
			// continue since that shouldn't stop us from losing the rest
			// of the information
		}
		return Long.toString(System.currentTimeMillis());
	}

	/**
	 * Writes the given string to the log, followed by the line terminator string.
	 */
	public void writeln(String s) throws IOException {
		write(s);
		writeln();
	}

	/**
	 * Shuts down the log.
	 */
	public synchronized void shutdown() {
		try {
			if (logFile != null) {
				closeLogFile();
				logFile = null;
			} else {
				if (log != null) {
					Writer old = log;
					log = null;
					old.flush();
					old.close();
				}
			}
		} catch (Exception e) {
			//we've shutdown the log, so not much else we can do!
			e.printStackTrace();
		}
	}

	protected void write(Throwable throwable) throws IOException {
		if (throwable == null)
			return;
		write(STACK);
		writeSpace();
		boolean isCoreException = throwable instanceof CoreException;
		if (isCoreException)
			writeln("1");//$NON-NLS-1$
		else
			writeln("0");//$NON-NLS-1$
		throwable.printStackTrace(new PrintWriter(log));
		if (isCoreException) {
			CoreException e = (CoreException) throwable;
			write(e.getStatus(), 0);
		}
	}

	public synchronized void log(IStatus status) {
		try {
			this.write(status, 0);
		} catch (IOException e) {
		}
	}

	protected void write(IStatus status, int depth) throws IOException {
		if (depth == 0) {
			write(ENTRY);
		} else {
			write(SUBENTRY);
			writeSpace();
			write(Integer.toString(depth));
		}
		writeSpace();
		write(status.getPlugin());
		writeSpace();
		write(Integer.toString(status.getSeverity()));
		writeSpace();
		write(Integer.toString(status.getCode()));
		writeSpace();
		write(getDate());
		writeln();

		write(MESSAGE);
		writeSpace();
		writeln(status.getMessage());

		//Took out the stack dump - too much space
		//write(status.getException());

		if (status.isMultiStatus()) {
			IStatus[] children = status.getChildren();
			for (int i = 0; i < children.length; i++) {
				write(children[i], depth + 1);
			}
		}
	}

	protected void writeln() throws IOException {
		write(LINE_SEPARATOR);
	}

	protected void write(String message) throws IOException {
		if (message != null)
			log.write(message);
	}

	protected void writeSpace() throws IOException {
		write(" ");//$NON-NLS-1$
	}

	public synchronized void flushLog() {
		try {
			log.flush();
		} catch (IOException e) {
		}
	}

}

Back to the top