diff --git a/platform/platform.hpp b/platform/platform.hpp index 621e638cc5..b711ed1ef9 100644 --- a/platform/platform.hpp +++ b/platform/platform.hpp @@ -119,8 +119,6 @@ protected: platform::BatteryLevelTracker m_batteryTracker; - platform::Trace m_trace; - public: Platform(); virtual ~Platform() = default; @@ -342,8 +340,6 @@ public: platform::BatteryLevelTracker & GetBatteryTracker() { return m_batteryTracker; } - platform::Trace & GetTrace() { return m_trace; } - private: void RunThreads(); void ShutdownThreads(); diff --git a/platform/trace.hpp b/platform/trace.hpp index c1dcec2929..96f80e4e1f 100644 --- a/platform/trace.hpp +++ b/platform/trace.hpp @@ -14,41 +14,39 @@ class TraceImpl; class Trace { public: - Trace() noexcept; - ~Trace() noexcept; - + static Trace & Instance() noexcept; + void BeginSection(char const * name) noexcept; void EndSection() noexcept; void SetCounter(char const * name, int64_t value) noexcept; private: + Trace(); + ~Trace(); + std::unique_ptr m_impl; - + DISALLOW_COPY_AND_MOVE(Trace); }; class TraceSection { public: - inline TraceSection(Trace & trace, char const * section) noexcept - : m_trace(trace) + inline TraceSection(char const * section) noexcept { - m_trace.BeginSection(section); + Trace::Instance().BeginSection(section); } inline ~TraceSection() noexcept { - m_trace.EndSection(); + Trace::Instance().EndSection(); } - -private: - Trace & m_trace; }; } // namespace platform #ifdef ENABLE_TRACE -#define TRACE_SECTION(section) platform::TraceSection ___section(GetPlatform().GetTrace(), section) -#define TRACE_COUNTER(name, value) GetPlatform().GetTrace().SetCounter(name, value) +#define TRACE_SECTION(section) platform::TraceSection ___section(section) +#define TRACE_COUNTER(name, value) platform::Trace::Instance().SetCounter(name, value) #else #define TRACE_SECTION(section) static_cast(0) #define TRACE_COUNTER(name, value) static_cast(0) diff --git a/platform/trace_android.cpp b/platform/trace_android.cpp index e8bc2d265b..6d1b9f6b1e 100644 --- a/platform/trace_android.cpp +++ b/platform/trace_android.cpp @@ -13,7 +13,7 @@ typedef void *(*ATrace_setCounter) (char const *, int64_t); class TraceImpl { public: - TraceImpl() noexcept + TraceImpl() { m_lib = dlopen("libandroid.so", RTLD_NOW | RTLD_LOCAL); @@ -28,7 +28,7 @@ public: } } - ~TraceImpl() noexcept + ~TraceImpl() { if (m_lib != nullptr) dlclose(m_lib); @@ -59,11 +59,17 @@ private: ATrace_setCounter m_setCounter = nullptr; }; -Trace::Trace() noexcept +// static +Trace & Trace::Instance() noexcept { + static Trace instance; + return instance; +} + +Trace::Trace() : m_impl(std::make_unique()) {} -Trace::~Trace() noexcept = default; +Trace::~Trace() = default; void Trace::BeginSection(char const * name) noexcept { diff --git a/platform/trace_empty.cpp b/platform/trace_empty.cpp index 1b6e88dca9..314acb2b3f 100644 --- a/platform/trace_empty.cpp +++ b/platform/trace_empty.cpp @@ -4,9 +4,9 @@ namespace platform { class TraceImpl {}; -Trace::Trace() noexcept = default; +Trace::Trace() = default; -Trace::~Trace() noexcept = default; +Trace::~Trace() = default; void Trace::BeginSection(char const * name) noexcept {}