2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.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
8 // http://floralicense.org/license/
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.
18 #include <dali/integration-api/debug.h>
26 #include <boost/thread/tss.hpp>
29 #include <dali/public-api/common/constants.h>
30 #include <dali/public-api/math/matrix3.h>
31 #include <dali/public-api/math/matrix.h>
32 #include <dali/public-api/math/vector3.h>
33 #include <dali/public-api/math/vector4.h>
34 #include <dali/public-api/math/quaternion.h>
35 #include <dali/internal/event/common/thread-local-storage.h>
40 namespace // unnamed namespace
44 * Generic function to print out any 2-Dimensional array
45 * @param[in] data pointer to the source data stored as float[rows][cols]
46 * @param[in] rows number of rows in 2D array
47 * @param[in] cols number of columns in 2D array
48 * @param[in] precision - the precision to write the float data.
49 * @param[in] indent - the indent level to use.
50 * @return string - the text representation of the 2D array
52 std::string Array2DToString(const float *data, unsigned int rows, unsigned int cols, size_t precision, size_t indent)
54 std::ostringstream oss;
56 std::ios_base::fmtflags mask = oss.flags();
57 mask &= ~std::ios_base::scientific;
58 mask |= std::ios_base::fixed;
60 for(unsigned int rowIdx = 0; rowIdx < rows; rowIdx++)
62 oss << std::setw(indent) << std::setfill(' ') << ' ' << "[ ";
63 oss << std::setfill(' ') << std::setprecision(precision) << std::right << std::setiosflags(mask);
65 for(unsigned int colIdx = 0; colIdx < cols; colIdx++)
67 oss << std::setw(precision + 6) << (*data++) << ' ';
70 oss << ']' << std::endl;
84 typedef LogFunction* LogFunctionPtr; ///< LogFunction pointer
87 * This stores a pointer to a log function in thread local storage.
88 * The data has to be allocated from the heap because
89 * it will automatically be deleted when the thread terminates
91 struct ThreadLocalLogging
93 ThreadLocalLogging(LogFunction func, unsigned int opts)
100 unsigned int logOptions;
103 boost::thread_specific_ptr<ThreadLocalLogging> threadLocal;
105 /* Forward declarations */
106 std::string FormatToString(const char *format, ...);
107 std::string ArgListToString(const char *format, va_list args);
109 unsigned int ParseLogOptions (const char* logOptString)
111 unsigned int ret = LogNone;
112 if (logOptString == NULL)
114 // environment variable was not set, turn on logging for all threads by default
115 ret |= LogEventThread;
116 ret |= LogUpdateThread;
117 ret |= LogRenderThread;
118 ret |= LogResourceThreads;
122 std::string setting(logOptString);
123 if (!setting.compare(DALI_LOG_OFF))
125 // leave as "LogNone"
127 else if (!setting.compare(DALI_LOG_EVENT_THREAD))
129 ret |= LogEventThread;
131 else if (!setting.compare(DALI_LOG_UPDATE_THREAD))
133 ret |= LogUpdateThread;
135 else if (!setting.compare(DALI_LOG_RENDER_THREAD))
137 ret |= LogRenderThread;
139 else if (!setting.compare(DALI_LOG_RESOURCE_THREADS))
141 ret |= LogResourceThreads;
143 else if (!setting.compare(DALI_LOG_ALL_THREADS))
145 ret |= LogEventThread;
146 ret |= LogUpdateThread;
147 ret |= LogRenderThread;
148 ret |= LogResourceThreads;
150 else if (!setting.compare(DALI_LOG_RESOURCE_LIFETIME))
152 ret |= LogEventThread;
153 ret |= LogUpdateThread;
154 ret |= LogRenderThread;
155 ret |= LogResourceThreads;
156 ret |= LogResourceLifetime;
162 void LogMessage(DebugPriority priority, const char* format, ...)
164 ThreadLocalLogging* threadLogging = threadLocal.get();
165 // see if there is a log function for this thread
170 const LogFunction& logfunction = threadLogging->function;
176 // avoid string operations and function call if trying to log resources when not requested
177 if (priority == DebugResources && !(threadLogging->logOptions & LogResourceLifetime))
183 va_start(arg, format);
184 std::string message = ArgListToString(format, arg);
187 logfunction(priority,message);
190 void InstallLogFunction(const LogFunction& logFunction, unsigned int logOpts)
192 // TLS stores a pointer to an object.
193 // It needs to be allocated on the heap, because TLS will destroy it when the thread exits.
195 ThreadLocalLogging* logStruct = new ThreadLocalLogging(logFunction, logOpts);
197 threadLocal.reset(logStruct);
200 void UninstallLogFunction()
207 /*Change false to true if trace is needed but don't commit to codeline*/
208 Filter* Filter::gRender = Filter::New(Debug::Concise, false, "LOG_RENDER");
209 Filter* Filter::gResource = Filter::New(Debug::Concise, false, "LOG_RESOURCE");
210 Filter* Filter::gGLResource = Filter::New(Debug::Concise, false, "LOG_GL_RESOURCE");
211 Filter* Filter::gObject = NULL;
212 Filter* Filter::gImage = Filter::New(Debug::Concise, false, "LOG_IMAGE");
213 Filter* Filter::gModel = Filter::New(Debug::Concise, false, "LOG_MODEL");
214 Filter* Filter::gNode = NULL;
215 Filter* Filter::gElement = NULL;
216 Filter* Filter::gActor = Filter::New(Debug::Concise, false, "LOG_ACTOR");
217 Filter* Filter::gShader = Filter::New(Debug::Concise, false, "LOG_SHADER");
218 Filter* Filter::gDynamics = Filter::New(Debug::Concise, false, "LOG_DYNAMICS");
220 Filter::FilterList* Filter::GetActiveFilters()
222 static Filter::FilterList* activeFilters = new FilterList;
223 return activeFilters;
226 Filter* Filter::New( LogLevel level, bool trace, const char * environmentVariableName )
228 char * environmentVariableValue = getenv( environmentVariableName );
229 if ( environmentVariableValue )
231 unsigned int envLevel(0);
232 char envTraceString(0);
233 sscanf( environmentVariableValue, "%u,%c", &envLevel, &envTraceString );
235 if ( envLevel > Verbose )
239 level = LogLevel( envLevel );
241 // Just use 'f' and 't' as it's faster than doing full string comparisons
242 if ( envTraceString == 't' )
246 else if ( envTraceString == 'f' )
252 Filter* filter = new Filter(level, trace);
254 GetActiveFilters()->push_back(filter);
259 * Enable trace on all filters.
261 void Filter::EnableGlobalTrace()
263 for(FilterIter iter = GetActiveFilters()->begin(); iter != GetActiveFilters()->end(); iter++)
265 (*iter)->EnableTrace();
270 * Disable trace on all filters.
272 void Filter::DisableGlobalTrace()
274 for(FilterIter iter = GetActiveFilters()->begin(); iter != GetActiveFilters()->end(); iter++)
276 (*iter)->DisableTrace();
281 void Filter::Log(LogLevel level, const char* format, ...)
283 if(level <= mLoggingLevel)
286 va_start(arg, format);
289 int numChars = asprintf(&buffer, "%-*c %s", mNesting, ':', format);
290 if(numChars >= 0) // No error
292 std::string message = ArgListToString(buffer, arg);
293 LogMessage(DebugInfo, message.c_str());
301 TraceObj::TraceObj(Filter* filter, const char*format, ...) : mFilter(filter)
303 if(mFilter && mFilter->IsTraceEnabled())
306 va_start(arg, format);
307 mMessage = ArgListToString(format, arg);
310 LogMessage(DebugInfo, "Entr%-*c %s\n", mFilter->mNesting, ':', mMessage.c_str());
315 TraceObj::~TraceObj()
317 if(mFilter && mFilter->IsTraceEnabled())
319 if (mFilter->mNesting)
323 LogMessage(DebugInfo, "Exit%-*c %s\n", mFilter->mNesting, ':', mMessage.c_str());
327 #endif // DEBUG_ENABLED
330 std::string ArgListToString(const char *format, va_list args)
332 std::string str; // empty string
336 int err = vasprintf(&buffer, format, args);
337 if(err >= 0) // No error
346 std::string FormatToString(const char *format, ...)
349 va_start(arg, format);
350 std::string s = ArgListToString(format, arg);
355 std::string ColorToString(const Vector4& color)
357 std::ostringstream oss;
358 oss << "<R:" << color.r << " G:" << color.g << " B:" << color.b << " A:" << color.a << ">";
362 std::string Vector4ToString(const Vector4& v, size_t precision, size_t indent)
364 std::ostringstream oss;
365 oss << std::setw(indent+3) << std::setfill(' ') << std::setprecision(precision) << std::right;
366 oss << "<X:" << std::setw(precision+4) << v.x
367 << " Y:" << std::setw(precision+4) << v.y
368 << " Z:" << std::setw(precision+4) << v.z
369 << " W:" << std::setw(precision+4) << v.w << ">";
373 std::string Vector3ToString(const Vector3& v, size_t precision, size_t indent)
375 std::ostringstream oss;
376 oss << std::setw(indent+3) << std::setfill(' ') << std::setprecision(precision) << std::right << std::setiosflags(std::ios_base::fixed);
377 oss << "<X:" << std::setw(precision+4) << v.x
378 << " Y:" << std::setw(precision+4) << v.y
379 << " Z:" << std::setw(precision+4) << v.z << ">";
383 std::string QuaternionToString(const Quaternion& q, size_t precision, size_t indent)
385 std::ostringstream oss;
389 q.ToAxisAngle(axis, angle);
391 oss << std::setw(indent+3) << std::setfill(' ') << std::setprecision(precision) << std::right;
392 oss << "<A:" << std::setw(precision+4) << angle * 180.0 / Math::PI << ", " << Vector3ToString(axis, precision, 0) << ">";
397 std::string Matrix3ToString(const Matrix3& m, size_t precision, size_t indent)
399 return Array2DToString(m.AsFloat(), 3, 3, precision, indent);
402 std::string MatrixToString(const Matrix& m, size_t precision, size_t indent)
404 return Array2DToString(m.AsFloat(), 4, 4, precision, indent);
409 } // namespace Integration