From 67d0140701f4b83c73bdd79b8a2d64d30a39705e Mon Sep 17 00:00:00 2001 From: samskivert Date: Sat, 25 Oct 2008 00:52:40 +0000 Subject: [PATCH] (Heavily modified) patch from Dave to allow varargs key/value logging bits to be supplied to ComplainingListener. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2462 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../samskivert/util/ComplainingListener.java | 45 ++++++++++++++++--- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/src/java/com/samskivert/util/ComplainingListener.java b/src/java/com/samskivert/util/ComplainingListener.java index 0f0fe6ab..a0378346 100644 --- a/src/java/com/samskivert/util/ComplainingListener.java +++ b/src/java/com/samskivert/util/ComplainingListener.java @@ -28,16 +28,32 @@ import java.util.logging.Level; public class ComplainingListener implements ResultListener { - public ComplainingListener (Logger logger, String errorText) + /** + * Creates a listener that will log failures to the supplied logger. + * + * @param logger the logger to which to log failures. + * @param errorText the text to log when the error is received. + * @param arguments to log along with the error text. See {@link Logger#format} for info on how + * they will be formatted. + */ + public ComplainingListener (Logger logger, String errorText, Object... args) { + this(errorText, args); _slogger = logger; - _errorText = errorText; } - public ComplainingListener (java.util.logging.Logger logger, String errorText) + /** + * Creates a listener that will log failures to the supplied logger. + * + * @param logger the logger to which to log failures. + * @param errorText the text to log when the error is received. + * @param arguments to log along with the error text. See {@link Logger#format} for info on how + * they will be formatted. + */ + public ComplainingListener (java.util.logging.Logger logger, String errorText, Object... args) { + this(errorText, args); _jlogger = logger; - _errorText = errorText; } // documentation inherited from interface ResultListener @@ -46,15 +62,27 @@ public class ComplainingListener // documentation inherited from interface ResultListener public void requestFailed (Exception cause) { + Object[] args = _args != null ? ArrayUtil.append(_args, cause) : new Object[] { cause }; if (_slogger != null) { - _slogger.warning(_errorText, cause); + _slogger.warning(_errorText, args); } else if (_jlogger != null) { - _jlogger.log(Level.WARNING, _errorText, cause); + _jlogger.log(Level.WARNING, Logger.format(_errorText, args), cause); } else { - System.err.println(_errorText + " [cause=" + cause + "]."); + System.err.println(Logger.format(_errorText, args)); } } + protected ComplainingListener (String errorText, Object[] args) + { + if (args != null && args.length % 2 == 1) { + throw new IllegalArgumentException( + "Got odd number of arguments (" + args.length + "). " + + "args must be a list of key/value pairs."); + } + _errorText = errorText; + _args = args; + } + /** The log to which we'll log our error, may be null. */ protected Logger _slogger; @@ -63,4 +91,7 @@ public class ComplainingListener /** The text to output if the error happens. */ protected String _errorText; + + /** Key-value pairs for extra information about the error. */ + protected Object[] _args; }