1 /*******************************************************************************
2  * Copyright (c) 2009, 2010 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.e4.core.services.log;
15 
16 import org.eclipse.e4.core.services.statusreporter.StatusReporter;
17 
18 /**
19  * Logging warnings, errors, information, as well as capturing debug and trace information.
20  * Everything done through this interface is not meant for normal end users. Strings are not
21  * expected to be translated.
22  *
23  * @see StatusReporter
24  */
25 public abstract class Logger {
isErrorEnabled()26 	public abstract boolean isErrorEnabled();
27 
error(Throwable t, String message)28 	public abstract void error(Throwable t, String message);
29 
isWarnEnabled()30 	public abstract boolean isWarnEnabled();
31 
warn(Throwable t, String message)32 	public abstract void warn(Throwable t, String message);
33 
isInfoEnabled()34 	public abstract boolean isInfoEnabled();
35 
info(Throwable t, String message)36 	public abstract void info(Throwable t, String message);
37 
isTraceEnabled()38 	public abstract boolean isTraceEnabled();
39 
trace(Throwable t, String message)40 	public abstract void trace(Throwable t, String message);
41 
isDebugEnabled()42 	public abstract boolean isDebugEnabled();
43 
debug(Throwable t)44 	public abstract void debug(Throwable t);
45 
debug(Throwable t, String message)46 	public abstract void debug(Throwable t, String message);
47 
debug(String message)48 	public void debug(String message) {
49 		debug((Throwable) null, message);
50 	}
51 
debug(String format, Object arg)52 	public void debug(String format, Object arg) {
53 		debug(internalBind(format, null, String.valueOf(arg), null));
54 	}
55 
debug(String format, Object arg1, Object arg2)56 	public void debug(String format, Object arg1, Object arg2) {
57 		debug(internalBind(format, null, String.valueOf(arg1), String.valueOf(arg2)));
58 	}
59 
debug(String format, Object[] args)60 	public void debug(String format, Object[] args) {
61 		debug(internalBind(format, args, null, null));
62 	}
63 
error(Throwable t)64 	public void error(Throwable t) {
65 		error(t, null);
66 	}
67 
error(String message)68 	public void error(String message) {
69 		error((Throwable) null, message);
70 	}
71 
error(String format, Object arg)72 	public void error(String format, Object arg) {
73 		error(internalBind(format, null, String.valueOf(arg), null));
74 	}
75 
error(String format, Object arg1, Object arg2)76 	public void error(String format, Object arg1, Object arg2) {
77 		error(internalBind(format, null, String.valueOf(arg1), String.valueOf(arg2)));
78 	}
79 
error(String format, Object[] args)80 	public void error(String format, Object[] args) {
81 		error(internalBind(format, args, null, null));
82 	}
83 
info(Throwable t)84 	public void info(Throwable t) {
85 		info(t, null);
86 	}
87 
info(String message)88 	public void info(String message) {
89 		info((Throwable) null, message);
90 	}
91 
info(String format, Object arg)92 	public void info(String format, Object arg) {
93 		info(internalBind(format, null, String.valueOf(arg), null));
94 	}
95 
info(String format, Object arg1, Object arg2)96 	public void info(String format, Object arg1, Object arg2) {
97 		info(internalBind(format, null, String.valueOf(arg1), String.valueOf(arg2)));
98 	}
99 
info(String format, Object[] args)100 	public void info(String format, Object[] args) {
101 		info(internalBind(format, args, null, null));
102 	}
103 
trace(Throwable t)104 	public void trace(Throwable t) {
105 		trace(t, null);
106 	}
107 
trace(String message)108 	public void trace(String message) {
109 		trace((Throwable) null, message);
110 	}
111 
trace(String format, Object arg)112 	public void trace(String format, Object arg) {
113 		trace(internalBind(format, null, String.valueOf(arg), null));
114 	}
115 
trace(String format, Object arg1, Object arg2)116 	public void trace(String format, Object arg1, Object arg2) {
117 		trace(internalBind(format, null, String.valueOf(arg1), String.valueOf(arg2)));
118 	}
119 
trace(String format, Object[] args)120 	public void trace(String format, Object[] args) {
121 		trace(internalBind(format, args, null, null));
122 	}
123 
warn(Throwable t)124 	public void warn(Throwable t) {
125 		warn(t, null);
126 	}
127 
warn(String message)128 	public void warn(String message) {
129 		warn((Throwable) null, message);
130 	}
131 
warn(String format, Object arg)132 	public void warn(String format, Object arg) {
133 		warn(internalBind(format, null, String.valueOf(arg), null));
134 	}
135 
warn(String format, Object arg1, Object arg2)136 	public void warn(String format, Object arg1, Object arg2) {
137 		warn(internalBind(format, null, String.valueOf(arg1), String.valueOf(arg2)));
138 	}
139 
warn(String format, Object[] args)140 	public void warn(String format, Object[] args) {
141 		warn(internalBind(format, args, null, null));
142 	}
143 
144 	private static final Object[] EMPTY_ARGS = new Object[0];
145 
146 	/*
147 	 * Perform the string substitution on the given message with the specified args. See the class
148 	 * comment for exact details.
149 	 */
internalBind(String message, Object[] args, String argZero, String argOne)150 	private static String internalBind(String message, Object[] args, String argZero, String argOne) {
151 		if (message == null)
152 			return "No message available."; //$NON-NLS-1$
153 		if (args == null || args.length == 0)
154 			args = EMPTY_ARGS;
155 
156 		int length = message.length();
157 		// estimate correct size of string buffer to avoid growth
158 		int bufLen = length + (args.length * 5);
159 		if (argZero != null)
160 			bufLen += argZero.length() - 3;
161 		if (argOne != null)
162 			bufLen += argOne.length() - 3;
163 		StringBuilder buffer = new StringBuilder(bufLen < 0 ? 0 : bufLen);
164 		for (int i = 0; i < length; i++) {
165 			char c = message.charAt(i);
166 			switch (c) {
167 			case '{':
168 				int index = message.indexOf('}', i);
169 				// if we don't have a matching closing brace then...
170 				if (index == -1) {
171 					buffer.append(c);
172 					break;
173 				}
174 				i++;
175 				if (i >= length) {
176 					buffer.append(c);
177 					break;
178 				}
179 				// look for a substitution
180 				int number = -1;
181 				try {
182 					number = Integer.parseInt(message.substring(i, index));
183 				} catch (NumberFormatException e) {
184 					throw new IllegalArgumentException();
185 				}
186 				if (number == 0 && argZero != null)
187 					buffer.append(argZero);
188 				else if (number == 1 && argOne != null)
189 					buffer.append(argOne);
190 				else {
191 					if (number >= args.length || number < 0) {
192 						buffer.append("<missing argument>"); //$NON-NLS-1$
193 						i = index;
194 						break;
195 					}
196 					buffer.append(args[number]);
197 				}
198 				i = index;
199 				break;
200 			case '\'':
201 				// if a single quote is the last char on the line then skip it
202 				int nextIndex = i + 1;
203 				if (nextIndex >= length) {
204 					buffer.append(c);
205 					break;
206 				}
207 				char next = message.charAt(nextIndex);
208 				// if the next char is another single quote then write out one
209 				if (next == '\'') {
210 					i++;
211 					buffer.append(c);
212 					break;
213 				}
214 				// otherwise we want to read until we get to the next single
215 				// quote
216 				index = message.indexOf('\'', nextIndex);
217 				// if there are no more in the string, then skip it
218 				if (index == -1) {
219 					buffer.append(c);
220 					break;
221 				}
222 				// otherwise write out the chars inside the quotes
223 				buffer.append(message.substring(nextIndex, index));
224 				i = index;
225 				break;
226 			default:
227 				buffer.append(c);
228 			}
229 		}
230 		return buffer.toString();
231 	}
232 }
233