2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.misc.env.impl;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.Stack;
28 import org.onap.aaf.misc.env.Env;
29 import org.onap.aaf.misc.env.LogTarget;
30 import org.onap.aaf.misc.env.Slot;
31 import org.onap.aaf.misc.env.StoreImpl;
32 import org.onap.aaf.misc.env.TimeTaken;
33 import org.onap.aaf.misc.env.TransStore;
35 public abstract class AbsTrans<ENV extends Env> implements TransStore {
36 private static final float[] EMPTYF = new float[0];
37 private static final Object[] EMPTYO = new Object[0];
39 protected ENV delegate;
40 protected List<TimeTaken> trail = new ArrayList<TimeTaken>(30);
41 private Object[] state;
44 public AbsTrans(ENV delegate) {
45 this.delegate = delegate;
46 state = delegate instanceof StoreImpl?((StoreImpl) delegate).newTransState():EMPTYO;
50 public LogTarget fatal() {
51 return delegate.fatal();
55 public LogTarget error() {
56 return delegate.error();
60 public LogTarget audit() {
61 return delegate.audit();
65 public LogTarget init() {
66 return delegate.init();
70 public LogTarget warn() {
71 return delegate.warn();
75 public LogTarget info() {
76 return delegate.info();
80 public LogTarget debug() {
81 return delegate.debug();
85 public LogTarget trace() {
86 return delegate.trace();
90 * Let the final Trans Implementation choose the exact kind of TimeTaken to use
95 protected abstract TimeTaken newTimeTaken(String name, int flag);
98 public final TimeTaken start(String name, int flag) {
99 TimeTaken tt = newTimeTaken(name,flag);
105 public final void checkpoint(String name) {
106 TimeTaken tt = newTimeTaken(name,CHECKPOINT);
111 public final void checkpoint(String name, int additionalFlag) {
112 TimeTaken tt = newTimeTaken(name,CHECKPOINT|additionalFlag);
118 public Metric auditTrail(int indent, StringBuilder sb, int ... flags) {
119 return auditTrail(info(),indent,sb,flags);
123 public Metric auditTrail(LogTarget lt, int indent, StringBuilder sb, int ... flags) {
124 Metric metric = new Metric();
125 int last = (metric.entries = trail.size()) -1;
126 metric.buckets = flags.length==0?EMPTYF:new float[flags.length];
128 TimeTaken first = trail.get(0);
129 // If first entry is sub, then it's actually the last "end" as well
130 // otherwise, check end
131 //long end = (first.flag&SUB)==SUB?first.end():trail.get(last).end();
132 long end = trail.get(last).end();
133 metric.total = (end - first.start) / 1000000f;
137 for(TimeTaken tt : trail) {
138 float ms = tt.millis();
139 for(int i=0;i<flags.length;++i) {
140 if(tt.flag == flags[i]) metric.buckets[i]+=ms;
143 } else if(!lt.isLoggable()) {
144 boolean first = true;
145 for(TimeTaken tt : trail) {
146 float ms = tt.millis();
147 for(int i=0;i<flags.length;++i) {
148 if(tt.flag == flags[i]) metric.buckets[i]+=ms;
150 if((tt.flag&ALWAYS)==ALWAYS) {
151 if(first) first = false;
157 Stack<Long> stack = new Stack<Long>();
158 for(TimeTaken tt : trail) {
159 // Create Indentation based on SUB
160 while(!stack.isEmpty() && tt.end()>stack.peek()) {
164 for(int i=0;i<indent;++i) {
169 if((tt.flag&SUB)==SUB) {
170 stack.push(tt.end());
174 // Add time values to Metric
175 float ms = tt.millis();
176 for(int i=0;i<flags.length;++i) {
177 if(tt.flag == flags[i]) metric.buckets[i]+=ms;
185 * Put data into the Trans State at the right slot
188 public void put(Slot slot, Object value) {
189 slot.put(state, value);
193 * Get data from the Trans State from the right slot
195 * This will do a cast to the expected type derived from Default
198 @SuppressWarnings("unchecked")
199 public<T> T get(Slot slot, T deflt) {
203 } catch(ArrayIndexOutOfBoundsException e) {
204 // Env State Size has changed because of dynamic Object creation... Rare event, but needs to be covered
205 Object[] temp = ((StoreImpl) delegate).newTransState();
206 System.arraycopy(state, 0, temp, 0, state.length);
210 return o==null?deflt:(T)o;