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>
28 # include <boost/thread/tss.hpp>
32 #include <dali/public-api/common/constants.h>
33 #include <dali/public-api/math/matrix3.h>
34 #include <dali/public-api/math/matrix.h>
35 #include <dali/public-api/math/vector3.h>
36 #include <dali/public-api/math/vector4.h>
37 #include <dali/public-api/math/quaternion.h>
38 #include <dali/internal/event/common/thread-local-storage.h>
43 namespace // unnamed namespace
47 * Generic function to print out any 2-Dimensional array
48 * @param[in] data pointer to the source data stored as float[rows][cols]
49 * @param[in] rows number of rows in 2D array
50 * @param[in] cols number of columns in 2D array
51 * @param[in] precision - the precision to write the float data.
52 * @param[in] indent - the indent level to use.
53 * @return string - the text representation of the 2D array
55 std::string Array2DToString(const float *data, unsigned int rows, unsigned int cols, size_t precision, size_t indent)
57 std::ostringstream oss;
59 std::ios_base::fmtflags mask = oss.flags();
60 mask &= ~std::ios_base::scientific;
61 mask |= std::ios_base::fixed;
63 for(unsigned int rowIdx = 0; rowIdx < rows; rowIdx++)
65 oss << std::setw(indent) << std::setfill(' ') << ' ' << "[ ";
66 oss << std::setfill(' ') << std::setprecision(precision) << std::right << std::setiosflags(mask);
68 for(unsigned int colIdx = 0; colIdx < cols; colIdx++)
70 oss << std::setw(precision + 6) << (*data++) << ' ';
73 oss << ']' << std::endl;
87 typedef LogFunction* LogFunctionPtr; ///< LogFunction pointer
90 * This stores a pointer to a log function in thread local storage.
91 * The data has to be allocated from the heap because
92 * it will automatically be deleted when the thread terminates
94 struct ThreadLocalLogging
96 ThreadLocalLogging(LogFunction func)
101 LogFunction function;
104 #ifndef EMSCRIPTEN // single threaded
105 boost::thread_specific_ptr<ThreadLocalLogging> threadLocal;
107 std::auto_ptr<ThreadLocalLogging> threadLocal;
110 /* Forward declarations */
111 std::string FormatToString(const char *format, ...);
112 std::string ArgListToString(const char *format, va_list args);
114 void LogMessage(DebugPriority priority, const char* format, ...)
116 ThreadLocalLogging* threadLogging = threadLocal.get();
117 // see if there is a log function for this thread
122 const LogFunction& logfunction = threadLogging->function;
129 va_start(arg, format);
130 std::string message = ArgListToString(format, arg);
133 logfunction(priority,message);
136 void InstallLogFunction(const LogFunction& logFunction)
138 // TLS stores a pointer to an object.
139 // It needs to be allocated on the heap, because TLS will destroy it when the thread exits.
141 ThreadLocalLogging* logStruct = new ThreadLocalLogging(logFunction);
143 threadLocal.reset(logStruct);
146 void UninstallLogFunction()
153 /*Change false to true if trace is needed but don't commit to codeline*/
154 Filter* Filter::gRender = Filter::New(Debug::Concise, false, "LOG_RENDER");
155 Filter* Filter::gResource = Filter::New(Debug::Concise, false, "LOG_RESOURCE");
156 Filter* Filter::gGLResource = Filter::New(Debug::Concise, false, "LOG_GL_RESOURCE");
157 Filter* Filter::gObject = NULL;
158 Filter* Filter::gImage = Filter::New(Debug::Concise, false, "LOG_IMAGE");
159 Filter* Filter::gModel = Filter::New(Debug::Concise, false, "LOG_MODEL");
160 Filter* Filter::gNode = NULL;
161 Filter* Filter::gElement = NULL;
162 Filter* Filter::gActor = Filter::New(Debug::Concise, false, "LOG_ACTOR");
163 Filter* Filter::gShader = Filter::New(Debug::Concise, false, "LOG_SHADER");
164 Filter* Filter::gDynamics = Filter::New(Debug::Concise, false, "LOG_DYNAMICS");
166 Filter::FilterList* Filter::GetActiveFilters()
168 static Filter::FilterList* activeFilters = new FilterList;
169 return activeFilters;
172 Filter* Filter::New( LogLevel level, bool trace, const char * environmentVariableName )
174 char * environmentVariableValue = getenv( environmentVariableName );
175 if ( environmentVariableValue )
177 unsigned int envLevel(0);
178 char envTraceString(0);
179 sscanf( environmentVariableValue, "%u,%c", &envLevel, &envTraceString );
181 if ( envLevel > Verbose )
185 level = LogLevel( envLevel );
187 // Just use 'f' and 't' as it's faster than doing full string comparisons
188 if ( envTraceString == 't' )
192 else if ( envTraceString == 'f' )
198 Filter* filter = new Filter(level, trace);
200 GetActiveFilters()->push_back(filter);
205 * Enable trace on all filters.
207 void Filter::EnableGlobalTrace()
209 for(FilterIter iter = GetActiveFilters()->begin(); iter != GetActiveFilters()->end(); iter++)
211 (*iter)->EnableTrace();
216 * Disable trace on all filters.
218 void Filter::DisableGlobalTrace()
220 for(FilterIter iter = GetActiveFilters()->begin(); iter != GetActiveFilters()->end(); iter++)
222 (*iter)->DisableTrace();
227 void Filter::Log(LogLevel level, const char* format, ...)
229 if(level <= mLoggingLevel)
232 va_start(arg, format);
235 int numChars = asprintf(&buffer, "%-*c %s", mNesting, ':', format);
236 if(numChars >= 0) // No error
238 std::string message = ArgListToString(buffer, arg);
239 LogMessage(DebugInfo, message.c_str());
247 TraceObj::TraceObj(Filter* filter, const char*format, ...) : mFilter(filter)
249 if(mFilter && mFilter->IsTraceEnabled())
252 va_start(arg, format);
253 mMessage = ArgListToString(format, arg);
256 LogMessage(DebugInfo, "Entr%-*c %s\n", mFilter->mNesting, ':', mMessage.c_str());
261 TraceObj::~TraceObj()
263 if(mFilter && mFilter->IsTraceEnabled())
265 if (mFilter->mNesting)
269 LogMessage(DebugInfo, "Exit%-*c %s\n", mFilter->mNesting, ':', mMessage.c_str());
273 #endif // DEBUG_ENABLED
276 std::string ArgListToString(const char *format, va_list args)
278 std::string str; // empty string
282 int err = vasprintf(&buffer, format, args);
283 if(err >= 0) // No error
292 std::string FormatToString(const char *format, ...)
295 va_start(arg, format);
296 std::string s = ArgListToString(format, arg);
301 std::string ColorToString(const Vector4& color)
303 std::ostringstream oss;
304 oss << "<R:" << color.r << " G:" << color.g << " B:" << color.b << " A:" << color.a << ">";
308 std::string Vector4ToString(const Vector4& v, size_t precision, size_t indent)
310 std::ostringstream oss;
311 oss << std::setw(indent+3) << std::setfill(' ') << std::setprecision(precision) << std::right;
312 oss << "<X:" << std::setw(precision+4) << v.x
313 << " Y:" << std::setw(precision+4) << v.y
314 << " Z:" << std::setw(precision+4) << v.z
315 << " W:" << std::setw(precision+4) << v.w << ">";
319 std::string Vector3ToString(const Vector3& v, size_t precision, size_t indent)
321 std::ostringstream oss;
322 oss << std::setw(indent+3) << std::setfill(' ') << std::setprecision(precision) << std::right << std::setiosflags(std::ios_base::fixed);
323 oss << "<X:" << std::setw(precision+4) << v.x
324 << " Y:" << std::setw(precision+4) << v.y
325 << " Z:" << std::setw(precision+4) << v.z << ">";
329 std::string QuaternionToString(const Quaternion& q, size_t precision, size_t indent)
331 std::ostringstream oss;
335 q.ToAxisAngle(axis, angle);
337 oss << std::setw(indent+3) << std::setfill(' ') << std::setprecision(precision) << std::right;
338 oss << "<A:" << std::setw(precision+4) << angle * 180.0 / Math::PI << ", " << Vector3ToString(axis, precision, 0) << ">";
343 std::string Matrix3ToString(const Matrix3& m, size_t precision, size_t indent)
345 return Array2DToString(m.AsFloat(), 3, 3, precision, indent);
348 std::string MatrixToString(const Matrix& m, size_t precision, size_t indent)
350 return Array2DToString(m.AsFloat(), 4, 4, precision, indent);
355 } // namespace Integration