Coverage Report - at.ipsquare.commons.core.util.DefaultPerformanceLogFormatter
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultPerformanceLogFormatter
100%
42/42
100%
24/24
5.333
 
 1  
 /**
 2  
  * Copyright (C) 2013 Matthias Langer
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *         http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package at.ipsquare.commons.core.util;
 17  
 
 18  
 import java.util.ArrayDeque;
 19  
 import java.util.Deque;
 20  
 
 21  
 import net.jcip.annotations.Immutable;
 22  
 
 23  
 /**
 24  
  * Default {@link PerformanceLogFormatter} implementation.
 25  
  * 
 26  
  * @author Matthias Langer
 27  
  * @since 2.1.0
 28  
  */
 29  
 @Immutable
 30  34
 public final class DefaultPerformanceLogFormatter implements PerformanceLogFormatter
 31  
 {
 32  
     @Override
 33  
     public String format(StackTraceElement from, StackTraceElement to, long millis, String message)
 34  
     {
 35  40
         StringBuilder sb = new StringBuilder();
 36  40
         if(from != null && to != null)
 37  
         {
 38  34
             Class<?> fromClass = StackTrace.associatedClass(from);
 39  34
             Class<?> toClass = StackTrace.associatedClass(to);
 40  
             
 41  34
             if(fromClass.equals(toClass))
 42  
             {
 43  24
                 sb.append(className(fromClass));
 44  24
                 if(from.getMethodName().equals(to.getMethodName()))
 45  
                 {
 46  20
                     sb.append(".")
 47  
                       .append(from.getMethodName())
 48  
                       .append("[")
 49  
                       .append(lineNumberToString(from))
 50  
                       .append("->")
 51  
                       .append(lineNumberToString(to))
 52  
                       .append("]");
 53  
                 }
 54  
                 else 
 55  
                 {
 56  4
                     sb.append("[")
 57  
                       .append(from.getMethodName())
 58  
                       .append(":")
 59  
                       .append(lineNumberToString(from))
 60  
                       .append("->")
 61  
                       .append(to.getMethodName())
 62  
                       .append(":")
 63  
                       .append(lineNumberToString(to))
 64  
                       .append("]");
 65  
                 }
 66  
             }
 67  
             else
 68  
             {
 69  10
                 sb.append("[")
 70  
                   .append(className(fromClass))
 71  
                   .append(".")
 72  
                   .append(from.getMethodName())
 73  
                   .append(":")
 74  
                   .append(lineNumberToString(from))
 75  
                   .append("->")
 76  
                   .append(className(toClass))
 77  
                   .append(".")
 78  
                   .append(to.getMethodName())
 79  
                   .append(":")
 80  
                   .append(lineNumberToString(to))
 81  
                   .append("]");
 82  
             }
 83  34
         }
 84  
         else
 85  
         {
 86  6
             sb.append("[");
 87  6
             StackTraceElement[] fromTo = { from, to };
 88  18
             for(int i = 0; i < 2; ++i)
 89  
             {
 90  12
                 StackTraceElement elem = fromTo[i];
 91  12
                 if(elem == null)
 92  8
                     sb.append("???");
 93  
                 else
 94  
                 {
 95  4
                     Class<?> elemClass = StackTrace.associatedClass(elem);
 96  4
                     sb.append(className(elemClass))
 97  
                       .append(".")
 98  
                       .append(from.getMethodName())
 99  
                       .append(":")
 100  
                       .append(lineNumberToString(elem));
 101  
                 }
 102  
                 
 103  12
                 if(i == 0)
 104  6
                     sb.append("->");
 105  
             }
 106  6
             sb.append("]");
 107  
         }
 108  
         
 109  40
         sb.append(" ")
 110  
         .append(millis)
 111  
         .append("ms");
 112  
         
 113  40
         if(message != null)
 114  
         {
 115  18
             sb.append(" <<")
 116  
               .append(message)
 117  
               .append(">>");
 118  
         }
 119  
         
 120  40
         return sb.toString();
 121  
     }
 122  
     
 123  
     private static String className(Class<?> clazz)
 124  
     {
 125  48
         Deque<Class<?>> parents = new ArrayDeque<Class<?>>(2);
 126  48
         parents.add(clazz);
 127  
         while(true)
 128  
         {
 129  86
             Class<?> parent = parents.getFirst().getEnclosingClass();
 130  86
             if(parent != null)
 131  38
                 parents.addFirst(parent);
 132  
             else
 133  
                 break;
 134  38
         }
 135  
         
 136  48
         StringBuilder sb = new StringBuilder();
 137  48
         for(Class<?> parent : parents)
 138  
         {
 139  86
             if(sb.length() > 0)
 140  38
                 sb.append("$");
 141  86
             sb.append(parent.getSimpleName());
 142  86
         }
 143  48
         return sb.toString();
 144  
     }
 145  
     
 146  
     private static String lineNumberToString(StackTraceElement elem)
 147  
     {
 148  72
         int ln = elem.getLineNumber();
 149  72
         return (ln > 0 ? String.valueOf(ln) : "?");
 150  
     }
 151  
 }