001// Copyright 2004, 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.hivemind.service.impl;
016
017import org.apache.commons.logging.Log;
018import org.apache.hivemind.service.ClassFabUtils;
019
020/**
021 * Collection of static methods used by loggers to
022 * log method entry and exit.
023 *
024 * @author Howard Lewis Ship
025 */
026public class LoggingUtils
027{
028    private static final int BUFFER_SIZE = 100;
029
030    public static void entry(Log log, String methodName, Object[] args)
031    {
032        StringBuffer buffer = new StringBuffer(BUFFER_SIZE);
033
034        buffer.append("BEGIN ");
035        buffer.append(methodName);
036        buffer.append("(");
037
038        int count = (args == null) ? 0 : args.length;
039
040        for (int i = 0; i < count; i++)
041        {
042            Object arg = args[i];
043
044            if (i > 0)
045                buffer.append(", ");
046
047            convert(buffer, arg);
048        }
049
050        buffer.append(")");
051
052        log.debug(buffer.toString());
053    }
054
055    public static void exit(Log log, String methodName, Object result)
056    {
057        StringBuffer buffer = new StringBuffer(BUFFER_SIZE);
058
059        buffer.append("END ");
060        buffer.append(methodName);
061        buffer.append("() [");
062
063        convert(buffer, result);
064
065        buffer.append("]");
066
067        log.debug(buffer.toString());
068    }
069
070    public static void voidExit(Log log, String methodName)
071    {
072        StringBuffer buffer = new StringBuffer(BUFFER_SIZE);
073
074        buffer.append("END ");
075        buffer.append(methodName);
076        buffer.append("()");
077
078        log.debug(buffer.toString());
079    }
080
081    public static void exception(Log log, String methodName, Throwable t)
082    {
083        StringBuffer buffer = new StringBuffer(BUFFER_SIZE);
084
085        buffer.append("EXCEPTION ");
086        buffer.append(methodName);
087        buffer.append("() -- ");
088
089        buffer.append(t.getClass().getName());
090
091        log.debug(buffer.toString(), t);
092    }
093
094    public static void convert(StringBuffer buffer, Object input)
095    {
096        if (input == null)
097        {
098            buffer.append("<null>");
099            return;
100        }
101
102        // Primitive types, and non-object arrays
103        // use toString().  Less than ideal for int[], etc., but
104        // that's a lot of work for a rare case.
105
106        if (!(input instanceof Object[]))
107        {
108            buffer.append(input.toString());
109            return;
110        }
111
112        buffer.append("(");
113        buffer.append(ClassFabUtils.getJavaClassName(input.getClass()));
114        buffer.append("){");
115
116        Object[] array = (Object[]) input;
117        int count = array.length;
118
119        for (int i = 0; i < count; i++)
120        {
121            if (i > 0)
122                buffer.append(", ");
123
124            // We use convert() again, because it could be a multi-dimensional array
125            // (god help us) where each element must be converted.
126            convert(buffer, array[i]);
127        }
128
129        buffer.append("}");
130    }
131}