Skip to main content
summaryrefslogtreecommitdiffstats
blob: a3ec68550cb4397594f74e9c8774e38713e594fb (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
/*
 * <copyright>
 *
 * Copyright (c) 2005-2006 Markus Voelter and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Markus Voelter - Initial API and implementation
 *
 * </copyright>
 */
package org.eclipse.m2t.common.recipe.ui;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Timestamp;

import org.eclipse.m2t.common.recipe.ui.shared.iface.ITraceLog;

public class TraceLog implements ITraceLog {
	private static PrintWriter pwLog = null;
	private static int cvLogLevel;
	private static String cvLogPath;
	/**
	 * returns the current time format: YYYY-MM-DD hh:mm:ss.fff (String)
	 */
	private final static String getTime() {
		// Variablen
		String time;
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		time = ts.toString();
		// ggf. 1/100 Sekunden auffuellen
		if (time.length() == 21) {
			time += "0";
		}
		// ggf. 1/1000 Sekunden auffuellen
		if (time.length() == 22) {
			time += "0";
		}
		return time;
	}
	/**
	 * initializes log
	 */
	private static void initLog() {
		if (cvLogPath == null)
			cvLogPath = DEFAULT_LOGPATH;
		try {
			pwLog = new PrintWriter(new FileOutputStream(cvLogPath, true));
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}
	}
	/**
	 * Writes a log
	 */
	private final static void writeLog(String aLog) {
		initLog();
		if (pwLog != null) {
			try {
				pwLog.write(aLog);
				pwLog.flush();
				pwLog.close();
				pwLog = null;
			} catch (Exception e) {
				System.err
						.println("TraceLog.log(): Fehler: could not write to log file "
								+ aLog);
				e.printStackTrace(System.err);
			}
		} else {
			System.err.println("TraceLog.log(): Fehler: Log nicht gestartet"
					+ aLog);
		}
	}
	/**
	 * Writes a log with a given level
	 */
	public final static void log(int aLevel, String aMessage) {
		if ((aLevel > (CRITICAL | ERROR | INFO | DEBUG | TRACE | EXCEPTION))
				|| (aLevel < 1)) {
			log(new Exception("TraceLog.log(): Error: invalid Level " + aLevel));
		} else {
			if ((cvLogLevel & aLevel) != QUIET) {
				StringBuffer log = new StringBuffer();
				String seperator = "-";
				// Zeitangabe
				log.append(getTime());
				log.append(' ');
				// loglevel CRITICAL
				if ((aLevel & CRITICAL) != 0) {
					log.append("CRI");
				} else {
					log.append(seperator);
				}
				// loglevel ERROR
				if ((aLevel & ERROR) != 0) {
					log.append("ERR");
				} else {
					log.append(seperator);
				}
				// loglevel INFO
				if ((aLevel & INFO) != 0) {
					log.append("INF");
				} else {
					log.append(seperator);
				}
				// loglevel DEBUG
				if ((aLevel & DEBUG) != 0) {
					log.append("DEB");
				} else {
					log.append(seperator);
				}
				// loglevel TRACE
				if ((aLevel & TRACE) != 0) {
					log.append("TRC");
				} else {
					log.append(seperator);
				}
				// loglevel EXCEPTION
				if ((aLevel & EXCEPTION) != 0) {
					log.append("EXC");
				} else {
					log.append(seperator);
				}
				log.append(" ");
				// Message
				log.append(aMessage);
				log.append("\n");
				writeLog(log.toString());
			}
		}
	}
	/**
	 * Writes a log with a implicite level "EXCEPTION"
	 */
	public final static void log(Exception anException) {
		StringWriter s = new StringWriter();
		anException.printStackTrace(new PrintWriter(s));
		log(TraceLog.EXCEPTION, s.toString());
	}
	/**
	 * Starts a new logging session with a given log level and a given log file
	 */
	public final static boolean startLogging(int aLevel, String aPath) {
		boolean result = true;
		cvLogLevel = aLevel;
		cvLogPath = aPath;
		if (aPath != null) {
			try {
				// Versuch eine bestehende Datei zu oeffnen
				pwLog = new PrintWriter(new FileOutputStream(aPath, true));
			} catch (IOException e) {
				e.printStackTrace(System.err);
				result = false;
			}
			log(INFO, "Session started");
		} else {
			pwLog = new PrintWriter(System.out);
		}
		return result;
	}
}

Back to the top