Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.functions » [javadoc | source]
    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  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.util.Collection;
   22   import java.util.LinkedList;
   23   import java.util.List;
   24   
   25   import org.apache.jmeter.engine.util.CompoundVariable;
   26   import org.apache.jmeter.samplers.SampleResult;
   27   import org.apache.jmeter.samplers.Sampler;
   28   import org.apache.jmeter.util.JMeterUtils;
   29   import org.apache.jorphan.logging.LoggingManager;
   30   import org.apache.log.Logger;
   31   import org.apache.log.Priority;
   32   
   33   /**
   34    * <p>
   35    * Function to log a message.
   36    * </p>
   37    *
   38    * <p>
   39    * Parameters:
   40    * <ul>
   41    * <li>string value</li>
   42    * <li>log level (optional; defaults to INFO; or DEBUG if unrecognised; or can use OUT or ERR)</li>
   43    * <li>throwable message (optional)</li>
   44    * <li>comment (optional)</li>
   45    * </ul>
   46    * </p>
   47    * Returns: - the input string
   48    *
   49    */
   50   public class LogFunction extends AbstractFunction {
   51       private static final Logger log = LoggingManager.getLoggerForClass();
   52   
   53       private static final List desc = new LinkedList();
   54   
   55       private static final String KEY = "__log"; //$NON-NLS-1$
   56   
   57       // Number of parameters expected - used to reject invalid calls
   58       private static final int MIN_PARAMETER_COUNT = 1;
   59   
   60       private static final int MAX_PARAMETER_COUNT = 4;
   61       static {
   62           desc.add(JMeterUtils.getResString("log_function_string_ret"));    //$NON-NLS-1$
   63           desc.add(JMeterUtils.getResString("log_function_level"));     //$NON-NLS-1$
   64           desc.add(JMeterUtils.getResString("log_function_throwable")); //$NON-NLS-1$
   65           desc.add(JMeterUtils.getResString("log_function_comment"));   //$NON-NLS-1$
   66       }
   67   
   68       private static final String DEFAULT_PRIORITY = "INFO"; //$NON-NLS-1$
   69   
   70       private static final String DEFAULT_SEPARATOR = " : "; //$NON-NLS-1$
   71   
   72       private Object[] values;
   73   
   74       public LogFunction() {
   75       }
   76   
   77       public synchronized String execute(SampleResult previousResult, Sampler currentSampler)
   78               throws InvalidVariableException {
   79           String stringToLog = ((CompoundVariable) values[0]).execute();
   80   
   81           String priorityString;
   82           if (values.length > 1) { // We have a default
   83               priorityString = ((CompoundVariable) values[1]).execute();
   84               if (priorityString.length() == 0) {
   85                   priorityString = DEFAULT_PRIORITY;
   86               }
   87           } else {
   88               priorityString = DEFAULT_PRIORITY;
   89           }
   90   
   91           Throwable t = null;
   92           if (values.length > 2) { // Throwable wanted
   93               String value = ((CompoundVariable) values[2]).execute();
   94               if (value.length() > 0) {
   95                   t = new Throwable(value);
   96               }
   97           }
   98   
   99           String comment = "";
  100           if (values.length > 3) { // Comment wanted
  101               comment = ((CompoundVariable) values[3]).execute();
  102           }
  103   
  104           logDetails(log, stringToLog, priorityString, t, comment);
  105   
  106           return stringToLog;
  107   
  108       }
  109   
  110       // Common output function
  111       private static void printDetails(java.io.PrintStream ps, String s, Throwable t, String c) {
  112           String tn = Thread.currentThread().getName();
  113   
  114           StringBuffer sb = new StringBuffer(80);
  115           sb.append("Log: ");
  116           sb.append(tn);
  117           if (c.length()>0){
  118               sb.append(" ");
  119               sb.append(c);
  120           } else {
  121               sb.append(DEFAULT_SEPARATOR);
  122           }
  123           sb.append(s);
  124           if (t != null) {
  125               sb.append(" ");
  126               ps.print(sb.toString());
  127               t.printStackTrace(ps);
  128           } else {
  129               ps.println(sb.toString());
  130           }
  131       }
  132   
  133       // Routine to perform the output (also used by __logn() function)
  134       static void logDetails(Logger l, String s, String prio, Throwable t, String c) {
  135           if (prio.equalsIgnoreCase("OUT")) //$NON-NLS-1
  136           {
  137               printDetails(System.out, s, t, c);
  138           } else if (prio.equalsIgnoreCase("ERR")) //$NON-NLS-1
  139           {
  140               printDetails(System.err, s, t, c);
  141           } else {
  142               // N.B. if the string is not recognised, DEBUG is assumed
  143               Priority p = Priority.getPriorityForName(prio);
  144               if (log.isPriorityEnabled(p)) {// Thread method is potentially expensive
  145                   String tn = Thread.currentThread().getName();
  146                   StringBuffer sb = new StringBuffer(40);
  147                   sb.append(tn);
  148                   if (c.length()>0){
  149                       sb.append(" ");
  150                       sb.append(c);
  151                   } else {
  152                       sb.append(DEFAULT_SEPARATOR);
  153                   }
  154                   sb.append(s);
  155                   log.log(p, sb.toString(), t);
  156               }
  157           }
  158   
  159       }
  160   
  161       public synchronized void setParameters(Collection parameters) throws InvalidVariableException {
  162           checkParameterCount(parameters, MIN_PARAMETER_COUNT, MAX_PARAMETER_COUNT);
  163           values = parameters.toArray();
  164       }
  165   
  166       public String getReferenceKey() {
  167           return KEY;
  168       }
  169   
  170       public List getArgumentDesc() {
  171           return desc;
  172       }
  173   
  174   }

Save This Page
Home » jakarta-jmeter-2.3.4_src » org.apache.jmeter.functions » [javadoc | source]