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),
58 TraceCallStack::~TraceCallStack()
63 * Turn on / off tracing
65 void TraceCallStack::Enable(bool enable)
67 mTraceActive = enable;
70 bool TraceCallStack::IsEnabled()
75 void TraceCallStack::EnableLogging(bool enablelogging)
77 mLogging = enablelogging;
81 * Push a call onto the stack if the trace is active
82 * @param[in] method The name of the method
83 * @param[in] params A comma separated list of parameter values
85 void TraceCallStack::PushCall(std::string method, std::string params)
89 FunctionCall stackFrame(method, params);
90 mCallStack.push_back(stackFrame);
94 fprintf(stderr, "%s%s(%s)\n", mPrefix.c_str(), method.c_str(), params.c_str());
98 void TraceCallStack::PushCall(std::string method, std::string params, const TraceCallStack::NamedParams& altParams)
102 FunctionCall stackFrame(method, params, altParams);
103 mCallStack.push_back(stackFrame);
107 fprintf(stderr, "%s%s(%s)\n", mPrefix.c_str(), method.c_str(), params.c_str());
112 * Search for a method in the stack
113 * @param[in] method The name of the method
114 * @return true if the method was in the stack
116 bool TraceCallStack::FindMethod(std::string method) const
119 for(size_t i = 0; i < mCallStack.size(); i++)
121 if(0 == mCallStack[i].method.compare(method))
129 fprintf(stderr, "Search for %s failed\n", method.c_str());
134 bool TraceCallStack::FindMethodAndGetParameters(std::string method, std::string& params) const
137 for(size_t i = 0; i < mCallStack.size(); i++)
139 if(0 == mCallStack[i].method.compare(method))
142 params = mCallStack[i].paramList;
148 fprintf(stderr, "Search for %s(%s) failed\n", method.c_str(), params.c_str());
153 int TraceCallStack::CountMethod(std::string method) const
156 for(size_t i = 0; i < mCallStack.size(); i++)
158 if(0 == mCallStack[i].method.compare(method))
167 * Search for a method in the stack with the given parameter list
168 * @param[in] method The name of the method
169 * @param[in] params A comma separated list of parameter values
170 * @return true if the method was in the stack
172 bool TraceCallStack::FindMethodAndParams(std::string method, std::string params) const
174 return FindIndexFromMethodAndParams(method, params) > -1;
177 bool TraceCallStack::FindMethodAndParams(std::string method, const NamedParams& params) const
179 return FindIndexFromMethodAndParams(method, params) > -1;
182 bool TraceCallStack::FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const
184 for(size_t i = startIndex; i < mCallStack.size(); ++i)
186 if((mCallStack[i].method.compare(method) == 0) && (mCallStack[i].paramList.compare(params) == 0))
196 * Search for a method in the stack with the given parameter list
197 * @param[in] method The name of the method
198 * @param[in] params A comma separated list of parameter values
199 * @return index in the stack where the method was found or -1 otherwise
201 int32_t TraceCallStack::FindIndexFromMethodAndParams(std::string method, std::string params) const
204 for(size_t i = 0; i < mCallStack.size(); i++)
206 if(0 == mCallStack[i].method.compare(method) && 0 == mCallStack[i].paramList.compare(params))
208 index = static_cast<int32_t>(i);
215 int TraceCallStack::FindIndexFromMethodAndParams(std::string method, const TraceCallStack::NamedParams& params) const
218 for(size_t i = 0; i < mCallStack.size(); i++)
220 if(0 == mCallStack[i].method.compare(method))
222 // Test each of the passed in parameters:
224 for(NamedParams::const_iterator iter = params.begin(); iter != params.end(); ++iter)
226 NamedParams::const_iterator paramIter = mCallStack[i].namedParams.find(iter->first);
227 if(paramIter == params.end() || paramIter->second.compare(iter->second) != 0)
235 index = static_cast<int32_t>(i);
244 * Test if the given method and parameters are at a given index in the stack
245 * @param[in] index Index in the call stack
246 * @param[in] method Name of method to test
247 * @param[in] params A comma separated list of parameter values to test
249 bool TraceCallStack::TestMethodAndParams(int index, std::string method, std::string params) const
251 return (0 == mCallStack[index].method.compare(method) && 0 == mCallStack[index].paramList.compare(params));
255 * Reset the call stack
257 void TraceCallStack::Reset()