001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.util;
019:
020: import java.util.*;
021:
022: /**
023: * A logger interface that strives to make it as easy as possible for
024: * developers to write log calls, while simultaneously making those
025: * calls as cheap as possible by performing lazy evaluation of the log
026: * message.<p>
027: *
028: * @author Marc Johnson (mjohnson at apache dot org)
029: * @author Glen Stampoultzis (glens at apache.org)
030: * @author Nicola Ken Barozzi (nicolaken at apache.org)
031: */
032:
033: public abstract class POILogger {
034:
035: public static final int DEBUG = 1;
036: public static final int INFO = 3;
037: public static final int WARN = 5;
038: public static final int ERROR = 7;
039: public static final int FATAL = 9;
040:
041: /**
042: * package scope so it cannot be instantiated outside of the util
043: * package. You need a POILogger? Go to the POILogFactory for one
044: *
045: */
046: POILogger() {
047: }
048:
049: abstract public void initialize(final String cat);
050:
051: abstract public void log(final int level, final Object obj1);
052:
053: /**
054: * Check if a logger is enabled to log at the specified level
055: *
056: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
057: */
058: abstract public boolean check(final int level);
059:
060: /**
061: * Log a message. Lazily appends Object parameters together.
062: *
063: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
064: * @param obj1 first object to place in the message
065: * @param obj2 second object to place in the message
066: */
067:
068: /**
069: * Log a message. Lazily appends Object parameters together.
070: *
071: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
072: * @param obj1 first object to place in the message
073: * @param obj2 second object to place in the message
074: */
075:
076: public void log(final int level, final Object obj1,
077: final Object obj2) {
078: if (check(level)) {
079: log(level, new StringBuffer(32).append(obj1).append(obj2));
080: }
081: }
082:
083: /**
084: * Log a message. Lazily appends Object parameters together.
085: *
086: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
087: * @param obj1 first Object to place in the message
088: * @param obj2 second Object to place in the message
089: * @param obj3 third Object to place in the message
090: */
091:
092: public void log(final int level, final Object obj1,
093: final Object obj2, final Object obj3) {
094:
095: if (check(level)) {
096: log(level, new StringBuffer(48).append(obj1).append(obj2)
097: .append(obj3));
098: }
099: }
100:
101: /**
102: * Log a message. Lazily appends Object parameters together.
103: *
104: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
105: * @param obj1 first Object to place in the message
106: * @param obj2 second Object to place in the message
107: * @param obj3 third Object to place in the message
108: * @param obj4 fourth Object to place in the message
109: */
110:
111: public void log(final int level, final Object obj1,
112: final Object obj2, final Object obj3, final Object obj4) {
113:
114: if (check(level)) {
115: log(level, new StringBuffer(64).append(obj1).append(obj2)
116: .append(obj3).append(obj4));
117: }
118: }
119:
120: /**
121: * Log a message. Lazily appends Object parameters together.
122: *
123: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
124: * @param obj1 first Object to place in the message
125: * @param obj2 second Object to place in the message
126: * @param obj3 third Object to place in the message
127: * @param obj4 fourth Object to place in the message
128: * @param obj5 fifth Object to place in the message
129: */
130:
131: public void log(final int level, final Object obj1,
132: final Object obj2, final Object obj3, final Object obj4,
133: final Object obj5) {
134:
135: if (check(level)) {
136: log(level, new StringBuffer(80).append(obj1).append(obj2)
137: .append(obj3).append(obj4).append(obj5));
138: }
139: }
140:
141: /**
142: * Log a message. Lazily appends Object parameters together.
143: *
144: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
145: * @param obj1 first Object to place in the message
146: * @param obj2 second Object to place in the message
147: * @param obj3 third Object to place in the message
148: * @param obj4 fourth Object to place in the message
149: * @param obj5 fifth Object to place in the message
150: * @param obj6 sixth Object to place in the message
151: */
152:
153: public void log(final int level, final Object obj1,
154: final Object obj2, final Object obj3, final Object obj4,
155: final Object obj5, final Object obj6) {
156:
157: if (check(level)) {
158: log(level, new StringBuffer(96).append(obj1).append(obj2)
159: .append(obj3).append(obj4).append(obj5)
160: .append(obj6));
161: }
162: }
163:
164: /**
165: * Log a message. Lazily appends Object parameters together.
166: *
167: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
168: * @param obj1 first Object to place in the message
169: * @param obj2 second Object to place in the message
170: * @param obj3 third Object to place in the message
171: * @param obj4 fourth Object to place in the message
172: * @param obj5 fifth Object to place in the message
173: * @param obj6 sixth Object to place in the message
174: * @param obj7 seventh Object to place in the message
175: */
176:
177: public void log(final int level, final Object obj1,
178: final Object obj2, final Object obj3, final Object obj4,
179: final Object obj5, final Object obj6, final Object obj7) {
180:
181: if (check(level)) {
182: log(level, new StringBuffer(112).append(obj1).append(obj2)
183: .append(obj3).append(obj4).append(obj5)
184: .append(obj6).append(obj7));
185: }
186: }
187:
188: /**
189: * Log a message. Lazily appends Object parameters together.
190: *
191: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
192: * @param obj1 first Object to place in the message
193: * @param obj2 second Object to place in the message
194: * @param obj3 third Object to place in the message
195: * @param obj4 fourth Object to place in the message
196: * @param obj5 fifth Object to place in the message
197: * @param obj6 sixth Object to place in the message
198: * @param obj7 seventh Object to place in the message
199: * @param obj8 eighth Object to place in the message
200: */
201:
202: public void log(final int level, final Object obj1,
203: final Object obj2, final Object obj3, final Object obj4,
204: final Object obj5, final Object obj6, final Object obj7,
205: final Object obj8) {
206:
207: if (check(level)) {
208: log(level, new StringBuffer(128).append(obj1).append(obj2)
209: .append(obj3).append(obj4).append(obj5)
210: .append(obj6).append(obj7).append(obj8));
211: }
212: }
213:
214: /**
215: * Log a message
216: *
217: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
218: * @param obj1 The object to log. This is converted to a string.
219: * @param exception An exception to be logged
220: */
221:
222: public void log(final int level, final Object obj1,
223: final Throwable exception) {
224: log(level, obj1, exception);
225: }
226:
227: /**
228: * Log a message. Lazily appends Object parameters together.
229: *
230: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
231: * @param obj1 first Object to place in the message
232: * @param obj2 second Object to place in the message
233: * @param exception An exception to be logged
234: */
235:
236: public void log(final int level, final Object obj1,
237: final Object obj2, final Throwable exception) {
238:
239: if (check(level)) {
240: log(level, new StringBuffer(32).append(obj1).append(obj2),
241: exception);
242: }
243: }
244:
245: /**
246: * Log a message. Lazily appends Object parameters together.
247: *
248: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
249: * @param obj1 first Object to place in the message
250: * @param obj2 second Object to place in the message
251: * @param obj3 third object to place in the message
252: * @param exception An error message to be logged
253: */
254:
255: public void log(final int level, final Object obj1,
256: final Object obj2, final Object obj3,
257: final Throwable exception) {
258:
259: if (check(level)) {
260: log(level, new StringBuffer(48).append(obj1).append(obj2)
261: .append(obj3), exception);
262: }
263: }
264:
265: /**
266: * Log a message. Lazily appends Object parameters together.
267: *
268: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
269: * @param obj1 first Object to place in the message
270: * @param obj2 second Object to place in the message
271: * @param obj3 third object to place in the message
272: * @param obj4 fourth object to place in the message
273: * @param exception An exception to be logged
274: */
275:
276: public void log(final int level, final Object obj1,
277: final Object obj2, final Object obj3, final Object obj4,
278: final Throwable exception) {
279:
280: if (check(level)) {
281: log(level, new StringBuffer(64).append(obj1).append(obj2)
282: .append(obj3).append(obj4), exception);
283: }
284: }
285:
286: /**
287: * Log a message. Lazily appends Object parameters together.
288: *
289: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
290: * @param obj1 first Object to place in the message
291: * @param obj2 second Object to place in the message
292: * @param obj3 third object to place in the message
293: * @param obj4 fourth object to place in the message
294: * @param obj5 fifth object to place in the message
295: * @param exception An exception to be logged
296: */
297:
298: public void log(final int level, final Object obj1,
299: final Object obj2, final Object obj3, final Object obj4,
300: final Object obj5, final Throwable exception) {
301:
302: if (check(level)) {
303: log(level, new StringBuffer(80).append(obj1).append(obj2)
304: .append(obj3).append(obj4).append(obj5), exception);
305: }
306: }
307:
308: /**
309: * Log a message. Lazily appends Object parameters together.
310: *
311: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
312: * @param obj1 first Object to place in the message
313: * @param obj2 second Object to place in the message
314: * @param obj3 third object to place in the message
315: * @param obj4 fourth object to place in the message
316: * @param obj5 fifth object to place in the message
317: * @param obj6 sixth object to place in the message
318: * @param exception An exception to be logged
319: */
320:
321: public void log(final int level, final Object obj1,
322: final Object obj2, final Object obj3, final Object obj4,
323: final Object obj5, final Object obj6,
324: final Throwable exception) {
325:
326: if (check(level)) {
327: log(level, new StringBuffer(96).append(obj1).append(obj2)
328: .append(obj3).append(obj4).append(obj5)
329: .append(obj6), exception);
330: }
331: }
332:
333: /**
334: * Log a message. Lazily appends Object parameters together.
335: *
336: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
337: * @param obj1 first Object to place in the message
338: * @param obj2 second Object to place in the message
339: * @param obj3 third object to place in the message
340: * @param obj4 fourth object to place in the message
341: * @param obj5 fifth object to place in the message
342: * @param obj6 sixth object to place in the message
343: * @param obj7 seventh object to place in the message
344: * @param exception An exception to be logged
345: */
346:
347: public void log(final int level, final Object obj1,
348: final Object obj2, final Object obj3, final Object obj4,
349: final Object obj5, final Object obj6, final Object obj7,
350: final Throwable exception) {
351:
352: if (check(level)) {
353: log(level, new StringBuffer(112).append(obj1).append(obj2)
354: .append(obj3).append(obj4).append(obj5)
355: .append(obj6).append(obj7), exception);
356: }
357: }
358:
359: /**
360: * Log a message. Lazily appends Object parameters together.
361: *
362: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
363: * @param obj1 first Object to place in the message
364: * @param obj2 second Object to place in the message
365: * @param obj3 third object to place in the message
366: * @param obj4 fourth object to place in the message
367: * @param obj5 fifth object to place in the message
368: * @param obj6 sixth object to place in the message
369: * @param obj7 seventh object to place in the message
370: * @param obj8 eighth object to place in the message
371: * @param exception An exception to be logged
372: */
373:
374: public void log(final int level, final Object obj1,
375: final Object obj2, final Object obj3, final Object obj4,
376: final Object obj5, final Object obj6, final Object obj7,
377: final Object obj8, final Throwable exception) {
378:
379: if (check(level)) {
380: log(level, new StringBuffer(128).append(obj1).append(obj2)
381: .append(obj3).append(obj4).append(obj5)
382: .append(obj6).append(obj7).append(obj8), exception);
383: }
384: }
385:
386: /**
387: * Logs a formated message. The message itself may contain %
388: * characters as place holders. This routine will attempt to match
389: * the placeholder by looking at the type of parameter passed to
390: * obj1.<p>
391: *
392: * If the parameter is an array, it traverses the array first and
393: * matches parameters sequentially against the array items.
394: * Otherwise the parameters after <code>message</code> are matched
395: * in order.<p>
396: *
397: * If the place holder matches against a number it is printed as a
398: * whole number. This can be overridden by specifying a precision
399: * in the form %n.m where n is the padding for the whole part and
400: * m is the number of decimal places to display. n can be excluded
401: * if desired. n and m may not be more than 9.<p>
402: *
403: * If the last parameter (after flattening) is a Throwable it is
404: * logged specially.
405: *
406: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
407: * @param message The message to log.
408: * @param obj1 The first object to match against.
409: */
410:
411: public void logFormatted(final int level, final String message,
412: final Object obj1) {
413: commonLogFormatted(level, message, new Object[] { obj1 });
414: }
415:
416: /**
417: * Logs a formated message. The message itself may contain %
418: * characters as place holders. This routine will attempt to match
419: * the placeholder by looking at the type of parameter passed to
420: * obj1.<p>
421: *
422: * If the parameter is an array, it traverses the array first and
423: * matches parameters sequentially against the array items.
424: * Otherwise the parameters after <code>message</code> are matched
425: * in order.<p>
426: *
427: * If the place holder matches against a number it is printed as a
428: * whole number. This can be overridden by specifying a precision
429: * in the form %n.m where n is the padding for the whole part and
430: * m is the number of decimal places to display. n can be excluded
431: * if desired. n and m may not be more than 9.<p>
432: *
433: * If the last parameter (after flattening) is a Throwable it is
434: * logged specially.
435: *
436: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
437: * @param message The message to log.
438: * @param obj1 The first object to match against.
439: * @param obj2 The second object to match against.
440: */
441:
442: public void logFormatted(final int level, final String message,
443: final Object obj1, final Object obj2) {
444: commonLogFormatted(level, message, new Object[] { obj1, obj2 });
445: }
446:
447: /**
448: * Logs a formated message. The message itself may contain %
449: * characters as place holders. This routine will attempt to match
450: * the placeholder by looking at the type of parameter passed to
451: * obj1.<p>
452: *
453: * If the parameter is an array, it traverses the array first and
454: * matches parameters sequentially against the array items.
455: * Otherwise the parameters after <code>message</code> are matched
456: * in order.<p>
457: *
458: * If the place holder matches against a number it is printed as a
459: * whole number. This can be overridden by specifying a precision
460: * in the form %n.m where n is the padding for the whole part and
461: * m is the number of decimal places to display. n can be excluded
462: * if desired. n and m may not be more than 9.<p>
463: *
464: * If the last parameter (after flattening) is a Throwable it is
465: * logged specially.
466: *
467: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
468: * @param message The message to log.
469: * @param obj1 The first object to match against.
470: * @param obj2 The second object to match against.
471: * @param obj3 The third object to match against.
472: */
473:
474: public void logFormatted(final int level, final String message,
475: final Object obj1, final Object obj2, final Object obj3) {
476: commonLogFormatted(level, message, new Object[] { obj1, obj2,
477: obj3 });
478: }
479:
480: /**
481: * Logs a formated message. The message itself may contain %
482: * characters as place holders. This routine will attempt to match
483: * the placeholder by looking at the type of parameter passed to
484: * obj1.<p>
485: *
486: * If the parameter is an array, it traverses the array first and
487: * matches parameters sequentially against the array items.
488: * Otherwise the parameters after <code>message</code> are matched
489: * in order.<p>
490: *
491: * If the place holder matches against a number it is printed as a
492: * whole number. This can be overridden by specifying a precision
493: * in the form %n.m where n is the padding for the whole part and
494: * m is the number of decimal places to display. n can be excluded
495: * if desired. n and m may not be more than 9.<p>
496: *
497: * If the last parameter (after flattening) is a Throwable it is
498: * logged specially.
499: *
500: * @param level One of DEBUG, INFO, WARN, ERROR, FATAL
501: * @param message The message to log.
502: * @param obj1 The first object to match against.
503: * @param obj2 The second object to match against.
504: * @param obj3 The third object to match against.
505: * @param obj4 The forth object to match against.
506: */
507:
508: public void logFormatted(final int level, final String message,
509: final Object obj1, final Object obj2, final Object obj3,
510: final Object obj4) {
511: commonLogFormatted(level, message, new Object[] { obj1, obj2,
512: obj3, obj4 });
513: }
514:
515: private void commonLogFormatted(final int level,
516: final String message, final Object[] unflatParams) {
517:
518: if (check(level)) {
519: Object[] params = flattenArrays(unflatParams);
520:
521: if (params[params.length - 1] instanceof Throwable) {
522: log(level, StringUtil.format(message, params),
523: (Throwable) params[params.length - 1]);
524: } else {
525: log(level, StringUtil.format(message, params));
526: }
527: }
528: }
529:
530: /**
531: * Flattens any contained objects. Only tranverses one level deep.
532: */
533:
534: private Object[] flattenArrays(final Object[] objects) {
535: List results = new ArrayList();
536:
537: for (int i = 0; i < objects.length; i++) {
538: results.addAll(objectToObjectArray(objects[i]));
539: }
540: return (Object[]) results.toArray(new Object[results.size()]);
541: }
542:
543: private List objectToObjectArray(Object object) {
544: List results = new ArrayList();
545:
546: if (object instanceof byte[]) {
547: byte[] array = (byte[]) object;
548:
549: for (int j = 0; j < array.length; j++) {
550: results.add(new Byte(array[j]));
551: }
552: }
553: if (object instanceof char[]) {
554: char[] array = (char[]) object;
555:
556: for (int j = 0; j < array.length; j++) {
557: results.add(new Character(array[j]));
558: }
559: } else if (object instanceof short[]) {
560: short[] array = (short[]) object;
561:
562: for (int j = 0; j < array.length; j++) {
563: results.add(new Short(array[j]));
564: }
565: } else if (object instanceof int[]) {
566: int[] array = (int[]) object;
567:
568: for (int j = 0; j < array.length; j++) {
569: results.add(new Integer(array[j]));
570: }
571: } else if (object instanceof long[]) {
572: long[] array = (long[]) object;
573:
574: for (int j = 0; j < array.length; j++) {
575: results.add(new Long(array[j]));
576: }
577: } else if (object instanceof float[]) {
578: float[] array = (float[]) object;
579:
580: for (int j = 0; j < array.length; j++) {
581: results.add(new Float(array[j]));
582: }
583: } else if (object instanceof double[]) {
584: double[] array = (double[]) object;
585:
586: for (int j = 0; j < array.length; j++) {
587: results.add(new Double(array[j]));
588: }
589: } else if (object instanceof Object[]) {
590: Object[] array = (Object[]) object;
591:
592: for (int j = 0; j < array.length; j++) {
593: results.add(array[j]);
594: }
595: } else {
596: results.add(object);
597: }
598: return results;
599: }
600:
601: } // end package scope abstract class POILogger
|