KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > functions > LogFunction


1 // $Header: /home/cvs/jakarta-jmeter/src/functions/org/apache/jmeter/functions/LogFunction.java,v 1.3.2.2 2004/06/20 00:39:18 sebb Exp $
2
/*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.functions;
20
21 import java.io.Serializable JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.jmeter.engine.util.CompoundVariable;
27 import org.apache.jmeter.samplers.SampleResult;
28 import org.apache.jmeter.samplers.Sampler;
29 import org.apache.jorphan.logging.LoggingManager;
30 import org.apache.log.Logger;
31 import org.apache.log.Priority;
32
33 /**
34  * Function to log a message
35  *
36  * Parameters:
37  * - string
38  * - log level (optional; defaults to INFO; or DEBUG if unrecognised)
39  * - throwable message (optional)
40  *
41  * Returns:
42  * - the input string
43  *
44  * @version $Revision: 1.3.2.2 $ Updated: $Date: 2004/06/20 00:39:18 $
45  */

46 public class LogFunction extends AbstractFunction implements Serializable JavaDoc
47 {
48     private static Logger log = LoggingManager.getLoggerForClass();
49
50     private static final List JavaDoc desc = new LinkedList JavaDoc();
51     private static final String JavaDoc KEY = "__log";
52
53     // Number of parameters expected - used to reject invalid calls
54
private static final int MIN_PARAMETER_COUNT = 1;
55     private static final int MAX_PARAMETER_COUNT = 3;
56     static {
57         desc.add("String to be logged");
58         desc.add("Log level (default INFO)");
59         desc.add("Throwable text (optional)");
60     }
61     private static final String JavaDoc DEFAULT_PRIORITY = "INFO"; //$NON-NLS-1$
62

63     private Object JavaDoc[] values;
64
65     public LogFunction()
66     {
67     }
68
69     public Object JavaDoc clone()
70     {
71         return new LogFunction();
72     }
73
74     public synchronized String JavaDoc execute(
75         SampleResult previousResult,
76         Sampler currentSampler)
77         throws InvalidVariableException
78     {
79         String JavaDoc stringToLog = ((CompoundVariable) values[0]).execute();
80         
81         String JavaDoc priorityString;
82         if (values.length > 1){ // We have a default
83
priorityString= ((CompoundVariable) values[1]).execute();
84             if (priorityString.length()==0) priorityString= DEFAULT_PRIORITY;
85         } else {
86             priorityString = DEFAULT_PRIORITY;
87         }
88         
89         Throwable JavaDoc t=null;
90         if (values.length > 2){ // Throwable wanted
91
t = new Throwable JavaDoc(((CompoundVariable) values[2]).execute());
92         }
93         
94         logDetails(log,stringToLog,priorityString,t);
95         
96         return stringToLog;
97
98     }
99
100     // Common output function
101
private static void printDetails(java.io.PrintStream JavaDoc ps,String JavaDoc s, Throwable JavaDoc t)
102     {
103         String JavaDoc tn = Thread.currentThread().getName();
104         if (t != null)
105         {
106             ps.print("Log: "+ tn + " : " + s + " ");
107             t.printStackTrace(ps);
108         }
109         else
110         {
111             ps.println("Log: "+ tn + " : " + s);
112         }
113     }
114     
115     // Routine to perform the output (also used by __logn() function)
116
static void logDetails(Logger l,String JavaDoc s,String JavaDoc prio,Throwable JavaDoc t)
117     {
118         if (prio.equalsIgnoreCase("OUT")) //$NON-NLS-1
119
{
120             printDetails(System.out,s,t);
121         }
122         else
123         if (prio.equalsIgnoreCase("ERR")) //$NON-NLS-1
124
{
125             printDetails(System.err,s,t);
126         }
127         else
128         {
129             // N.B. if the string is not recognised, DEBUG is assumed
130
Priority p = Priority.getPriorityForName(prio);
131             if (log.isPriorityEnabled(p)){//Thread method is potentially expensive
132
String JavaDoc tn = Thread.currentThread().getName();
133                 log.log(p,tn+" "+s,t);
134             }
135         }
136         
137     }
138
139     public void setParameters(Collection JavaDoc parameters)
140         throws InvalidVariableException
141     {
142
143         values = parameters.toArray();
144
145         if ((values.length < MIN_PARAMETER_COUNT)
146             || (values.length > MAX_PARAMETER_COUNT))
147         {
148             throw new InvalidVariableException(
149                 "Parameter Count not between "
150                     + MIN_PARAMETER_COUNT
151                     + " & "
152                     + MAX_PARAMETER_COUNT);
153         }
154
155     }
156
157     public String JavaDoc getReferenceKey()
158     {
159         return KEY;
160     }
161
162     public List JavaDoc getArgumentDesc()
163     {
164         return desc;
165     }
166
167 }
Popular Tags