Revert "[Tizen] Appendix log for ttrace + Print keycode and timestamp"
[platform/core/uifw/dali-core.git] / dali / integration-api / trace.cpp
1 /*
2  * Copyright (c) 2022 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)
39 {
40   if(!gThreadLocalLogContextFunction)
41   {
42     return;
43   }
44   gThreadLocalLogContextFunction(start, tag);
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);
113 }
114
115 /**
116  * End Trace
117  */
118 void Filter::EndTrace(const char* tagName)
119 {
120   Dali::Integration::Trace::LogContext(false, tagName);
121 }
122
123 /**
124  * Tracer Constructor
125  */
126 Tracer::Tracer(Filter* filter, const char* tag)
127 : mTag(tag),
128   mFilter(filter)
129 {
130   if(mFilter && mFilter->IsTraceEnabled())
131   {
132     mFilter->BeginTrace(mTag);
133   }
134 }
135
136 /**
137  * Tracer Destructor
138  */
139 Tracer::~Tracer()
140 {
141   if(mFilter && mFilter->IsTraceEnabled())
142   {
143     mFilter->EndTrace(mTag);
144   }
145 }
146
147 #endif //TRACE_ENABLED
148
149 } // namespace Trace
150
151 } // namespace Integration
152
153 } // namespace Dali