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