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