#pragma once // std #include namespace osdev { namespace components { namespace mqtt { /** * @brief Set the current thread context * The context is restored to the previous context when this object goes out of scope. * @note This object is meat to be used on the stack. */ class ThreadContextScope { public: /** * @brief Construct a scoped object that sets the current thread context. * @param context The context that will be used by the loggin framework. */ explicit ThreadContextScope( const std::string &context ); ~ThreadContextScope(); // Non copyable and non movable ThreadContextScope( const ThreadContextScope& ) = delete; ThreadContextScope& operator=( const ThreadContextScope& ) = delete; ThreadContextScope( ThreadContextScope&& ) = delete; ThreadContextScope& operator=( ThreadContextScope&& ) = delete; private: std::string m_previousContext; ///< Copy of the previous context. }; /** * @brief Add context to a thread. * For every thread only one specific instance of this object will exist. * @note Contexts can only be set by using a ThreadContextScope object. */ class ThreadContext { // Contexts can only be set by using the ThreadContextScope object. friend class ThreadContextScope; public: static ThreadContext& instance(); const std::string& context() const { return m_context; } private: /** * @brief Set the thread context. */ void setContext( const std::string &contextString ) { m_context = contextString; } /** * Construct a ThreadContext object. * The context is set to "default" */ ThreadContext(); // Non copyable and non moveable ThreadContext(const ThreadContext&) = delete; ThreadContext& operator=(const ThreadContext&) = delete; ThreadContext(ThreadContext&&) = delete; ThreadContext& operator=(ThreadContext&&) = delete; std::string m_context; ///< The context string }; } // End namespace mqtt } // End namespace components } // End namespace osdev