diff --git a/include/rtlog/Formatter.hpp b/include/rtlog/Formatter.hpp index 97c4e48..27ec36c 100644 --- a/include/rtlog/Formatter.hpp +++ b/include/rtlog/Formatter.hpp @@ -44,7 +44,8 @@ class CFormatterT { return m_Writer.c_str(); } /** Performs a message formatting and return internal pointer */ - const char_type* format(moodycamel::ConcurrentQueue& queue) + template + const char_type* format(moodycamel::ConcurrentQueue& queue) { if (m_MessageCompleted) { m_Writer.clear(); diff --git a/include/rtlog/rtlog.hpp b/include/rtlog/rtlog.hpp index 1ea253b..b748061 100644 --- a/include/rtlog/rtlog.hpp +++ b/include/rtlog/rtlog.hpp @@ -24,12 +24,18 @@ namespace rtlog */ void initialize(std::string const & log_directory, std::string const & log_file_name, uint32_t log_file_roll_size_mb); -template -class CLoggerT : public Singleton> +struct ConcurrentQueueTraits : public moodycamel::ConcurrentQueueDefaultTraits +{ + static const size_t BLOCK_SIZE = 32; + static const size_t MAX_SUBQUEUE_SIZE = 64; +}; + +template +class CLoggerT : public Singleton> { static_assert(PARAM_SIZE > 7); - friend class Singleton>; - friend class std::default_delete>; + friend class Singleton>; + friend class std::default_delete>; // Cannot access thread id private member and need an ostream to format thread::id // so just revert to pthread_t @@ -74,17 +80,17 @@ class CLoggerT : public Singleton> ); } - moodycamel::ConcurrentQueue& arg_holders; + moodycamel::ConcurrentQueue& arg_holders; protected: - CLoggerT(moodycamel::ConcurrentQueue& queue) : arg_holders(queue) {} + CLoggerT(moodycamel::ConcurrentQueue& queue) : arg_holders(queue) {} ~CLoggerT() {} private: }; /** Default logger */ -using CLogger = CLoggerT<8, 1024, char>; +using CLogger = CLoggerT<8, 1024, char, ConcurrentQueueTraits>; inline bool is_logged(LogLevel level); } // namespace rtlog diff --git a/src/test.cpp b/src/test.cpp index cf441c2..297160f 100644 --- a/src/test.cpp +++ b/src/test.cpp @@ -18,22 +18,22 @@ bool is_logged(LogLevel level) } // namespace rtlog - -class CLogConsumer +template +class CLogConsumerT { protected: - moodycamel::ConcurrentQueue& m_Queue; + moodycamel::ConcurrentQueue& m_Queue; std::chrono::microseconds m_PollInterval; rtlog::CFormatter m_Formatter; std::thread m_ConsumerThread; std::atomic_bool m_Stop; public: - CLogConsumer(moodycamel::ConcurrentQueue& queue, uint32_t poll_interval) : + CLogConsumerT(moodycamel::ConcurrentQueue& queue, uint32_t poll_interval) : m_Queue(queue), m_PollInterval(poll_interval) { m_Stop.store(false); - m_ConsumerThread = std::thread(std::bind(&CLogConsumer::consume, this)); + m_ConsumerThread = std::thread(std::bind(&CLogConsumerT::consume, this)); } void consume() @@ -57,11 +57,11 @@ class CLogConsumer m_ConsumerThread.join(); } }; - +using CLogConsumer = CLogConsumerT; int main(int argc, char* argv[]) { - moodycamel::ConcurrentQueue main_queue; + moodycamel::ConcurrentQueue main_queue; rtlog::CFormatter formatter; rtlog::CLogger::initialize(main_queue); @@ -74,7 +74,7 @@ int main(int argc, char* argv[]) for (int i{0}; i < 100; i++) { for (int j{0}; j < 10; j++) { - LOG_INFO("asd", (i*10)+j); + LOG_INFO("User message", (i*10)+j); std::this_thread::sleep_for( std::chrono::microseconds(uniform_dist(e1)) );