2 * Copyright (c) 2023 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.
19 #include <dali/integration-api/trace.h>
32 thread_local LogContextFunction gThreadLocalLogContextFunction = nullptr;
34 void InstallLogContextFunction(const LogContextFunction& logContextFunction)
36 gThreadLocalLogContextFunction = logContextFunction;
39 void LogContext(bool start, const char* tag, const char* message)
41 if(!gThreadLocalLogContextFunction)
45 gThreadLocalLogContextFunction(start, tag, message);
50 typedef std::list<std::unique_ptr<Filter>> FilterList;
51 typedef std::list<std::unique_ptr<Filter>>::iterator FilterIter;
55 static FilterList& GetActiveFilters()
57 static FilterList activeFilters;
62 Filter* Filter::New(bool trace, const char* environmentVariableName)
64 char* environmentVariableValue = getenv(environmentVariableName);
65 if(environmentVariableValue)
67 char envTraceString(0);
68 sscanf(environmentVariableValue, "%c", &envTraceString);
70 // Just use 'f' and 't' as it's faster than doing full string comparisons
71 if(envTraceString == '1' || envTraceString == 't')
75 else if(envTraceString == '0' || envTraceString == 'f')
81 Filter* filter = new Filter(trace);
83 GetActiveFilters().push_back(std::unique_ptr<Filter>(filter));
88 * Enable trace on all filters.
90 void Filter::EnableGlobalTrace()
92 for(FilterIter iter = GetActiveFilters().begin(); iter != GetActiveFilters().end(); iter++)
94 (*iter)->EnableTrace();
99 * Disable trace on all filters.
101 void Filter::DisableGlobalTrace()
103 for(FilterIter iter = GetActiveFilters().begin(); iter != GetActiveFilters().end(); iter++)
105 (*iter)->DisableTrace();
112 void Filter::BeginTrace(const char* tagName)
114 Dali::Integration::Trace::LogContext(true, tagName, nullptr);
117 void Filter::BeginTrace(const char* tagName, const char* message)
119 Dali::Integration::Trace::LogContext(true, tagName, message);
125 void Filter::EndTrace(const char* tagName)
127 Dali::Integration::Trace::LogContext(false, tagName, nullptr);
130 void Filter::EndTrace(const char* tagName, const char* message)
132 Dali::Integration::Trace::LogContext(false, tagName, message);
138 Tracer::Tracer(Filter* filter, const char* tag)
142 if(mFilter && mFilter->IsTraceEnabled())
144 mFilter->BeginTrace(mTag);
153 if(mFilter && mFilter->IsTraceEnabled())
155 mFilter->EndTrace(mTag);
159 #endif //TRACE_ENABLED
163 } // namespace Integration