From 62a87561ea46f799780e44a8553ccedea7c0659a Mon Sep 17 00:00:00 2001 From: samskivert Date: Fri, 12 Jun 2009 21:29:20 +0000 Subject: [PATCH] Patch from Charlie to extract Logger.format into LogBuilder class. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2576 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/LogBuilder.java | 90 ++++++++++++++++++++ src/java/com/samskivert/util/Logger.java | 19 +---- 2 files changed, 91 insertions(+), 18 deletions(-) create mode 100644 src/java/com/samskivert/util/LogBuilder.java diff --git a/src/java/com/samskivert/util/LogBuilder.java b/src/java/com/samskivert/util/LogBuilder.java new file mode 100644 index 00000000..aeee2e6f --- /dev/null +++ b/src/java/com/samskivert/util/LogBuilder.java @@ -0,0 +1,90 @@ +// +// $Id$ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001-2009 Michael Bayne +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.util; + +/** + * Formats a message and an array of alternating key value pairs like so: + * + *
message [key=value, key=value, key=value]
+ */ +public class LogBuilder +{ + /** + * Creates a log builder with no message and no initial key value pairs. + */ + public LogBuilder () + { + this(""); + } + + /** + * Creates a log builder with the given message and key value pairs. + */ + public LogBuilder (Object message, Object... args) + { + _log = new StringBuilder().append(message); + append(args); + } + + /** + * Adds the given key value pairs to the log. + */ + public LogBuilder append (Object... args) + { + if (args != null && args.length > 1) { + for (int ii = 0, nn = args.length / 2; ii < nn; ii++) { + if (_hasArgs) { + _log.append(", "); + } else { + if (_log.length() > 0) { + // only need a space if we have a message + _log.append(' '); + } + _log.append('['); + _hasArgs = true; + } + _log.append(args[2 * ii]).append("="); + try { + StringUtil.toString(_log, args[2 * ii + 1]); + } catch (Throwable t) { + _log.append(""); + } + } + } + return this; + } + + /** + * Returns the formatted log message. Does not reset the buffer. You can continue to append + * arguments and call {@link #toString} again. + */ + @Override public String toString () + { + String log = _log.toString(); + if (_hasArgs) { + log += "]"; + } + return log; + } + + protected boolean _hasArgs; + protected StringBuilder _log; +} diff --git a/src/java/com/samskivert/util/Logger.java b/src/java/com/samskivert/util/Logger.java index 404215bd..248f0f61 100644 --- a/src/java/com/samskivert/util/Logger.java +++ b/src/java/com/samskivert/util/Logger.java @@ -85,24 +85,7 @@ public abstract class Logger */ public static String format (Object message, Object... args) { - StringBuilder buf = new StringBuilder(); - buf.append(message); - if (args != null && args.length > 1) { - buf.append(" ["); - for (int ii = 0, nn = args.length/2; ii < nn; ii++) { - if (ii > 0) { - buf.append(", "); - } - buf.append(args[2*ii]).append("="); - try { - StringUtil.toString(buf, args[2*ii+1]); - } catch (Throwable t) { - buf.append(""); - } - } - buf.append("]"); - } - return buf.toString(); + return new LogBuilder(message, args).toString(); } /**