Dali 3D User Interface Engine
debug.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17 
18 // CLASS HEADER
20 
21 // EXTERNAL INCLUDES
22 #include <cstdio>
23 #include <cstdarg>
24 #include <cstring>
25 #include <cstdlib>
26 #include <sstream>
27 #include <iomanip>
28 
29 // INTERNAL INCLUDES
37 
38 namespace Dali
39 {
40 
41 namespace // unnamed namespace
42 {
43 
53 std::string Array2DToString(const float *data, unsigned int rows, unsigned int cols, size_t precision, size_t indent)
54 {
55  std::ostringstream oss;
56 
57  std::ios_base::fmtflags mask = oss.flags();
58  mask &= ~std::ios_base::scientific;
59  mask |= std::ios_base::fixed;
60 
61  for(unsigned int rowIdx = 0; rowIdx < rows; rowIdx++)
62  {
63  oss << std::setw(indent) << std::setfill(' ') << ' ' << "[ ";
64  oss << std::setfill(' ') << std::setprecision(precision) << std::right << std::setiosflags(mask);
65 
66  for(unsigned int colIdx = 0; colIdx < cols; colIdx++)
67  {
68  oss << std::setw(precision + 6) << (*data++) << ' ';
69  }
70 
71  oss << ']' << std::endl;
72  }
73 
74  return oss.str();
75 }
76 
77 }
78 
79 namespace Integration
80 {
81 
82 namespace Log
83 {
84 
86 
87 /* Forward declarations */
88 std::string FormatToString(const char *format, ...);
89 std::string ArgListToString(const char *format, va_list args);
90 
91 void LogMessage(DebugPriority priority, const char* format, ...)
92 {
94  {
95  return;
96  }
97 
98  va_list arg;
99  va_start(arg, format);
100  std::string message = ArgListToString(format, arg);
101  va_end(arg);
102 
103  gthreadLocalLogFunction(priority,message);
104 }
105 
106 void InstallLogFunction(const LogFunction& logFunction)
107 {
108  // TLS stores a pointer to an object.
109  // It needs to be allocated on the heap, because TLS will destroy it when the thread exits.
110 
111  gthreadLocalLogFunction = logFunction;
112 }
113 
115 {
117 }
118 
119 #ifdef DEBUG_ENABLED
120 
121 /*Change false to true if trace is needed but don't commit to codeline*/
122 Filter* Filter::gRender = Filter::New(Debug::Concise, false, "LOG_RENDER");
123 Filter* Filter::gResource = Filter::New(Debug::Concise, false, "LOG_RESOURCE");
124 Filter* Filter::gGLResource = Filter::New(Debug::Concise, false, "LOG_GL_RESOURCE");
125 Filter* Filter::gObject = NULL;
126 Filter* Filter::gImage = Filter::New(Debug::Concise, false, "LOG_IMAGE");
127 Filter* Filter::gModel = Filter::New(Debug::Concise, false, "LOG_MODEL");
128 Filter* Filter::gNode = NULL;
129 Filter* Filter::gElement = NULL;
130 Filter* Filter::gActor = Filter::New(Debug::Concise, false, "LOG_ACTOR");
131 Filter* Filter::gShader = Filter::New(Debug::Concise, false, "LOG_SHADER");
132 
133 Filter::FilterList* Filter::GetActiveFilters()
134 {
135  static Filter::FilterList* activeFilters = new FilterList;
136  return activeFilters;
137 }
138 
139 Filter* Filter::New( LogLevel level, bool trace, const char * environmentVariableName )
140 {
141  char * environmentVariableValue = getenv( environmentVariableName );
142  if ( environmentVariableValue )
143  {
144  unsigned int envLevel(0);
145  char envTraceString(0);
146  sscanf( environmentVariableValue, "%u,%c", &envLevel, &envTraceString );
147 
148  if ( envLevel > Verbose )
149  {
150  envLevel = Verbose;
151  }
152  level = LogLevel( envLevel );
153 
154  // Just use 'f' and 't' as it's faster than doing full string comparisons
155  if ( envTraceString == 't' )
156  {
157  trace = true;
158  }
159  else if ( envTraceString == 'f' )
160  {
161  trace = false;
162  }
163  }
164 
165  Filter* filter = new Filter(level, trace);
166  filter->mNesting++;
167  GetActiveFilters()->push_back(filter);
168  return filter;
169 }
170 
174 void Filter::EnableGlobalTrace()
175 {
176  for(FilterIter iter = GetActiveFilters()->begin(); iter != GetActiveFilters()->end(); iter++)
177  {
178  (*iter)->EnableTrace();
179  }
180 }
181 
185 void Filter::DisableGlobalTrace()
186 {
187  for(FilterIter iter = GetActiveFilters()->begin(); iter != GetActiveFilters()->end(); iter++)
188  {
189  (*iter)->DisableTrace();
190  }
191 }
192 
193 
194 void Filter::Log(LogLevel level, const char* format, ...)
195 {
196  if(level <= mLoggingLevel)
197  {
198  va_list arg;
199  va_start(arg, format);
200 
201  if( mTraceEnabled )
202  {
203  char *buffer = NULL;
204  int numChars = asprintf( &buffer, " %-*c %s", mNesting, ':', format );
205  if( numChars >= 0 ) // No error
206  {
207  std::string message = ArgListToString( buffer, arg );
208  LogMessage( DebugInfo, message.c_str() );
209  free( buffer );
210  }
211  }
212  else
213  {
214  std::string message = ArgListToString( format, arg );
215  LogMessage( DebugInfo, message.c_str() );
216  }
217  va_end(arg);
218  }
219 }
220 
221 
222 TraceObj::TraceObj(Filter* filter, const char*format, ...) : mFilter(filter)
223 {
224  if(mFilter && mFilter->IsTraceEnabled())
225  {
226  va_list arg;
227  va_start(arg, format);
228  mMessage = ArgListToString(format, arg);
229  va_end(arg);
230 
231  LogMessage(DebugInfo, "Entr%-*c %s\n", mFilter->mNesting, ':', mMessage.c_str());
232  ++mFilter->mNesting;
233  }
234 }
235 
236 TraceObj::~TraceObj()
237 {
238  if(mFilter && mFilter->IsTraceEnabled())
239  {
240  if (mFilter->mNesting)
241  {
242  --mFilter->mNesting;
243  }
244  LogMessage(DebugInfo, "Exit%-*c %s\n", mFilter->mNesting, ':', mMessage.c_str());
245  }
246 }
247 
248 #endif // DEBUG_ENABLED
249 
250 
251 std::string ArgListToString(const char *format, va_list args)
252 {
253  std::string str; // empty string
254  if(format != NULL)
255  {
256  char* buffer = NULL;
257  int err = vasprintf(&buffer, format, args);
258  if(err >= 0) // No error
259  {
260  str = buffer;
261  free(buffer);
262  }
263  }
264  return str;
265 }
266 
267 std::string FormatToString(const char *format, ...)
268 {
269  va_list arg;
270  va_start(arg, format);
271  std::string s = ArgListToString(format, arg);
272  va_end(arg);
273  return s;
274 }
275 
276 std::string ColorToString(const Vector4& color)
277 {
278  std::ostringstream oss;
279  oss << "<R:" << color.r << " G:" << color.g << " B:" << color.b << " A:" << color.a << ">";
280  return oss.str();
281 }
282 
283 std::string Vector4ToString(const Vector4& v, size_t precision, size_t indent)
284 {
285  std::ostringstream oss;
286  oss << std::setw(indent+3) << std::setfill(' ') << std::setprecision(precision) << std::right;
287  oss << "<X:" << std::setw(precision+4) << v.x
288  << " Y:" << std::setw(precision+4) << v.y
289  << " Z:" << std::setw(precision+4) << v.z
290  << " W:" << std::setw(precision+4) << v.w << ">";
291  return oss.str();
292 }
293 
294 std::string Vector3ToString(const Vector3& v, size_t precision, size_t indent)
295 {
296  std::ostringstream oss;
297  oss << std::setw(indent+3) << std::setfill(' ') << std::setprecision(precision) << std::right << std::setiosflags(std::ios_base::fixed);
298  oss << "<X:" << std::setw(precision+4) << v.x
299  << " Y:" << std::setw(precision+4) << v.y
300  << " Z:" << std::setw(precision+4) << v.z << ">";
301  return oss.str();
302 }
303 
304 std::string QuaternionToString(const Quaternion& q, size_t precision, size_t indent)
305 {
306  std::ostringstream oss;
307 
308  Vector3 axis;
309  Radian angle;
310  q.ToAxisAngle(axis, angle);
311 
312  oss << std::setw(indent+3) << std::setfill(' ') << std::setprecision(precision) << std::right;
313  oss << "<A:" << std::setw(precision+4) << Degree( angle ).degree << ", " << Vector3ToString(axis, precision, 0) << ">";
314 
315  return oss.str();
316 }
317 
318 std::string Matrix3ToString(const Matrix3& m, size_t precision, size_t indent)
319 {
320  return Array2DToString(m.AsFloat(), 3, 3, precision, indent);
321 }
322 
323 std::string MatrixToString(const Matrix& m, size_t precision, size_t indent)
324 {
325  return Array2DToString(m.AsFloat(), 4, 4, precision, indent);
326 }
327 
328 } // namespace Log
329 
330 } // namespace Integration
331 
332 } // namespace Dali
Dali Docs Home
Read more about Dali