1 #ifndef __DALI_INTEGRATION_DEBUG_H__
2 #define __DALI_INTEGRATION_DEBUG_H__
5 * Copyright (c) 2014 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.
27 #include <dali/public-api/common/dali-common.h>
29 // Using Debug namespace alias shortens the log usage significantly
30 namespace Dali{namespace Integration{namespace Log{}}}
31 namespace Debug = Dali::Integration::Log;
56 * Used by logging macros to log a message along with function/class name
57 * @param level debug level
58 * @param format string format
60 DALI_IMPORT_API void LogMessage(enum DebugPriority level,const char *format, ...);
63 * typedef for the logging function.
65 typedef void (*LogFunction)(DebugPriority priority, std::string& message);
68 * A log function has to be installed for every thread that wants to use logging.
69 * This should be done by the adaptor.
70 * The log function can be different for each thread.
71 * @param logFunction the log function to install
72 * @param logOpts the log options to save in thread
74 DALI_IMPORT_API void InstallLogFunction(const LogFunction& logFunction);
77 * A log function has to be uninstalled for every thread that wants to use logging.
78 * The log function can be different for each thread.
80 DALI_IMPORT_API void UninstallLogFunction();
82 /********************************************************************************
83 * Error/Warning macros. *
84 ********************************************************************************/
87 * Provides unfiltered logging for global error level messages
89 #define DALI_LOG_ERROR(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugError, "%s " format, __PRETTY_FUNCTION__, ## args)
91 #define DALI_LOG_ERROR_NOFN(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugError, format, ## args)
93 #define DALI_LOG_WARNING_NOFN(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugWarning, format, ## args)
96 * Provides unfiltered logging for fps monitor
98 #define DALI_LOG_FPS(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, format, ## args)
101 * Provides unfiltered logging for update status
103 #define DALI_LOG_UPDATE_STATUS(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, format, ## args)
106 * Provides unfiltered logging for render information
108 #define DALI_LOG_RENDER_INFO(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, format, ## args)
113 * Provides unfiltered logging for global warning level messages
115 #define DALI_LOG_WARNING(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugWarning, "%s " format, __PRETTY_FUNCTION__, ## args)
118 #else // DEBUG_ENABLED
120 // Don't warn on release build
121 #define DALI_LOG_WARNING(format, args...)
125 /********************************************************************************
127 ********************************************************************************/
132 * Enumeration of logging levels.
133 * Used by the filters to provide multiple log levels.
134 * In general, the higher the value, the more debug is available for that filter.
146 * The Filter object is used by the DALI_LOG_INFO macro and others to determine if the logging
147 * should take place, and routes the logging via the platform abstraction's LogMessage.
149 * It provides a logging level. If this is set to zero, then DALI_LOG_INFO won't log anything.
150 * It provides the ability to turn tracing on or off.
153 class DALI_IMPORT_API Filter
156 typedef std::list<Filter*> FilterList;
157 typedef std::list<Filter*>::iterator FilterIter;
162 * Test if the filter is enabled for the given logging level
163 * @param[in] level - the level to test.
164 * @return true if this level of logging is enabled.
166 bool IsEnabledFor(LogLevel level) { return level != Debug::NoLogging && level <= mLoggingLevel;}
169 * Test if trace is enabled for this filter.
170 * @return true if trace is enabled;
172 bool IsTraceEnabled() { return mTraceEnabled; }
175 * Enable tracing on this filter.
177 void EnableTrace() { mTraceEnabled = true; }
180 * Disable tracing on this filter.
182 void DisableTrace() { mTraceEnabled = false; }
185 * Set the log level for this filter. Setting to a higher value than Debug::General also
188 void SetLogLevel(LogLevel level) { mLoggingLevel = level; }
191 * Perform the logging for this filter.
193 void Log(LogLevel level, const char* format, ...);
196 * Create a new filter whose debug level and trace can be modified through the use of an
197 * environment variable.
199 * @param[in] level The default log level
200 * @param[in] trace The default trace level. If true, function tracing is on.
201 * @param[in] environmentVariableName The environment variable name used in order to change the
202 * log level or trace.
204 * @info To modify logg level/trace at runtime, you can should define your filter as shown below:
207 * Debug::Filter* filter = Debug::Filter::New( Debug::NoLogging, false, "FILTER_ENV" );
210 * And to use it when running an executable:
212 * FILTER_ENV=3 dali-demo // LogLevel Verbose, Trace using default
213 * FILTER_ENV=1,true dali-demo // LogLevel Concise, Trace ON
214 * FILTER_ENV=2,false dali-demo // LogLevel General, Trace OFF
215 * FILTER_ENV=0,true dali-demo // LogLevel NoLogging, Trace ON
218 static Filter* New(LogLevel level, bool trace, const char * environmentVariableName );
221 * Enable trace on all filters.
223 void EnableGlobalTrace();
226 * Disable trace on all filters.
228 void DisableGlobalTrace();
234 * @param[in] level - the highest log level.
235 * @param[in] trace - whether this filter allows tracing.
237 Filter(LogLevel level, bool trace) : mLoggingLevel(level), mTraceEnabled(trace), mNesting(0) {}
239 static FilterList* GetActiveFilters();
242 // High level filters. If these filters are too broad for your current requirement, then
243 // you can add a filter to your own class or source file. If you do, use Filter::New()
244 // to tell this class about it.
246 static Filter *gRender;
247 static Filter *gResource;
248 static Filter *gGLResource;
249 static Filter *gObject;
250 static Filter *gImage;
251 static Filter *gModel;
252 static Filter *gNode;
253 static Filter *gElement;
254 static Filter *gActor;
255 static Filter *gShader;
258 LogLevel mLoggingLevel;
266 #define DALI_LOG_FILTER_SET_LEVEL(filter, level) filter->SetLogLevel(level)
267 #define DALI_LOG_FILTER_ENABLE_TRACE(filter) filter->EnableTrace()
268 #define DALI_LOG_FILTER_DISABLE_TRACE(filter) filter->DisableTrace()
272 #define DALI_LOG_FILTER_SET_LEVEL(filter, level)
273 #define DALI_LOG_FILTER_ENABLE_TRACE(filter)
274 #define DALI_LOG_FILTER_DISABLE_TRACE(filter)
278 /********************************************************************************
279 * General Logging macros *
280 ********************************************************************************/
284 #define DALI_LOG_INFO(filter, level, format, args...) \
285 if(filter && filter->IsEnabledFor(level)) { filter->Log(level, format, ## args); }
287 #else // DEBUG_ENABLED
289 #define DALI_LOG_INFO(filter, level, format, args...)
291 #endif // DEBUG_ENABLED
294 /********************************************************************************
296 ********************************************************************************/
299 * These macros allow the instrumentation of methods. These translate into calls
300 * to LogMessage(DebugInfo).
305 class DALI_IMPORT_API TraceObj
308 TraceObj(Filter* filter, const char* fmt, ...);
312 std::string mMessage;
317 #define DALI_LOG_TRACE_METHOD_FMT(filter, format, args...) \
318 Dali::Integration::Log::TraceObj debugTraceObj(filter, "%s: " format, __PRETTY_FUNCTION__, ## args)
320 #define DALI_LOG_TRACE_METHOD(filter) \
321 Dali::Integration::Log::TraceObj debugTraceObj(filter, __PRETTY_FUNCTION__)
324 #else // DEBUG_ENABLED
326 #define DALI_LOG_TRACE_METHOD_FMT(filter, format, args...)
327 #define DALI_LOG_TRACE_METHOD(filter)
332 /********************************************************************************
333 * Extra object debug *
334 ********************************************************************************/
339 * Warning, this macro changes the current scope, so should usually be put at the
340 * end of the class definition.
342 * Suggest that the value is usually set to the object's name.
343 * Warning - this will increase the size of the object for a debug build.
345 #define DALI_LOG_OBJECT_STRING_DECLARATION \
347 std::string mDebugString;
350 * Print all the actor tree names
352 #define DALI_LOG_ACTOR_TREE( node ) { \
353 std::stringstream branch; \
354 Node* tempNode = node; \
355 while( tempNode ) { \
356 branch << "<" << tempNode->mDebugString << ">::"; \
357 tempNode = tempNode->GetParent(); \
359 DALI_LOG_ERROR_NOFN("Actor tree: %s\n", branch.str().c_str()); \
363 * Allows one object to set another object's debug string
365 #define DALI_LOG_SET_OBJECT_STRING(object, string) (object->mDebugString = string)
368 * Allows one object to set another object's std::string easily
370 #define DALI_LOG_FMT_OBJECT_STRING(object, fmt, args...) (object->mDebugString = FormatToString(fmt, ## args))
373 * Allows one object to get another object's debug string
375 #define DALI_LOG_GET_OBJECT_STRING(object) (object->mDebugString)
378 * Get the C string (for use in formatted logging)
380 #define DALI_LOG_GET_OBJECT_C_STR(object) (object->mDebugString.c_str())
383 * Filtered logging of the object's debug string
385 #define DALI_LOG_OBJECT(filter, object) DALI_LOG_INFO(filter, Debug::General, object->mDebugString)
388 #else // DEBUG_ENABLED
390 #define DALI_LOG_OBJECT_STRING_DECLARATION
391 #define DALI_LOG_ACTOR_TREE(node)
392 #define DALI_LOG_SET_OBJECT_STRING(object, string)
393 #define DALI_LOG_FMT_OBJECT_STRING(object, fmt, args...)
394 #define DALI_LOG_GET_OBJECT_STRING(object)
395 #define DALI_LOG_GET_OBJECT_C_STR(object) ""
396 #define DALI_LOG_OBJECT(filter, object)
400 /********************************************************************************
402 ********************************************************************************/
405 * Helper method to translate a color to a string.
406 * @param[in] color - the color to translate
407 * @return string - the text representation of the color.
409 DALI_IMPORT_API std::string ColorToString(const Vector4& color);
412 * Helper method to translate a vector4 to a string.
413 * @param[in] v - the vector
414 * @param[in] precision - the precision to write the float data.
415 * @param[in] indent - the indent level to use.
416 * @return string - the text representation of the vector.
418 DALI_IMPORT_API std::string Vector4ToString(const Vector4& v, size_t precision=3, size_t indent=0);
421 * Helper method to translate a vector4 to a string.
422 * @param[in] v - the vector
423 * @param[in] precision - the precision to write the float data.
424 * @param[in] indent - the indent level to use.
425 * @return string - the text representation of the vector.
427 DALI_IMPORT_API std::string Vector3ToString(const Vector3& v, size_t precision=3, size_t indent=0);
430 * Helper method to translate a quaternion to a string.
431 * @param[in] q the quaternion
432 * @param[in] precision - the precision to write the float data.
433 * @param[in] indent - the indent level to use.
434 * @return string - the text representation of the quaternion.
436 DALI_IMPORT_API std::string QuaternionToString(const Quaternion& q, size_t precision=3, size_t indent=0);
439 * Helper method to translate a 3x3 matrix to a string.
440 * @param[in] m - the matrix
441 * @param[in] precision - the precision to write the float data.
442 * @param[in] indent - the indent level to use.
443 * @return string - the text representation of the vector.
445 DALI_IMPORT_API std::string Matrix3ToString(const Matrix3& m, size_t precision=3, size_t indent=0);
448 * Helper method to translate a 4x4 matrix to a string.
449 * @param[in] m - the matrix
450 * @param[in] precision - the precision to write the float data.
451 * @param[in] indent - the indent level to use.
452 * @return string - the text representation of the vector.
454 DALI_IMPORT_API std::string MatrixToString(const Matrix& m, size_t precision=3, size_t indent=0);
460 * Filtered write of a matrix
462 #define DALI_LOG_MATRIX(filter, matrix) DALI_LOG_INFO(filter, Debug::General, MatrixToString(matrix))
465 * Filtered write of a vector
467 #define DALI_LOG_VECTOR4(filter, vector) DALI_LOG_INFO(filter, Debug::General, Vector4ToString(vector))
470 * Filtered write of a vector
472 #define DALI_LOG_VECTOR3(filter, vector) DALI_LOG_INFO(filter, Debug::General, Vector3ToString(vector))
475 * Filtered write of a color
477 #define DALI_LOG_COLOR(filter, color) DALI_LOG_INFO(filter, Debug::General, ColorToString(color))
481 #define DALI_LOG_MATRIX(filter, matrix)
482 #define DALI_LOG_VECTOR4(filter,vector)
483 #define DALI_LOG_VECTOR3(filter,vector)
484 #define DALI_LOG_COLOR(filter, color)
488 }}} // Dali/Integration/Debug namespaces
491 #endif // __DALI_INTEGRATION_DEBUG_H__