2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "test-trace-call-stack.h"
24 std::string ToString(int x)
26 std::stringstream out;
31 std::string ToString(unsigned int x)
33 std::stringstream out;
38 std::string ToString(float x)
40 std::stringstream out;
48 TraceCallStack::TraceCallStack()
56 TraceCallStack::~TraceCallStack()
61 * Turn on / off tracing
63 void TraceCallStack::Enable(bool enable)
65 mTraceActive = enable;
68 bool TraceCallStack::IsEnabled()
74 * Push a call onto the stack if the trace is active
75 * @param[in] method The name of the method
76 * @param[in] params A comma separated list of parameter values
78 void TraceCallStack::PushCall(std::string method, std::string params)
82 FunctionCall stackFrame(method, params);
83 mCallStack.push_back(stackFrame);
87 void TraceCallStack::PushCall(std::string method, std::string params, const TraceCallStack::NamedParams& altParams)
91 FunctionCall stackFrame(method, params, altParams);
92 mCallStack.push_back(stackFrame);
97 * Search for a method in the stack
98 * @param[in] method The name of the method
99 * @return true if the method was in the stack
101 bool TraceCallStack::FindMethod(std::string method) const
104 for(size_t i = 0; i < mCallStack.size(); i++)
106 if(0 == mCallStack[i].method.compare(method))
115 bool TraceCallStack::FindMethodAndGetParameters(std::string method, std::string& params) const
118 for(size_t i = 0; i < mCallStack.size(); i++)
120 if(0 == mCallStack[i].method.compare(method))
123 params = mCallStack[i].paramList;
130 int TraceCallStack::CountMethod(std::string method) const
133 for(size_t i = 0; i < mCallStack.size(); i++)
135 if(0 == mCallStack[i].method.compare(method))
144 * Search for a method in the stack with the given parameter list
145 * @param[in] method The name of the method
146 * @param[in] params A comma separated list of parameter values
147 * @return true if the method was in the stack
149 bool TraceCallStack::FindMethodAndParams(std::string method, std::string params) const
151 return FindIndexFromMethodAndParams(method, params) > -1;
154 bool TraceCallStack::FindMethodAndParams(std::string method, const NamedParams& params) const
156 return FindIndexFromMethodAndParams(method, params) > -1;
159 bool TraceCallStack::FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const
161 for(size_t i = startIndex; i < mCallStack.size(); ++i)
163 if((mCallStack[i].method.compare(method) == 0) && (mCallStack[i].paramList.compare(params) == 0))
173 * Search for a method in the stack with the given parameter list
174 * @param[in] method The name of the method
175 * @param[in] params A comma separated list of parameter values
176 * @return index in the stack where the method was found or -1 otherwise
178 int32_t TraceCallStack::FindIndexFromMethodAndParams(std::string method, std::string params) const
181 for(size_t i = 0; i < mCallStack.size(); i++)
183 if(0 == mCallStack[i].method.compare(method) && 0 == mCallStack[i].paramList.compare(params))
185 index = static_cast<int32_t>(i);
192 int TraceCallStack::FindIndexFromMethodAndParams(std::string method, const TraceCallStack::NamedParams& params) const
195 for(size_t i = 0; i < mCallStack.size(); i++)
197 if(0 == mCallStack[i].method.compare(method))
199 // Test each of the passed in parameters:
201 for(NamedParams::const_iterator iter = params.begin(); iter != params.end(); ++iter)
203 NamedParams::const_iterator paramIter = mCallStack[i].namedParams.find(iter->first);
204 if(paramIter == params.end() || paramIter->second.compare(iter->second) != 0)
212 index = static_cast<int32_t>(i);
221 * Test if the given method and parameters are at a given index in the stack
222 * @param[in] index Index in the call stack
223 * @param[in] method Name of method to test
224 * @param[in] params A comma separated list of parameter values to test
226 bool TraceCallStack::TestMethodAndParams(int index, std::string method, std::string params) const
228 return (0 == mCallStack[index].method.compare(method) && 0 == mCallStack[index].paramList.compare(params));
232 * Reset the call stack
234 void TraceCallStack::Reset()