Skip to content

Commit

Permalink
Added a specialized queue trait and support non-default queues
Browse files Browse the repository at this point in the history
  • Loading branch information
Giuseppe Corbelli committed Aug 22, 2017
1 parent 9490919 commit 29e29eb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
3 changes: 2 additions & 1 deletion include/rtlog/Formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<rtlog::Argument>& queue)
template<typename QUEUE_TRAITS>
const char_type* format(moodycamel::ConcurrentQueue<rtlog::Argument, QUEUE_TRAITS>& queue)
{
if (m_MessageCompleted) {
m_Writer.clear();
Expand Down
20 changes: 13 additions & 7 deletions include/rtlog/rtlog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::size_t PARAM_SIZE, std::size_t BUFFER_SIZE, typename CHAR_TYPE>
class CLoggerT : public Singleton<CLoggerT<PARAM_SIZE, BUFFER_SIZE, CHAR_TYPE>>
struct ConcurrentQueueTraits : public moodycamel::ConcurrentQueueDefaultTraits
{
static const size_t BLOCK_SIZE = 32;
static const size_t MAX_SUBQUEUE_SIZE = 64;
};

template<std::size_t PARAM_SIZE, std::size_t BUFFER_SIZE, typename CHAR_TYPE, typename QUEUE_TRAITS>
class CLoggerT : public Singleton<CLoggerT<PARAM_SIZE, BUFFER_SIZE, CHAR_TYPE, QUEUE_TRAITS>>
{
static_assert(PARAM_SIZE > 7);
friend class Singleton<CLoggerT<PARAM_SIZE, BUFFER_SIZE, CHAR_TYPE>>;
friend class std::default_delete<CLoggerT<PARAM_SIZE, BUFFER_SIZE, CHAR_TYPE>>;
friend class Singleton<CLoggerT<PARAM_SIZE, BUFFER_SIZE, CHAR_TYPE, QUEUE_TRAITS>>;
friend class std::default_delete<CLoggerT<PARAM_SIZE, BUFFER_SIZE, CHAR_TYPE, QUEUE_TRAITS>>;

// Cannot access thread id private member and need an ostream to format thread::id
// so just revert to pthread_t
Expand Down Expand Up @@ -74,17 +80,17 @@ class CLoggerT : public Singleton<CLoggerT<PARAM_SIZE, BUFFER_SIZE, CHAR_TYPE>>
);
}

moodycamel::ConcurrentQueue<rtlog::Argument>& arg_holders;
moodycamel::ConcurrentQueue<rtlog::Argument, QUEUE_TRAITS>& arg_holders;

protected:
CLoggerT(moodycamel::ConcurrentQueue<rtlog::Argument>& queue) : arg_holders(queue) {}
CLoggerT(moodycamel::ConcurrentQueue<rtlog::Argument, QUEUE_TRAITS>& 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
Expand Down
16 changes: 8 additions & 8 deletions src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ bool is_logged(LogLevel level)
} // namespace rtlog



class CLogConsumer
template<typename QUEUE_TRAITS>
class CLogConsumerT
{
protected:
moodycamel::ConcurrentQueue<rtlog::Argument>& m_Queue;
moodycamel::ConcurrentQueue<rtlog::Argument, QUEUE_TRAITS>& m_Queue;
std::chrono::microseconds m_PollInterval;
rtlog::CFormatter m_Formatter;
std::thread m_ConsumerThread;
std::atomic_bool m_Stop;

public:
CLogConsumer(moodycamel::ConcurrentQueue<rtlog::Argument>& queue, uint32_t poll_interval) :
CLogConsumerT(moodycamel::ConcurrentQueue<rtlog::Argument, QUEUE_TRAITS>& 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<QUEUE_TRAITS>::consume, this));
}

void consume()
Expand All @@ -57,11 +57,11 @@ class CLogConsumer
m_ConsumerThread.join();
}
};

using CLogConsumer = CLogConsumerT<rtlog::ConcurrentQueueTraits>;

int main(int argc, char* argv[])
{
moodycamel::ConcurrentQueue<rtlog::Argument> main_queue;
moodycamel::ConcurrentQueue<rtlog::Argument, rtlog::ConcurrentQueueTraits> main_queue;
rtlog::CFormatter formatter;

rtlog::CLogger::initialize(main_queue);
Expand All @@ -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))
);
Expand Down

0 comments on commit 29e29eb

Please sign in to comment.