Logger.java

1
/*
2
 * Copyright 2006 - 2013
3
 *     Stefan Balev     <stefan.balev@graphstream-project.org>
4
 *     Julien Baudry    <julien.baudry@graphstream-project.org>
5
 *     Antoine Dutot    <antoine.dutot@graphstream-project.org>
6
 *     Yoann Pign��      <yoann.pigne@graphstream-project.org>
7
 *     Guilhelm Savin   <guilhelm.savin@graphstream-project.org>
8
 * 
9
 * This file is part of GraphStream <http://graphstream-project.org>.
10
 * 
11
 * GraphStream is a library whose purpose is to handle static or dynamic
12
 * graph, create them from scratch, file or any source and display them.
13
 * 
14
 * This program is free software distributed under the terms of two licenses, the
15
 * CeCILL-C license that fits European law, and the GNU Lesser General Public
16
 * License. You can  use, modify and/ or redistribute the software under the terms
17
 * of the CeCILL-C license as circulated by CEA, CNRS and INRIA at the following
18
 * URL <http://www.cecill.info> or under the terms of the GNU LGPL as published by
19
 * the Free Software Foundation, either version 3 of the License, or (at your
20
 * option) any later version.
21
 * 
22
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
23
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
24
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
25
 * 
26
 * You should have received a copy of the GNU Lesser General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 * 
29
 * The fact that you are presently reading this means that you have had
30
 * knowledge of the CeCILL-C and LGPL licenses and that you accept their terms.
31
 */
32
package org.graphstream.util;
33
34
import java.io.BufferedWriter;
35
import java.io.FileWriter;
36
import java.io.IOException;
37
import java.io.PrintWriter;
38
39
/**
40
 * Logger.
41
 *
42
 * @author Fr��d��ric Guinand
43
 * @author Yoann Pign��
44
 * @author Antoine Dutot
45
 * @since  20061108
46
 */
47
public class Logger
48
{
49
// ---------- Constants --------------
50
51
	/**
52
	 * The different log levels.
53
	 * 
54
	 * It can be {@link LogLevel#DEBUG}, {@link LogLevel#INFO},
55
	 * {@link LogLevel#WARN} or {@link LogLevel#ERROR}. There is an order
56
	 * between the different log level: DEBUG < INFO < WARN < ERROR. So if you
57
	 * set the logLevel to DEBUG, then you will receive _all_ the messages, if
58
	 * you set it to INFO, then you will not receive the DEBUG messages. If you
59
	 * set it to WARN, you will not receive the INFO or DEBUG messages.
60
	 * And so on...
61
	 */
62
	public enum LogLevel
63
	{
64
		DEBUG(  "Debug",   0 ),
65
		INFO(   "Info",    1 ),
66
		WARN(   "Warning", 2 ),
67
		ERROR(  "Error",   3 ),
68
		RESULT( "Result",  4 );
69
		
70
		public String info;
71
		public int value;
72
		LogLevel( String info, int value ) { this.info = info; this.value = value; }
73 4 1. ge : changed conditional boundary → NO_COVERAGE
2. ge : negated conditional → NO_COVERAGE
3. ge : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
4. ge : replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE
		public boolean ge( LogLevel other ) { return value >= other.value; }
74
	};
75
	
76
	public static Logger GLOBAL_LOGGER;
77
	
78
// ------- Attributes ------
79
	
80
	/**
81
	 * Name of the log file. Default value is "stderr". It means that output is
82
	 * written on the standard error stream.
83
	 */
84
	protected String logFileName = "stderr";
85
86
	/**
87
	 * Has the logging file been opened yet?.
88
	 */
89
	protected boolean logFileOpened = false;
90
91
	/**
92
	 * Output stream.
93
	 */
94
	protected static PrintWriter out;
95
	
96
	/**
97
	 * The current log level;
98
	 */
99
	protected LogLevel logLevel = LogLevel.DEBUG;
100
101
// ------- Methods -------
102
103
	/**
104
	 * The method that every class of the package should use to send exception
105
	 * messages to the user.
106
	 * @param level The log level of the message.
107
	 * @param ref The name of the class calling this method.
108
	 * @param e The exception to log.
109
	 */
110
	public void
111
	log( LogLevel level, String ref, Exception e )
112
	{
113 1 1. log : negated conditional → NO_COVERAGE
		if( level.ge( logLevel ) )
114
		{
115
			try
116
			{
117 1 1. log : removed call to org/graphstream/util/Logger::openLogFile → NO_COVERAGE
				openLogFile();
118
119
				out.printf( "%-5s : %s : %s\n", logLevel.info, ref, e.toString() );
120
				out.printf( "The exception is in %s", java.lang.Thread.currentThread().toString() );
121 1 1. log : removed call to java/lang/Exception::printStackTrace → NO_COVERAGE
				e.printStackTrace( out );
122 1 1. log : removed call to java/io/PrintWriter::flush → NO_COVERAGE
				out.flush();
123
			}
124
			catch( IOException ioe )
125
			{
126
				System.err.printf( "%-5s : %s : %s\n", "ERROR", "Environment",
127
						ioe.toString() );
128 1 1. log : removed call to java/io/IOException::printStackTrace → NO_COVERAGE
				ioe.printStackTrace();
129 1 1. log : removed call to java/lang/System::exit → NO_COVERAGE
				System.exit( 0 );
130
			}
131
		}
132
	}
133
134
	/**
135
	 * The method that every class of the package should use to send messages to
136
	 * the user.
137
	 * @param level The log level of the message.
138
	 * @param ref The name of the class calling this method.
139
	 * @param message The message to log (can be in printf format).
140
	 * @param params The parameter of the message if in printf format.
141
	 */
142
	public void
143
	log( LogLevel level, String ref, String message, Object ... params )
144
	{
145 1 1. log : negated conditional → NO_COVERAGE
		if( level.ge( logLevel ) )
146
		{
147
			try
148
			{
149 1 1. log : removed call to org/graphstream/util/Logger::openLogFile → NO_COVERAGE
				openLogFile();
150
151
				out.printf( "%-5s : %s : ", level.info, ref );
152
				out.printf( message, params );
153
				out.printf( "%n" );
154 1 1. log : removed call to java/io/PrintWriter::flush → NO_COVERAGE
				out.flush();
155
			}
156
			catch( IOException ioe )
157
			{
158
				System.err.printf( "%-5s : %s : %s\n", "ERROR", "Environment",
159
						ioe.toString() );
160 1 1. log : removed call to java/io/IOException::printStackTrace → NO_COVERAGE
				ioe.printStackTrace();
161 1 1. log : removed call to java/lang/System::exit → NO_COVERAGE
				System.exit( 0 );
162
			}
163
		}
164
	}
165
	
166
	/**
167
	 * Verifies that the output log file is open, and if not open it. 
168
	 * @throws IOException For any error while openning the file.
169
	 */
170
	protected void
171
	openLogFile()
172
		throws IOException
173
	{
174 1 1. openLogFile : negated conditional → NO_COVERAGE
		if( ! logFileOpened )
175
		{
176 1 1. openLogFile : negated conditional → NO_COVERAGE
			if( logFileName.equals( "stderr" ) )
177
			{
178
				out = new PrintWriter( System.err );
179
			}
180
			else
181
			{
182
				out = new PrintWriter( new BufferedWriter(
183
						new FileWriter( logFileName ) ) );
184
			}
185
			
186
			logFileOpened = true;
187
		}
188
	}
189
	
190
	/**
191
	 * Change the log level.
192
	 * @param level The new log level.
193
	 */
194
	public void
195
	setLogLevel( LogLevel level )
196
	{
197
		logLevel = level;
198
	}
199
	
200
	/**
201
	 * Return the shared global instance of the logger. This singleton instance
202
	 * is avaiable in the whole JVM.
203
	 * @return The singleton global instance of the logger.
204
	 */
205
	public static Logger
206
	getGlobalLogger()
207
	{
208 1 1. getGlobalLogger : negated conditional → NO_COVERAGE
		if( GLOBAL_LOGGER == null )
209
			GLOBAL_LOGGER = new Logger();
210
		
211 1 1. getGlobalLogger : mutated return of Object value for org/graphstream/util/Logger::getGlobalLogger to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE
		return GLOBAL_LOGGER;
212
	}
213
}

Mutations

73

1.1
Location : ge
Killed by : none
changed conditional boundary → NO_COVERAGE

2.2
Location : ge
Killed by : none
negated conditional → NO_COVERAGE

3.3
Location : ge
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

4.4
Location : ge
Killed by : none
replaced return of integer sized value with (x == 0 ? 1 : 0) → NO_COVERAGE

113

1.1
Location : log
Killed by : none
negated conditional → NO_COVERAGE

117

1.1
Location : log
Killed by : none
removed call to org/graphstream/util/Logger::openLogFile → NO_COVERAGE

121

1.1
Location : log
Killed by : none
removed call to java/lang/Exception::printStackTrace → NO_COVERAGE

122

1.1
Location : log
Killed by : none
removed call to java/io/PrintWriter::flush → NO_COVERAGE

128

1.1
Location : log
Killed by : none
removed call to java/io/IOException::printStackTrace → NO_COVERAGE

129

1.1
Location : log
Killed by : none
removed call to java/lang/System::exit → NO_COVERAGE

145

1.1
Location : log
Killed by : none
negated conditional → NO_COVERAGE

149

1.1
Location : log
Killed by : none
removed call to org/graphstream/util/Logger::openLogFile → NO_COVERAGE

154

1.1
Location : log
Killed by : none
removed call to java/io/PrintWriter::flush → NO_COVERAGE

160

1.1
Location : log
Killed by : none
removed call to java/io/IOException::printStackTrace → NO_COVERAGE

161

1.1
Location : log
Killed by : none
removed call to java/lang/System::exit → NO_COVERAGE

174

1.1
Location : openLogFile
Killed by : none
negated conditional → NO_COVERAGE

176

1.1
Location : openLogFile
Killed by : none
negated conditional → NO_COVERAGE

208

1.1
Location : getGlobalLogger
Killed by : none
negated conditional → NO_COVERAGE

211

1.1
Location : getGlobalLogger
Killed by : none
mutated return of Object value for org/graphstream/util/Logger::getGlobalLogger to ( if (x != null) null else throw new RuntimeException ) → NO_COVERAGE

Active mutators

Tests examined


Report generated by PIT 0.33