Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 85b506eec87c14b2229cfca25a4a4ab0651f53bf (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
/*******************************************************************************
* Copyright (c) 2011 protos software gmbh (http://www.protos.de).
* All rights reserved.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* CONTRIBUTORS:
*           Thomas Schuetz and Henrik Rentz-Reichert (initial contribution)
*
 *******************************************************************************/

package org.eclipse.etrice.generator.base.logging;

import org.eclipse.etrice.generator.base.io.ILineOutput;
import org.eclipse.etrice.generator.base.io.LineOutput;

/**
 * A simple logger class implementing the
 * {@link ILineOutputLogger ILineOutputLogger}
 * interface. If no ILineOutputLogger is set then output is sent to
 * {@link java.lang.System#out System.out}.
 * 
 * @author Henrik Rentz-Reichert
 * 
 */
public class Logger implements ILineOutputLogger, ILineOutput {

	private static final String DEBUG_PREFIX =		"[DEBUG]   ";
	private static final String INFO_PREFIX =		"[INFO]    ";
	private static final String WARNING_PREFIX =	"[WARNING] ";
	private static final String ERROR_PREFIX =		"[ERROR]   ";
	
	
	private Loglevel loglevel;
	private ILineOutput output;
	
	public Logger() {
		this(Loglevel.WARNING, new LineOutput());
	}
	
	public Logger(Loglevel loglevel) {
		this(loglevel, new LineOutput());
	}
	
	public Logger(ILineOutput out) {
		this(Loglevel.WARNING, out);
	}
	
	public Logger(Loglevel loglevel, ILineOutput out) {
		setLoglevel(loglevel);
		setOutput(out);
	}
	
	@Override
	public void logDebug(String text) {
		if(Loglevel.DEBUG.compareTo(loglevel) >= 0) {
			println(DEBUG_PREFIX + text);
		}
	}
	
	@Override
	public void logInfo(String text) {
		if(Loglevel.INFO.compareTo(loglevel) >= 0) {
			println(INFO_PREFIX + text);
		}
	}
	
	@Override
	public void logWarning(String text) {
		if(Loglevel.WARNING.compareTo(loglevel) >= 0) {
			println(WARNING_PREFIX + text);
		}
	}

	@Override
	public void logError(String text) {
		if(Loglevel.ERROR.compareTo(loglevel) >= 0) {
			println(ERROR_PREFIX + text);
		}
	}

	@Override
	public Loglevel getLoglevel() {
		return loglevel;
	}
	
	@Override
	public void setLoglevel(Loglevel loglevel) {
		this.loglevel = loglevel;
	}
	
	@Override
	public void setOutput(ILineOutput out) {
		output = out;
	}

	@Override
	public void println(String txt) {
		output.println(txt);
	}
	
}

Back to the top