Make Log::LogMessage print to stderr / stdout if log function not installed 48/314048/5
authorEunki, Hong <eunkiki.hong@samsung.com>
Thu, 4 Jul 2024 10:16:18 +0000 (19:16 +0900)
committerEunki, Hong <eunkiki.hong@samsung.com>
Thu, 4 Jul 2024 11:29:15 +0000 (20:29 +0900)
Until now, we just ignore the messages when we use Dali::Integration::Log::LogMessage(Something like, DALI_LOG_ERROR macro) when we don't install log function.

The log function exist per each threads.
But it is difficult to debug something at worker thread jobs
what DALi cannot controled. (Like GC thread for dotnet Application)

For more general and each debugging, let we print 'something' even if
log function is not installed, like stderr.

For now, Tizen platform re-open the stderr / stdout as dlog.
and Linux-friendly + Windows support to print logs into stderr, stdout file.

Change-Id: Iaf6a49d8aa9f53108b4c83bbdb18df406e3b57fa
Signed-off-by: Eunki, Hong <eunkiki.hong@samsung.com>
dali/integration-api/debug.cpp
dali/integration-api/debug.h

index dab858548de927e35a6905e25ff4da8f4ee6158c..7647c224f1578a837bd044b3d6d96f276522fcaa 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -37,6 +37,39 @@ namespace Integration
 {
 namespace Log
 {
+namespace
+{
+void FormatPrintToStandardOutput(DebugPriority priority, const char* format, va_list args)
+{
+  if(format != nullptr)
+  {
+    char* buffer       = nullptr;
+    int   bufferLength = vasprintf(&buffer, format, args); // Development note : bufferLength doesn't include null-terminated character.
+
+    if(bufferLength >= 0) // No error
+    {
+      // TODO : We need to consider thread-safety way to print something.
+      switch(priority)
+      {
+        case DebugPriority::DEBUG:
+        case DebugPriority::INFO:
+        {
+          fprintf(stdout, "%.*s", bufferLength, buffer);
+          break;
+        }
+        case DebugPriority::WARNING:
+        case DebugPriority::ERROR:
+        default:
+        {
+          fprintf(stderr, "%.*s", bufferLength, buffer);
+          break;
+        }
+      }
+      free(buffer);
+    }
+  }
+}
+} // namespace
 thread_local LogFunction gthreadLocalLogFunction = nullptr;
 
 /* Forward declarations */
@@ -45,17 +78,22 @@ std::string ArgListToString(const char* format, va_list args);
 
 void LogMessage(DebugPriority priority, const char* format, ...)
 {
-  if(!gthreadLocalLogFunction)
+  if(DALI_UNLIKELY(!gthreadLocalLogFunction))
   {
-    return;
+    va_list arg;
+    va_start(arg, format);
+    FormatPrintToStandardOutput(priority, format, arg);
+    va_end(arg);
   }
+  else
+  {
+    va_list arg;
+    va_start(arg, format);
+    std::string message = ArgListToString(format, arg);
+    va_end(arg);
 
-  va_list arg;
-  va_start(arg, format);
-  std::string message = ArgListToString(format, arg);
-  va_end(arg);
-
-  gthreadLocalLogFunction(priority, message);
+    gthreadLocalLogFunction(priority, message);
+  }
 }
 
 void InstallLogFunction(const LogFunction& logFunction)
index 38a0ff0adba7b979c75fd3a87604ba2b6c7046e7..5d812586662858caca574bd723cc58c5a64464ea 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_INTEGRATION_DEBUG_H
 
 /*
- * Copyright (c) 2023 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2024 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -124,6 +124,7 @@ using LogFunction = void (*)(DebugPriority, std::string&);
  * A log function has to be installed for every thread that wants to use logging.
  * This should be done by the adaptor.
  * The log function can be different for each thread.
+ * If a log function is not installed, the log message will be print at stderr.
  * @param logFunction the log function to install
  * @param logOpts the log options to save in thread
  */