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.
26 #include <dali/public-api/common/dali-common.h>
28 // Using Debug namespace alias shortens the log usage significantly
29 namespace Dali{namespace Integration{namespace Log{}}}
30 namespace Debug = Dali::Integration::Log;
47 // environment variable for enabling/disabling fps tracking
48 #define DALI_ENV_FPS_TRACKING "DALI_FPS_TRACKING"
50 // environment variable for enabling/disabling fps tracking
51 #define DALI_ENV_UPDATE_STATUS_INTERVAL "DALI_UPDATE_STATUS_INTERVAL"
61 * Used by logging macros to log a message along with function/class name
62 * @param level debug level
63 * @param format string format
65 DALI_IMPORT_API void LogMessage(enum DebugPriority level,const char *format, ...);
68 * typedef for the logging function.
70 typedef void (*LogFunction)(DebugPriority priority, std::string& message);
73 * A log function has to be installed for every thread that wants to use logging.
74 * This should be done by the adaptor.
75 * The log function can be different for each thread.
76 * @param logFunction the log function to install
77 * @param logOpts the log options to save in thread
79 DALI_IMPORT_API void InstallLogFunction(const LogFunction& logFunction);
82 * A log function has to be uninstalled for every thread that wants to use logging.
83 * The log function can be different for each thread.
85 DALI_IMPORT_API void UninstallLogFunction();
87 /********************************************************************************
88 * Error/Warning macros. *
89 ********************************************************************************/
92 * Provides unfiltered logging for global error level messages
94 #define DALI_LOG_ERROR(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugError, "%s " format, __PRETTY_FUNCTION__, ## args)
96 #define DALI_LOG_ERROR_NOFN(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugError, format, ## args)
99 * Provides unfiltered logging for fps monitor
101 #define DALI_LOG_FPS(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, format, ## args)
104 * Provides unfiltered logging for update status
106 #define DALI_LOG_UPDATE_STATUS(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, format, ## args)
109 * Provides unfiltered logging for render information
111 #define DALI_LOG_RENDER_INFO(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, format, ## args)
116 * Provides unfiltered logging for global warning level messages
118 #define DALI_LOG_WARNING(format, args...) Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugWarning, "%s " format, __PRETTY_FUNCTION__, ## args)
121 #else // DEBUG_ENABLED
123 // Don't warn on release build
124 #define DALI_LOG_WARNING(format, args...)
128 /********************************************************************************
130 ********************************************************************************/
135 * Enumeration of logging levels.
136 * Used by the filters to provide multiple log levels.
137 * In general, the higher the value, the more debug is available for that filter.
149 * The Filter object is used by the DALI_LOG_INFO macro and others to determine if the logging
150 * should take place, and routes the logging via the platform abstraction's LogMessage.
152 * It provides a logging level. If this is set to zero, then DALI_LOG_INFO won't log anything.
153 * It provides the ability to turn tracing on or off.
156 class DALI_IMPORT_API Filter
159 typedef std::list<Filter*> FilterList;
160 typedef std::list<Filter*>::iterator FilterIter;
165 * Test if the filter is enabled for the given logging level
166 * @param[in] level - the level to test.
167 * @return true if this level of logging is enabled.
169 bool IsEnabledFor(LogLevel level) { return level != Debug::NoLogging && level <= mLoggingLevel;}
172 * Test if trace is enabled for this filter.
173 * @return true if trace is enabled;
175 bool IsTraceEnabled() { return mTraceEnabled; }
178 * Enable tracing on this filter.
180 void EnableTrace() { mTraceEnabled = true; }
183 * Disable tracing on this filter.
185 void DisableTrace() { mTraceEnabled = false; }
188 * Set the log level for this filter. Setting to a higher value than Debug::General also
191 void SetLogLevel(LogLevel level) { mLoggingLevel = level; }
194 * Perform the logging for this filter.
196 void Log(LogLevel level, const char* format, ...);
199 * Create a new filter whose debug level and trace can be modified through the use of an
200 * environment variable.
202 * @param[in] level The default log level
203 * @param[in] trace The default trace level. If true, function tracing is on.
204 * @param[in] environmentVariableName The environment variable name used in order to change the
205 * log level or trace.
207 * @info To modify logg level/trace at runtime, you can should define your filter as shown below:
210 * Debug::Filter* filter = Debug::Filter::New( Debug::NoLogging, false, "FILTER_ENV" );
213 * And to use it when running an executable:
215 * FILTER_ENV=3 dali-demo // LogLevel Verbose, Trace using default
216 * FILTER_ENV=1,true dali-demo // LogLevel Concise, Trace ON
217 * FILTER_ENV=2,false dali-demo // LogLevel General, Trace OFF
218 * FILTER_ENV=0,true dali-demo // LogLevel NoLogging, Trace ON
221 static Filter* New(LogLevel level, bool trace, const char * environmentVariableName );
224 * Enable trace on all filters.
226 void EnableGlobalTrace();
229 * Disable trace on all filters.
231 void DisableGlobalTrace();
237 * @param[in] level - the highest log level.
238 * @param[in] trace - whether this filter allows tracing.
240 Filter(LogLevel level, bool trace) : mLoggingLevel(level), mTraceEnabled(trace), mNesting(0) {}
242 static FilterList* GetActiveFilters();
245 // High level filters. If these filters are too broad for your current requirement, then
246 // you can add a filter to your own class or source file. If you do, use Filter::New()
247 // to tell this class about it.
249 static Filter *gRender;
250 static Filter *gResource;
251 static Filter *gGLResource;
252 static Filter *gObject;
253 static Filter *gImage;
254 static Filter *gModel;
255 static Filter *gNode;
256 static Filter *gElement;
257 static Filter *gActor;
258 static Filter *gShader;
259 static Filter *gDynamics;
262 LogLevel mLoggingLevel;
270 #define DALI_LOG_FILTER_SET_LEVEL(filter, level) filter->SetLogLevel(level)
271 #define DALI_LOG_FILTER_ENABLE_TRACE(filter) filter->EnableTrace()
272 #define DALI_LOG_FILTER_DISABLE_TRACE(filter) filter->DisableTrace()
276 #define DALI_LOG_FILTER_SET_LEVEL(filter, level)
277 #define DALI_LOG_FILTER_ENABLE_TRACE(filter)
278 #define DALI_LOG_FILTER_DISABLE_TRACE(filter)
282 /********************************************************************************
283 * General Logging macros *
284 ********************************************************************************/
288 #define DALI_LOG_INFO(filter, level, format, args...) \
289 if(filter && filter->IsEnabledFor(level)) { filter->Log(level, format, ## args); }
291 #else // DEBUG_ENABLED
293 #define DALI_LOG_INFO(filter, level, format, args...)
295 #endif // DEBUG_ENABLED
298 /********************************************************************************
300 ********************************************************************************/
303 * These macros allow the instrumentation of methods. These translate into calls
304 * to LogMessage(DebugInfo).
309 class DALI_IMPORT_API TraceObj
312 TraceObj(Filter* filter, const char* fmt, ...);
316 std::string mMessage;
321 #define DALI_LOG_TRACE_METHOD_FMT(filter, format, args...) \
322 Dali::Integration::Log::TraceObj debugTraceObj(filter, "%s: " format, __PRETTY_FUNCTION__, ## args)
324 #define DALI_LOG_TRACE_METHOD(filter) \
325 Dali::Integration::Log::TraceObj debugTraceObj(filter, __PRETTY_FUNCTION__)
328 #else // DEBUG_ENABLED
330 #define DALI_LOG_TRACE_METHOD_FMT(filter, format, args...)
331 #define DALI_LOG_TRACE_METHOD(filter)
336 /********************************************************************************
337 * Extra object debug *
338 ********************************************************************************/
343 * Warning, this macro changes the current scope, so should usually be put at the
344 * end of the class definition.
346 * Suggest that the value is usually set to the object's name.
347 * Warning - this will increase the size of the object for a debug build.
349 #define DALI_LOG_OBJECT_STRING_DECLARATION \
351 std::string mDebugString;
354 * Allows one object to set another object's debug string
356 #define DALI_LOG_SET_OBJECT_STRING(object, string) (object->mDebugString = string)
359 * Allows one object to set another object's std::string easily
361 #define DALI_LOG_FMT_OBJECT_STRING(object, fmt, args...) (object->mDebugString = FormatToString(fmt, ## args))
364 * Allows one object to get another object's debug string
366 #define DALI_LOG_GET_OBJECT_STRING(object) (object->mDebugString)
369 * Get the C string (for use in formatted logging)
371 #define DALI_LOG_GET_OBJECT_C_STR(object) (object->mDebugString.c_str())
374 * Filtered logging of the object's debug string
376 #define DALI_LOG_OBJECT(filter, object) DALI_LOG_INFO(filter, Debug::General, object->mDebugString)
379 #else // DEBUG_ENABLED
381 #define DALI_LOG_OBJECT_STRING_DECLARATION
382 #define DALI_LOG_SET_OBJECT_STRING(object, string)
383 #define DALI_LOG_FMT_OBJECT_STRING(object, fmt, args...)
384 #define DALI_LOG_GET_OBJECT_STRING(object)
385 #define DALI_LOG_GET_OBJECT_C_STR(object) ""
386 #define DALI_LOG_OBJECT(filter, object)
390 /********************************************************************************
392 ********************************************************************************/
395 * Helper method to translate a color to a string.
396 * @param[in] color - the color to translate
397 * @return string - the text representation of the color.
399 DALI_IMPORT_API std::string ColorToString(const Vector4& color);
402 * Helper method to translate a vector4 to a string.
403 * @param[in] v - the vector
404 * @param[in] precision - the precision to write the float data.
405 * @param[in] indent - the indent level to use.
406 * @return string - the text representation of the vector.
408 DALI_IMPORT_API std::string Vector4ToString(const Vector4& v, size_t precision=3, size_t indent=0);
411 * Helper method to translate a vector4 to a string.
412 * @param[in] v - the vector
413 * @param[in] precision - the precision to write the float data.
414 * @param[in] indent - the indent level to use.
415 * @return string - the text representation of the vector.
417 DALI_IMPORT_API std::string Vector3ToString(const Vector3& v, size_t precision=3, size_t indent=0);
420 * Helper method to translate a quaternion to a string.
421 * @param[in] q the quaternion
422 * @param[in] precision - the precision to write the float data.
423 * @param[in] indent - the indent level to use.
424 * @return string - the text representation of the quaternion.
426 DALI_IMPORT_API std::string QuaternionToString(const Quaternion& q, size_t precision=3, size_t indent=0);
429 * Helper method to translate a 3x3 matrix to a string.
430 * @param[in] m - the matrix
431 * @param[in] precision - the precision to write the float data.
432 * @param[in] indent - the indent level to use.
433 * @return string - the text representation of the vector.
435 DALI_IMPORT_API std::string Matrix3ToString(const Matrix3& m, size_t precision=3, size_t indent=0);
438 * Helper method to translate a 4x4 matrix to a string.
439 * @param[in] m - the matrix
440 * @param[in] precision - the precision to write the float data.
441 * @param[in] indent - the indent level to use.
442 * @return string - the text representation of the vector.
444 DALI_IMPORT_API std::string MatrixToString(const Matrix& m, size_t precision=3, size_t indent=0);
450 * Filtered write of a matrix
452 #define DALI_LOG_MATRIX(filter, matrix) DALI_LOG_INFO(filter, Debug::General, MatrixToString(matrix))
455 * Filtered write of a vector
457 #define DALI_LOG_VECTOR4(filter, vector) DALI_LOG_INFO(filter, Debug::General, Vector4ToString(vector))
460 * Filtered write of a vector
462 #define DALI_LOG_VECTOR3(filter, vector) DALI_LOG_INFO(filter, Debug::General, Vector3ToString(vector))
465 * Filtered write of a color
467 #define DALI_LOG_COLOR(filter, color) DALI_LOG_INFO(filter, Debug::General, ColorToString(color))
471 #define DALI_LOG_MATRIX(filter, matrix)
472 #define DALI_LOG_VECTOR4(filter,vector)
473 #define DALI_LOG_VECTOR3(filter,vector)
474 #define DALI_LOG_COLOR(filter, color)
478 }}} // Dali/Integration/Debug namespaces
481 #endif // __DALI_INTEGRATION_DEBUG_H__