2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/integration-api/debug.h>
29 # include <boost/thread/tss.hpp>
33 #include <dali/public-api/common/constants.h>
34 #include <dali/public-api/math/matrix3.h>
35 #include <dali/public-api/math/matrix.h>
36 #include <dali/public-api/math/vector3.h>
37 #include <dali/public-api/math/vector4.h>
38 #include <dali/public-api/math/quaternion.h>
39 #include <dali/internal/event/common/thread-local-storage.h>
44 namespace // unnamed namespace
48 * Generic function to print out any 2-Dimensional array
49 * @param[in] data pointer to the source data stored as float[rows][cols]
50 * @param[in] rows number of rows in 2D array
51 * @param[in] cols number of columns in 2D array
52 * @param[in] precision - the precision to write the float data.
53 * @param[in] indent - the indent level to use.
54 * @return string - the text representation of the 2D array
56 std::string Array2DToString(const float *data, unsigned int rows, unsigned int cols, size_t precision, size_t indent)
58 std::ostringstream oss;
60 std::ios_base::fmtflags mask = oss.flags();
61 mask &= ~std::ios_base::scientific;
62 mask |= std::ios_base::fixed;
64 for(unsigned int rowIdx = 0; rowIdx < rows; rowIdx++)
66 oss << std::setw(indent) << std::setfill(' ') << ' ' << "[ ";
67 oss << std::setfill(' ') << std::setprecision(precision) << std::right << std::setiosflags(mask);
69 for(unsigned int colIdx = 0; colIdx < cols; colIdx++)
71 oss << std::setw(precision + 6) << (*data++) << ' ';
74 oss << ']' << std::endl;
88 typedef LogFunction* LogFunctionPtr; ///< LogFunction pointer
91 * This stores a pointer to a log function in thread local storage.
92 * The data has to be allocated from the heap because
93 * it will automatically be deleted when the thread terminates
95 struct ThreadLocalLogging
97 ThreadLocalLogging(LogFunction func)
102 LogFunction function;
105 #ifndef EMSCRIPTEN // single threaded
106 boost::thread_specific_ptr<ThreadLocalLogging> threadLocal;
108 std::auto_ptr<ThreadLocalLogging> threadLocal;
111 /* Forward declarations */
112 std::string FormatToString(const char *format, ...);
113 std::string ArgListToString(const char *format, va_list args);
115 void LogMessage(DebugPriority priority, const char* format, ...)
117 ThreadLocalLogging* threadLogging = threadLocal.get();
118 // see if there is a log function for this thread
123 const LogFunction& logfunction = threadLogging->function;
130 va_start(arg, format);
131 std::string message = ArgListToString(format, arg);
134 logfunction(priority,message);
137 void InstallLogFunction(const LogFunction& logFunction)
139 // TLS stores a pointer to an object.
140 // It needs to be allocated on the heap, because TLS will destroy it when the thread exits.
142 ThreadLocalLogging* logStruct = new ThreadLocalLogging(logFunction);
144 threadLocal.reset(logStruct);
147 void UninstallLogFunction()
154 /*Change false to true if trace is needed but don't commit to codeline*/
155 Filter* Filter::gRender = Filter::New(Debug::Concise, false, "LOG_RENDER");
156 Filter* Filter::gResource = Filter::New(Debug::Concise, false, "LOG_RESOURCE");
157 Filter* Filter::gGLResource = Filter::New(Debug::Concise, false, "LOG_GL_RESOURCE");
158 Filter* Filter::gObject = NULL;
159 Filter* Filter::gImage = Filter::New(Debug::Concise, false, "LOG_IMAGE");
160 Filter* Filter::gModel = Filter::New(Debug::Concise, false, "LOG_MODEL");
161 Filter* Filter::gNode = NULL;
162 Filter* Filter::gElement = NULL;
163 Filter* Filter::gActor = Filter::New(Debug::Concise, false, "LOG_ACTOR");
164 Filter* Filter::gShader = Filter::New(Debug::Concise, false, "LOG_SHADER");
165 Filter* Filter::gDynamics = Filter::New(Debug::Concise, false, "LOG_DYNAMICS");
167 Filter::FilterList* Filter::GetActiveFilters()
169 static Filter::FilterList* activeFilters = new FilterList;
170 return activeFilters;
173 Filter* Filter::New( LogLevel level, bool trace, const char * environmentVariableName )
175 char * environmentVariableValue = getenv( environmentVariableName );
176 if ( environmentVariableValue )
178 unsigned int envLevel(0);
179 char envTraceString(0);
180 sscanf( environmentVariableValue, "%u,%c", &envLevel, &envTraceString );
182 if ( envLevel > Verbose )
186 level = LogLevel( envLevel );
188 // Just use 'f' and 't' as it's faster than doing full string comparisons
189 if ( envTraceString == 't' )
193 else if ( envTraceString == 'f' )
199 Filter* filter = new Filter(level, trace);
201 GetActiveFilters()->push_back(filter);
206 * Enable trace on all filters.
208 void Filter::EnableGlobalTrace()
210 for(FilterIter iter = GetActiveFilters()->begin(); iter != GetActiveFilters()->end(); iter++)
212 (*iter)->EnableTrace();
217 * Disable trace on all filters.
219 void Filter::DisableGlobalTrace()
221 for(FilterIter iter = GetActiveFilters()->begin(); iter != GetActiveFilters()->end(); iter++)
223 (*iter)->DisableTrace();
228 void Filter::Log(LogLevel level, const char* format, ...)
230 if(level <= mLoggingLevel)
233 va_start(arg, format);
236 int numChars = asprintf(&buffer, "%-*c %s", mNesting, ':', format);
237 if(numChars >= 0) // No error
239 std::string message = ArgListToString(buffer, arg);
240 LogMessage(DebugInfo, message.c_str());
248 TraceObj::TraceObj(Filter* filter, const char*format, ...) : mFilter(filter)
250 if(mFilter && mFilter->IsTraceEnabled())
253 va_start(arg, format);
254 mMessage = ArgListToString(format, arg);
257 LogMessage(DebugInfo, "Entr%-*c %s\n", mFilter->mNesting, ':', mMessage.c_str());
262 TraceObj::~TraceObj()
264 if(mFilter && mFilter->IsTraceEnabled())
266 if (mFilter->mNesting)
270 LogMessage(DebugInfo, "Exit%-*c %s\n", mFilter->mNesting, ':', mMessage.c_str());
274 #endif // DEBUG_ENABLED
277 std::string ArgListToString(const char *format, va_list args)
279 std::string str; // empty string
283 int err = vasprintf(&buffer, format, args);
284 if(err >= 0) // No error
293 std::string FormatToString(const char *format, ...)
296 va_start(arg, format);
297 std::string s = ArgListToString(format, arg);
302 std::string ColorToString(const Vector4& color)
304 std::ostringstream oss;
305 oss << "<R:" << color.r << " G:" << color.g << " B:" << color.b << " A:" << color.a << ">";
309 std::string Vector4ToString(const Vector4& v, size_t precision, size_t indent)
311 std::ostringstream oss;
312 oss << std::setw(indent+3) << std::setfill(' ') << std::setprecision(precision) << std::right;
313 oss << "<X:" << std::setw(precision+4) << v.x
314 << " Y:" << std::setw(precision+4) << v.y
315 << " Z:" << std::setw(precision+4) << v.z
316 << " W:" << std::setw(precision+4) << v.w << ">";
320 std::string Vector3ToString(const Vector3& v, size_t precision, size_t indent)
322 std::ostringstream oss;
323 oss << std::setw(indent+3) << std::setfill(' ') << std::setprecision(precision) << std::right << std::setiosflags(std::ios_base::fixed);
324 oss << "<X:" << std::setw(precision+4) << v.x
325 << " Y:" << std::setw(precision+4) << v.y
326 << " Z:" << std::setw(precision+4) << v.z << ">";
330 std::string QuaternionToString(const Quaternion& q, size_t precision, size_t indent)
332 std::ostringstream oss;
336 q.ToAxisAngle(axis, angle);
338 oss << std::setw(indent+3) << std::setfill(' ') << std::setprecision(precision) << std::right;
339 oss << "<A:" << std::setw(precision+4) << angle * 180.0 / Math::PI << ", " << Vector3ToString(axis, precision, 0) << ">";
344 std::string Matrix3ToString(const Matrix3& m, size_t precision, size_t indent)
346 return Array2DToString(m.AsFloat(), 3, 3, precision, indent);
349 std::string MatrixToString(const Matrix& m, size_t precision, size_t indent)
351 return Array2DToString(m.AsFloat(), 4, 4, precision, indent);
356 } // namespace Integration