1 #ifndef DALI_INTEGRATION_DEBUG_H
2 #define DALI_INTEGRATION_DEBUG_H
5 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
28 #include <dali/public-api/common/vector-wrapper.h>
29 #include <dali/public-api/object/property-map.h>
32 #include <dali/public-api/common/dali-common.h>
34 // Using Debug namespace alias shortens the log usage significantly
35 namespace Dali{namespace Integration{namespace Log{}}}
36 namespace Debug = Dali::Integration::Log;
48 #if defined(DEBUG_ENABLED)
50 // Less opaque types for debugger
51 typedef std::vector<Dali::Property::Value> DebugPropertyValueArray;
52 typedef std::pair< Property::Index, Property::Value > DebugIndexValuePair;
53 typedef std::vector<Dali::StringValuePair> DebugStringValueContainer;
54 typedef std::vector< DebugIndexValuePair > DebugIndexValueContainer;
56 struct DebugPropertyValueMap
58 DebugStringValueContainer stringValues;
59 DebugIndexValueContainer intValues;
62 // Fake globals for gdb typedefs
63 extern Dali::DebugPropertyValueArray gValueArray;
64 extern Dali::DebugPropertyValueMap gValueMap;
81 * Used by logging macros to log a message along with function/class name
82 * @param level debug level
83 * @param format string format
85 DALI_CORE_API void LogMessage(enum DebugPriority level,const char *format, ...);
88 * typedef for the logging function.
90 typedef void (*LogFunction)(DebugPriority priority, std::string& message);
93 * A log function has to be installed for every thread that wants to use logging.
94 * This should be done by the adaptor.
95 * The log function can be different for each thread.
96 * @param logFunction the log function to install
97 * @param logOpts the log options to save in thread
99 DALI_CORE_API void InstallLogFunction(const LogFunction& logFunction);
102 * A log function has to be uninstalled for every thread that wants to use logging.
103 * The log function can be different for each thread.
105 DALI_CORE_API void UninstallLogFunction();
107 /********************************************************************************
108 * Error/Warning macros. *
109 ********************************************************************************/
112 * Provides unfiltered logging for global error level messages
114 #define DALI_LOG_ERROR(format, ...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugError, "%s " format, __FUNCTION__, ## __VA_ARGS__)
116 #define DALI_LOG_ERROR_NOFN(format, ...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugError, format, ## __VA_ARGS__)
118 #define DALI_LOG_WARNING_NOFN(format, ...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugWarning, format, ## __VA_ARGS__)
121 * Provides unfiltered logging for fps monitor
123 #define DALI_LOG_FPS(format, ...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, format, ## __VA_ARGS__)
126 * Provides unfiltered logging for update status
128 #define DALI_LOG_UPDATE_STATUS(format, ...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, format, ## __VA_ARGS__)
131 * Provides unfiltered logging for render information
133 #define DALI_LOG_RENDER_INFO(format, ...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, format, ## __VA_ARGS__)
136 * Provides unfiltered logging for release
138 #define DALI_LOG_RELEASE_INFO(format, ...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, format, ## __VA_ARGS__)
143 * Provides unfiltered logging for global warning level messages
145 #define DALI_LOG_WARNING(format, ...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugWarning, "%s " format, ASSERT_LOCATION, ## __VA_ARGS__)
148 #else // DEBUG_ENABLED
150 // Don't warn on release build
151 #define DALI_LOG_WARNING(format, ...)
155 /********************************************************************************
157 ********************************************************************************/
162 * Enumeration of logging levels.
163 * Used by the filters to provide multiple log levels.
164 * In general, the higher the value, the more debug is available for that filter.
176 * The Filter object is used by the DALI_LOG_INFO macro and others to determine if the logging
177 * should take place, and routes the logging via the platform abstraction's LogMessage.
179 * It provides a logging level. If this is set to zero, then DALI_LOG_INFO won't log anything.
180 * It provides the ability to turn tracing on or off.
183 class DALI_CORE_API Filter
186 typedef std::list<Filter*> FilterList;
187 typedef std::list<Filter*>::iterator FilterIter;
192 * Test if the filter is enabled for the given logging level
193 * @param[in] level - the level to test.
194 * @return true if this level of logging is enabled.
196 bool IsEnabledFor(LogLevel level) { return level != Debug::NoLogging && level <= mLoggingLevel;}
199 * Test if trace is enabled for this filter.
200 * @return true if trace is enabled;
202 bool IsTraceEnabled() { return mTraceEnabled; }
205 * Enable tracing on this filter.
207 void EnableTrace() { mTraceEnabled = true; }
210 * Disable tracing on this filter.
212 void DisableTrace() { mTraceEnabled = false; }
215 * Set the log level for this filter. Setting to a higher value than Debug::General also
218 void SetLogLevel(LogLevel level) { mLoggingLevel = level; }
221 * Perform the logging for this filter.
223 void Log(LogLevel level, const char* format, ...);
226 * Create a new filter whose debug level and trace can be modified through the use of an
227 * environment variable.
229 * @param[in] level The default log level
230 * @param[in] trace The default trace level. If true, function tracing is on.
231 * @param[in] environmentVariableName The environment variable name used in order to change the
232 * log level or trace.
234 * @info To modify logg level/trace at runtime, you can should define your filter as shown below:
237 * Debug::Filter* filter = Debug::Filter::New( Debug::NoLogging, false, "FILTER_ENV" );
240 * And to use it when running an executable:
242 * FILTER_ENV=3 dali-demo // LogLevel Verbose, Trace using default
243 * FILTER_ENV=1,true dali-demo // LogLevel Concise, Trace ON
244 * FILTER_ENV=2,false dali-demo // LogLevel General, Trace OFF
245 * FILTER_ENV=0,true dali-demo // LogLevel NoLogging, Trace ON
248 static Filter* New(LogLevel level, bool trace, const char * environmentVariableName );
251 * Enable trace on all filters.
253 static void EnableGlobalTrace();
256 * Disable trace on all filters.
258 static void DisableGlobalTrace();
261 * Set log level for all filters.
263 * @param[in] level The log level
265 static void SetGlobalLogLevel( LogLevel level );
271 * @param[in] level - the highest log level.
272 * @param[in] trace - whether this filter allows tracing.
274 Filter(LogLevel level, bool trace) : mLoggingLevel(level), mTraceEnabled(trace), mNesting(0) {}
276 static FilterList* GetActiveFilters();
279 // High level filters. If these filters are too broad for your current requirement, then
280 // you can add a filter to your own class or source file. If you do, use Filter::New()
281 // to tell this class about it.
283 static Filter *gRender;
284 static Filter *gResource;
285 static Filter *gGLResource;
286 static Filter *gObject;
287 static Filter *gImage;
288 static Filter *gModel;
289 static Filter *gNode;
290 static Filter *gElement;
291 static Filter *gActor;
292 static Filter *gShader;
295 LogLevel mLoggingLevel;
303 #define DALI_LOG_FILTER_SET_LEVEL(filter, level) filter->SetLogLevel(level)
304 #define DALI_LOG_FILTER_ENABLE_TRACE(filter) filter->EnableTrace()
305 #define DALI_LOG_FILTER_DISABLE_TRACE(filter) filter->DisableTrace()
309 #define DALI_LOG_FILTER_SET_LEVEL(filter, level)
310 #define DALI_LOG_FILTER_ENABLE_TRACE(filter)
311 #define DALI_LOG_FILTER_DISABLE_TRACE(filter)
315 /********************************************************************************
316 * General Logging macros *
317 ********************************************************************************/
321 #define DALI_LOG_INFO(filter, level, format, ...) \
322 if(filter && filter->IsEnabledFor(level)) { filter->Log(level, format, ## __VA_ARGS__); }
324 #define DALI_LOG_STREAM( filter, level, stream ) \
325 if(filter && filter->IsEnabledFor(level)) \
327 std::ostringstream o; \
328 o << stream << std::endl; \
329 filter->Log(level, "%s", o.str().c_str()); \
332 #else // DEBUG_ENABLED
334 #define DALI_LOG_INFO(filter, level, format, ...)
335 #define DALI_LOG_STREAM( filter, level, stream )
337 #endif // DEBUG_ENABLED
340 /********************************************************************************
342 ********************************************************************************/
345 * These macros allow the instrumentation of methods. These translate into calls
346 * to LogMessage(DebugInfo).
351 class DALI_CORE_API TraceObj
354 TraceObj(Filter* filter, const char* fmt, ...);
358 std::string mMessage;
363 #define DALI_LOG_TRACE_METHOD_FMT(filter, format, ...) \
364 Dali::Integration::Log::TraceObj debugTraceObj(filter, "%s: " format, ASSERT_LOCATION, ## __VA_ARGS__)
366 #define DALI_LOG_TRACE_METHOD(filter) \
367 Dali::Integration::Log::TraceObj debugTraceObj(filter, ASSERT_LOCATION)
370 #else // DEBUG_ENABLED
372 #define DALI_LOG_TRACE_METHOD_FMT(filter, format, ...)
373 #define DALI_LOG_TRACE_METHOD(filter)
378 /********************************************************************************
379 * Extra object debug *
380 ********************************************************************************/
385 * Warning, this macro changes the current scope, so should usually be put at the
386 * end of the class definition.
388 * Suggest that the value is usually set to the object's name.
389 * Warning - this will increase the size of the object for a debug build.
391 #define DALI_LOG_OBJECT_STRING_DECLARATION \
393 std::string mDebugString;
396 * Print all the actor tree names
398 #define DALI_LOG_ACTOR_TREE( node ) { \
399 std::stringstream branch; \
400 Node* tempNode = node; \
401 while( tempNode ) { \
402 branch << "<" << tempNode->mDebugString << ">::"; \
403 tempNode = tempNode->GetParent(); \
405 DALI_LOG_ERROR_NOFN("Actor tree: %s\n", branch.str().c_str()); \
409 * Allows one object to set another object's debug string
411 #define DALI_LOG_SET_OBJECT_STRING(object, string) (object->mDebugString = string)
414 * Allows one object to set another object's std::string easily
416 #define DALI_LOG_FMT_OBJECT_STRING(object, fmt, ...) (object->mDebugString = FormatToString(fmt, ## __VA_ARGS__))
419 * Allows one object to get another object's debug string
421 #define DALI_LOG_GET_OBJECT_STRING(object) (object->mDebugString)
424 * Get the C string (for use in formatted logging)
426 #define DALI_LOG_GET_OBJECT_C_STR(object) (object->mDebugString.c_str())
429 * Filtered logging of the object's debug string
431 #define DALI_LOG_OBJECT(filter, object) DALI_LOG_INFO(filter, Debug::General, object->mDebugString)
434 #else // DEBUG_ENABLED
436 #define DALI_LOG_OBJECT_STRING_DECLARATION
437 #define DALI_LOG_ACTOR_TREE(node)
438 #define DALI_LOG_SET_OBJECT_STRING(object, string)
439 #define DALI_LOG_FMT_OBJECT_STRING(object, fmt, ...)
440 #define DALI_LOG_GET_OBJECT_STRING(object)
441 #define DALI_LOG_GET_OBJECT_C_STR(object) ""
442 #define DALI_LOG_OBJECT(filter, object)
446 /********************************************************************************
447 * Time instrumentation *
448 ********************************************************************************/
450 #if defined(DEBUG_ENABLED)
452 void GetNanoseconds( uint64_t& timeInNanoseconds );
454 #define DALI_LOG_TIMER_START( timeVariable ) \
455 uint64_t timeVariable##1; \
456 Debug::GetNanoseconds( timeVariable##1 );
458 #define DALI_LOG_TIMER_END( timeVariable, filter, level, preString) \
459 uint64_t timeVariable##2; \
460 Debug::GetNanoseconds( timeVariable##2 ); \
461 DALI_LOG_INFO( filter, level, preString " %ld uSec\n", ((timeVariable##2-timeVariable##1)/1000));
463 #else // DEBUG_ENABLED
465 #define DALI_LOG_TIMER_START( timeVariable )
466 #define DALI_LOG_TIMER_END( timeVariable, filter, level, preString)
475 #endif // DALI_INTEGRATION_DEBUG_H