SLF4J adapter (in 'common') + call graph demo
[logging-analytics.git] / reference / slf4j-reference / src / test / java / org / onap / logging / ref / slf4j / analysis / CallGraphReportWriter.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.logging
4  * ================================================================================
5  * Copyright © 2018 Amdocs
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *    http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.logging.ref.slf4j.analysis;
23
24 import java.util.List;
25
26 import org.apache.commons.lang3.StringUtils;
27 import org.testng.Assert;
28
29 /**
30  * A simple, recursive text-only report writer for the call graph.
31  */
32 public class CallGraphReportWriter {
33
34     /** The analyzer which does the work. */
35     final CallGraphAnalyzer mAnalyzer;
36
37     /** Short report, for validation. */
38     final StringBuilder mShortReport = new StringBuilder();
39
40     /** Longer report, for human eyes. */
41     final StringBuilder mLongReport = new StringBuilder();
42
43     /**
44      * Construct writer.
45      * @param analyzer initialized analyzer.
46      */
47     public CallGraphReportWriter(final CallGraphAnalyzer analyzer) {
48
49         this.mAnalyzer = analyzer;
50
51         Assert.assertTrue(analyzer.getEntries().size() > 0);
52         final LogEntry e0 = analyzer.findEntryPoint();
53         Assert.assertNotNull(e0);
54
55         this.mLongReport.append(e0.toShortString()).append("\n");
56         this.mShortReport.append(StringUtils.substringAfter(e0.getLogger(), ".Component")).append("\n");
57
58         this.report(e0, 1);
59
60     }
61
62     /**
63      * Recursively analyze.
64      * @param invoker entry point.
65      * @param depth recursive depth, for handbrake.
66      */
67     private void report(final LogEntry invoker, final int depth) {
68
69         if (depth > 100) {
70             throw new AssertionError("Recursion ad infinitum");
71         }
72
73         final List<LogEntry> invokes0 = this.mAnalyzer.findInvokes(invoker);
74         for (final LogEntry invoke0 : invokes0) {
75
76             final LogEntry invoked0 = this.mAnalyzer.findInvocation(invoke0);
77
78             Assert.assertNotNull(invoked0);
79
80             final String indent = StringUtils.repeat(' ', depth * 4);
81             this.mLongReport.append(indent).append(invoked0.toShortString()).append('\n');
82             this.mShortReport.append(indent).append(StringUtils.substringAfter(invoked0.getLogger(), ".Component")).append('\n');
83
84             report(invoked0, depth + 1);
85         }
86     }
87
88     /**
89      * Get report.
90      * @return short report, for validation.
91      */
92     public String getShortReport() {
93         return this.mShortReport.toString();
94     }
95
96     /**
97      * Get report.
98      * @return long report, for printing out.
99      */
100     public String getLongReport() {
101         return this.mLongReport.toString();
102     }
103
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public String toString() {
109         return this.getLongReport();
110     }
111 }