2 * Copyright (c) 2021 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"
25 std::string ToString(int x)
27 std::stringstream out;
32 std::string ToString(unsigned int x)
34 std::stringstream out;
39 std::string ToString(float x)
41 std::stringstream out;
49 TraceCallStack::TraceCallStack(std::string prefix)
50 : mTraceActive(false),
56 TraceCallStack::TraceCallStack(bool logging, std::string prefix)
57 : mTraceActive(false),
66 TraceCallStack::~TraceCallStack()
71 * Turn on / off tracing
73 void TraceCallStack::Enable(bool enable)
75 mTraceActive = enable;
78 bool TraceCallStack::IsEnabled()
83 void TraceCallStack::EnableLogging(bool enablelogging)
85 mLogging = enablelogging;
89 * Push a call onto the stack if the trace is active
90 * @param[in] method The name of the method
91 * @param[in] params A comma separated list of parameter values
93 void TraceCallStack::PushCall(std::string method, std::string params)
97 FunctionCall stackFrame(method, params);
98 mCallStack.push_back(stackFrame);
102 fprintf(stderr, "%s%s(%s)\n", mPrefix.c_str(), method.c_str(), params.c_str());
106 void TraceCallStack::PushCall(std::string method, std::string params, const TraceCallStack::NamedParams& altParams)
110 FunctionCall stackFrame(method, params, altParams);
111 mCallStack.push_back(stackFrame);
115 fprintf(stderr, "%s%s(%s)\n", mPrefix.c_str(), method.c_str(), params.c_str());
120 * Search for a method in the stack
121 * @param[in] method The name of the method
122 * @return true if the method was in the stack
124 bool TraceCallStack::FindMethod(std::string method) const
127 for(size_t i = 0; i < mCallStack.size(); i++)
129 if(0 == mCallStack[i].method.compare(method))
137 fprintf(stderr, "Search for %s failed\n", method.c_str());
142 bool TraceCallStack::FindMethodAndGetParameters(std::string method, std::string& params) const
145 for(size_t i = 0; i < mCallStack.size(); i++)
147 if(0 == mCallStack[i].method.compare(method))
150 params = mCallStack[i].paramList;
156 fprintf(stderr, "Search for %s(%s) failed\n", method.c_str(), params.c_str());
161 int TraceCallStack::CountMethod(std::string method) const
164 for(size_t i = 0; i < mCallStack.size(); i++)
166 if(0 == mCallStack[i].method.compare(method))
175 * Search for a method in the stack with the given parameter list
176 * @param[in] method The name of the method
177 * @param[in] params A comma separated list of parameter values
178 * @return true if the method was in the stack
180 bool TraceCallStack::FindMethodAndParams(std::string method, std::string params) const
182 return FindIndexFromMethodAndParams(method, params) > -1;
185 bool TraceCallStack::FindMethodAndParams(std::string method, const NamedParams& params) const
187 return FindIndexFromMethodAndParams(method, params) > -1;
190 bool TraceCallStack::FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const
192 for(size_t i = startIndex; i < mCallStack.size(); ++i)
194 if((mCallStack[i].method.compare(method) == 0) && (mCallStack[i].paramList.compare(params) == 0))
204 * Search for a method in the stack with the given parameter list
205 * @param[in] method The name of the method
206 * @param[in] params A comma separated list of parameter values
207 * @return index in the stack where the method was found or -1 otherwise
209 int32_t TraceCallStack::FindIndexFromMethodAndParams(std::string method, std::string params) const
212 for(size_t i = 0; i < mCallStack.size(); i++)
214 if(0 == mCallStack[i].method.compare(method) && 0 == mCallStack[i].paramList.compare(params))
216 index = static_cast<int32_t>(i);
222 fprintf(stderr, "Search for %s(%s) failed\n", method.c_str(), params.c_str());
227 int TraceCallStack::FindIndexFromMethodAndParams(std::string method, const TraceCallStack::NamedParams& params) const
230 for(size_t i = 0; i < mCallStack.size(); i++)
232 if(0 == mCallStack[i].method.compare(method))
234 // Test each of the passed in parameters:
237 for(auto iter = params.mParams.begin(); iter != params.mParams.end(); ++iter)
239 auto paramIter = mCallStack[i].namedParams.find(iter->parameterName);
240 std::string value = paramIter->value.str();
241 std::string iValue = iter->value.str();
243 if(paramIter == mCallStack[i].namedParams.end() || value.compare(iValue))
251 index = static_cast<int32_t>(i);
259 fprintf(stderr, "Search for %s(%s) failed\n", method.c_str(), params.str().c_str());
266 * Test if the given method and parameters are at a given index in the stack
267 * @param[in] index Index in the call stack
268 * @param[in] method Name of method to test
269 * @param[in] params A comma separated list of parameter values to test
271 bool TraceCallStack::TestMethodAndParams(int index, std::string method, std::string params) const
273 return (0 == mCallStack[index].method.compare(method) && 0 == mCallStack[index].paramList.compare(params));
277 * Reset the call stack
279 void TraceCallStack::Reset()