Add log prints for debugging
[platform/core/appfw/app-core.git] / tizen-cpp / app-core-cpp / app_core_base.cc
index 9b41cb0..aa55b1d 100644 (file)
@@ -22,7 +22,6 @@
 #include <bundle_internal.h>
 #include <dlfcn.h>
 #include <errno.h>
-#include <execinfo.h>
 #include <gio/gio.h>
 #include <glib-unix.h>
 #include <glib.h>
 #include <vector>
 
 #include "app-core-cpp/app_core_plugin_private.hh"
+#include "app-core-cpp/exit_handler_private.hh"
+#include "app-core-cpp/sigterm_handler_private.hh"
 #include "common/glib_private.hh"
 #include "common/log_private.hh"
-
-#define SIGRTANR (SIGRTMIN + 3)
-#undef _ERR
-#define _ERR(fmt, ...) fprintf(stderr, fmt "\n", ##__VA_ARGS__)
+#include "common/log_tracer.hh"
 
 extern "C" void aul_finalize();
 namespace tizen_cpp {
@@ -66,86 +64,8 @@ AppCoreBase* AppCoreBase::context_ = nullptr;
 
 namespace {
 
-constexpr const int BACKTRACE_BUFFER_SIZE = 128;
-
-void PrintBacktrace() {
-  char* buffer[BACKTRACE_BUFFER_SIZE];
-  int nptrs = backtrace(reinterpret_cast<void**>(buffer),
-      BACKTRACE_BUFFER_SIZE);
-  _ERR("backtrace() returned %d addresses", nptrs - 4);
-
-  // To print numbers, we use backtrace_symbols() instead of backtrace_symbols_fd().
-  // If there is an issues related to the memory, we cannot use backtrace_symbols().
-  // backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
-  char** strings = backtrace_symbols(reinterpret_cast<void**>(buffer), nptrs);
-  if (strings == nullptr) {
-    perror("backtrace_symbols");
-    return;
-  }
-
-  Dl_info info;
-  for (int i = 4; i < nptrs; ++i) {
-    dladdr(buffer[i], &info);
-    _ERR("[%3d] %s %s",
-        i - 4, info.dli_sname ? info.dli_sname : "?", strings[i]);
-  }
-  free(strings);
-}
-
-class ExitHandler {
- public:
-  ExitHandler() {
-    on_exit(OnExit, this);
-  }
-
- private:
-  static void OnExit(int status, void *user_data) {
-    if (status != 127)
-      return;
-
-    PrintBacktrace();
-
-    _ERR("%s(%d) abort()", __FUNCTION__, __LINE__);
-    abort();
-  }
-};
-
-ExitHandler exit_handler;
-
-class AnrMonitor {
- public:
-  AnrMonitor() {
-    struct sigaction action;
-    memset(&action, '\0', sizeof(action));
-    sigemptyset(&action.sa_mask);
-    action.sa_flags = SA_RESTART;
-    action.sa_handler = SignalHandler;
-
-    if (sigaction(SIGRTANR, &action, &old_action_) != 0)
-      _E("sigaction() is failed. errno(%d)", errno);
-  }
-
-  ~AnrMonitor() {
-    if (sigaction(SIGRTANR, &old_action_, nullptr) != 0)
-      _W("sigaction() is failed. errno(%d)", errno);
-  }
-
- private:
-  static void SignalHandler(int signo) {
-    static unsigned int count;
-    _ERR("===================================================================");
-    _ERR("=================== Application Not Responding ====================");
-    PrintBacktrace();
-    _ERR("============== Application did not respond %d times ===============",
-        ++count);
-    _ERR("===================================================================");
-  }
-
- private:
-  struct sigaction old_action_;
-};
-
-AnrMonitor anr_monitor;
+internal::ExitHandler exit_handler;
+internal::SigtermHandler sigterm_handler;
 
 enum TizenProfile {
   Unknown = 0x00,
@@ -273,6 +193,7 @@ class AppCoreBase::Impl {
   static void ReceiveSuspendSignalCb(GDBusConnection*, const gchar*,
       const gchar*, const gchar*, const gchar*, GVariant*, gpointer);
   static void OnLowBatteryCb(keynode_t* key, void* data);
+  static void OnTimeZoneChangedCb(keynode_t* key, void* data);
   static void LockCb(keynode_t* node, void* user_data);
   static void AutoRotationChangedCb(sensor_t sensor, unsigned int event_type,
       sensor_data_t* data, void* user_data);
@@ -835,6 +756,21 @@ void AppCoreBase::Impl::OnLowBatteryCb(keynode_t* key, void* data) {
   }
 }
 
+void AppCoreBase::Impl::OnTimeZoneChangedCb(keynode_t* key, void* data) {
+  char* time_zone_id = vconf_keynode_get_str(key);
+  if (time_zone_id == nullptr) {
+    return;
+  }
+
+  char* time_zone = vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_INT);
+  if (time_zone != nullptr) {
+    AppCoreBase* base = reinterpret_cast<AppCoreBase*>(data);
+    base->impl_->InvokeCallback(std::string(time_zone) + "|" + time_zone_id,
+        IEvent::Type::TIME_ZONE_CHANGED);
+    free(time_zone);
+  }
+}
+
 void AppCoreBase::Impl::UnregisterRotationChangedEvent() {
   if (!__rotation.ref)
     return;
@@ -876,6 +812,10 @@ int AppCoreBase::OnSetEvent(IEvent::Type event) {
     break;
   case IEvent::Type::SUSPENDED_STATE_CHANGE:
     break;
+  case IEvent::Type::TIME_ZONE_CHANGED:
+    vconf_notify_key_changed(VCONFKEY_SETAPPL_TIMEZONE_ID,
+        impl_->OnTimeZoneChangedCb, this);
+    break;
   default:
     break;
   }
@@ -1270,12 +1210,14 @@ void AppCoreBase::Run(int argc, char** argv) {
 }
 
 void AppCoreBase::Init(int argc, char** argv) {
+  LogTracer tracer("AppCoreBase::Init()");
   impl_->tid_ = 0;
   impl_->suspended_state_ = false;
   impl_->allowed_bg_ = false;
   impl_->argc_ = argc;
   impl_->argv_ = argv;
   traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:OPS_INIT");
+  sigterm_handler.Restore();
   if (impl_->loop_delegator_)
     impl_->loop_delegator_->OnLoopInit(argc, argv);
   else
@@ -1351,6 +1293,7 @@ void AppCoreBase::Init(int argc, char** argv) {
 }
 
 void AppCoreBase::Fini() {
+  LogTracer tracer("AppCoreBase::Fini()");
   if (impl_->signal_handler_source_) {
     g_source_remove(impl_->signal_handler_source_);
     impl_->signal_handler_source_ = 0;