KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > impl > LoggingUtils


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.service.impl;
16
17 import org.apache.commons.logging.Log;
18 import org.apache.hivemind.service.ClassFabUtils;
19
20 /**
21  * Collection of static methods used by loggers to
22  * log method entry and exit.
23  *
24  * @author Howard Lewis Ship
25  */

26 public class LoggingUtils
27 {
28     private static final int BUFFER_SIZE = 100;
29
30     public static void entry(Log log, String JavaDoc methodName, Object JavaDoc[] args)
31     {
32         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(BUFFER_SIZE);
33
34         buffer.append("BEGIN ");
35         buffer.append(methodName);
36         buffer.append("(");
37
38         int count = (args == null) ? 0 : args.length;
39
40         for (int i = 0; i < count; i++)
41         {
42             Object JavaDoc arg = args[i];
43
44             if (i > 0)
45                 buffer.append(", ");
46
47             convert(buffer, arg);
48         }
49
50         buffer.append(")");
51
52         log.debug(buffer.toString());
53     }
54
55     public static void exit(Log log, String JavaDoc methodName, Object JavaDoc result)
56     {
57         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(BUFFER_SIZE);
58
59         buffer.append("END ");
60         buffer.append(methodName);
61         buffer.append("() [");
62
63         convert(buffer, result);
64
65         buffer.append("]");
66
67         log.debug(buffer.toString());
68     }
69
70     public static void voidExit(Log log, String JavaDoc methodName)
71     {
72         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(BUFFER_SIZE);
73
74         buffer.append("END ");
75         buffer.append(methodName);
76         buffer.append("()");
77
78         log.debug(buffer.toString());
79     }
80
81     public static void exception(Log log, String JavaDoc methodName, Throwable JavaDoc t)
82     {
83         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(BUFFER_SIZE);
84
85         buffer.append("EXCEPTION ");
86         buffer.append(methodName);
87         buffer.append("() -- ");
88
89         buffer.append(t.getClass().getName());
90
91         log.debug(buffer.toString(), t);
92     }
93
94     public static void convert(StringBuffer JavaDoc buffer, Object JavaDoc input)
95     {
96         if (input == null)
97         {
98             buffer.append("<null>");
99             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 JavaDoc[]))
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 JavaDoc[] array = (Object JavaDoc[]) 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 }
Popular Tags