5786f3a169d7dd81aa05119b1f169e435481a1de
[platform/core/uifw/dali-core.git] / dali / integration-api / trace.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/integration-api/trace.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstdarg>
23 #include <list>
24
25 namespace Dali
26 {
27 namespace Integration
28 {
29 namespace Trace
30 {
31 thread_local LogContextFunction gThreadLocalLogContextFunction = nullptr;
32
33 void InstallLogContextFunction(const LogContextFunction& logContextFunction)
34 {
35   gThreadLocalLogContextFunction = logContextFunction;
36 }
37
38 void LogContext(bool start, const char* tag, const char* message)
39 {
40   if(!gThreadLocalLogContextFunction)
41   {
42     return;
43   }
44   gThreadLocalLogContextFunction(start, tag, message);
45 }
46
47 #ifdef TRACE_ENABLED
48
49 typedef std::list<Filter*>           FilterList;
50 typedef std::list<Filter*>::iterator FilterIter;
51
52 namespace
53 {
54 static FilterList* GetActiveFilters()
55 {
56   static FilterList* activeFilters = new FilterList;
57   return activeFilters;
58 }
59 } // namespace
60
61 Filter* Filter::New(bool trace, const char* environmentVariableName)
62 {
63   char* environmentVariableValue = getenv(environmentVariableName);
64   if(environmentVariableValue)
65   {
66     char envTraceString(0);
67     sscanf(environmentVariableValue, "%c", &envTraceString);
68
69     // Just use 'f' and 't' as it's faster than doing full string comparisons
70     if(envTraceString == '1' || envTraceString == 't')
71     {
72       trace = true;
73     }
74     else if(envTraceString == '0' || envTraceString == 'f')
75     {
76       trace = false;
77     }
78   }
79
80   Filter* filter = new Filter(trace);
81   GetActiveFilters()->push_back(filter);
82   return filter;
83 }
84
85 /**
86  * Enable trace on all filters.
87  */
88 void Filter::EnableGlobalTrace()
89 {
90   for(FilterIter iter = GetActiveFilters()->begin(); iter != GetActiveFilters()->end(); iter++)
91   {
92     (*iter)->EnableTrace();
93   }
94 }
95
96 /**
97  * Disable trace on all filters.
98  */
99 void Filter::DisableGlobalTrace()
100 {
101   for(FilterIter iter = GetActiveFilters()->begin(); iter != GetActiveFilters()->end(); iter++)
102   {
103     (*iter)->DisableTrace();
104   }
105 }
106
107 /**
108  * Begin Trace
109  */
110 void Filter::BeginTrace(const char* tagName)
111 {
112   Dali::Integration::Trace::LogContext(true, tagName, nullptr);
113 }
114
115 void Filter::BeginTrace(const char* tagName, const char* message)
116 {
117   Dali::Integration::Trace::LogContext(true, tagName, message);
118 }
119
120 /**
121  * End Trace
122  */
123 void Filter::EndTrace(const char* tagName)
124 {
125   Dali::Integration::Trace::LogContext(false, tagName, nullptr);
126 }
127
128 void Filter::EndTrace(const char* tagName, const char* message)
129 {
130   Dali::Integration::Trace::LogContext(false, tagName, message);
131 }
132
133 /**
134  * Tracer Constructor
135  */
136 Tracer::Tracer(Filter* filter, const char* tag)
137 : mTag(tag),
138   mFilter(filter)
139 {
140   if(mFilter && mFilter->IsTraceEnabled())
141   {
142     mFilter->BeginTrace(mTag);
143   }
144 }
145
146 /**
147  * Tracer Destructor
148  */
149 Tracer::~Tracer()
150 {
151   if(mFilter && mFilter->IsTraceEnabled())
152   {
153     mFilter->EndTrace(mTag);
154   }
155 }
156
157 #endif //TRACE_ENABLED
158
159 } // namespace Trace
160
161 } // namespace Integration
162
163 } // namespace Dali