Skip to content

Commit

Permalink
add vendors/spdlog
Browse files Browse the repository at this point in the history
  • Loading branch information
ingangi committed May 16, 2019
1 parent 19b8509 commit f7c9a9a
Show file tree
Hide file tree
Showing 62 changed files with 15,489 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

*.json
*.log
*.o
*.a
.vscode/
Expand Down
4 changes: 2 additions & 2 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ LD = @echo " ld $@"; ld
AR = @echo " ar $@"; ar
RM = @echo " RM $@"; rm -f

CFLAGS += -Wall -std=c++11
CFLAGS += -Wall -std=c++11 #-Wextra -pedantic -Wconversion
CFLAGS += -g3

#LDFLAGS += "-Wl" -lpthread -lc
LDFLAGS += -L. -lpthread ##-lenc -lsyslink -lbinder -llog -losa -lpdi -lsdk -lz -lrt -lsplice -lmem -ldec
AFLAGS += -r

INC = -I../engin -I../util #-I./rpc
INC = -I../engin -I../util -I../vendors #-I./rpc

BINDIR = .

Expand Down
2 changes: 1 addition & 1 deletion examples/channel_example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LDFLAGS += -lpthread #-lc
LDFLAGS += -L. -Wl,--no-as-needed ##-lenc -lsyslink -lbinder -llog -losa -lpdi -lsdk -lz -lrt -lsplice -lmem -ldec
AFLAGS += -r

INC = -I../../engin -I../../util
INC = -I../../engin -I../../util -I../../vendors

BINDIR = .
SRCDIR = . ../../engin ../../util
Expand Down
2 changes: 1 addition & 1 deletion examples/chutex_example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LDFLAGS += -lpthread #-lc
LDFLAGS += -L. -Wl,--no-as-needed ##-lenc -lsyslink -lbinder -llog -losa -lpdi -lsdk -lz -lrt -lsplice -lmem -ldec
AFLAGS += -r

INC = -I../../engin -I../../util
INC = -I../../engin -I../../util -I../../vendors

BINDIR = .
SRCDIR = . ../../engin ../../util
Expand Down
2 changes: 1 addition & 1 deletion examples/http_client_example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LDFLAGS += -lcurl
LDFLAGS += -L. ##-lenc -lsyslink -lbinder -llog -losa -lpdi -lsdk -lz -lrt -lsplice -lmem -ldec
AFLAGS += -r

INC = -I../../engin -I../../net/http_client -I../../util
INC = -I../../engin -I../../net/http_client -I../../util -I../../vendors

BINDIR = .
SRCDIR = . ../../engin ../../net/http_client ../../util
Expand Down
2 changes: 1 addition & 1 deletion examples/rpc_example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CFLAGS += -g3
LDFLAGS += -L. -lpthread -lprotobuf -lgrpc++ -lgrpc
AFLAGS += -r

INC = -I../../engin -I../../net/rpc -I../../net/proto_code -I../../util
INC = -I../../engin -I../../net/rpc -I../../net/proto_code -I../../util -I../../vendors

BINDIR = .

Expand Down
2 changes: 1 addition & 1 deletion examples/rpc_example/test_client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CFLAGS += -g3
LDFLAGS += -L. -lpthread -lprotobuf -lgrpc++ -lgrpc
AFLAGS += -r

INC = -I../../../engin -I../../../net/rpc -I../../../net/proto_code -I../../../util
INC = -I../../../engin -I../../../net/rpc -I../../../net/proto_code -I../../../util -I../../../vendors

BINDIR = .

Expand Down
2 changes: 1 addition & 1 deletion examples/timer_example/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ LDFLAGS += -lpthread #-lc
LDFLAGS += -L. -Wl,--no-as-needed ##-lenc -lsyslink -lbinder -llog -losa -lpdi -lsdk -lz -lrt -lsplice -lmem -ldec
AFLAGS += -r

INC = -I../../engin -I../../util
INC = -I../../engin -I../../util -I../../vendors

BINDIR = .
SRCDIR = . ../../engin ../../util
Expand Down
32 changes: 28 additions & 4 deletions util/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,40 @@

#include <iostream>
#include <stdarg.h>
#include "spdlog/spdlog.h"
#include "spdlog/async.h"
#include "spdlog/sinks/daily_file_sink.h"

#define MAX_LOG_LEN 512

#define TRACE ::spdlog::level::trace
#define DEBUG ::spdlog::level::debug
#define INFO ::spdlog::level::info
#define WARN ::spdlog::level::warn
#define ERROR ::spdlog::level::err
#define CRITICAL ::spdlog::level::critical
#define OFF ::spdlog::level::off


namespace chr {


class logger_t
{
const int MAX_LOG_LEN = 512;
private:
logger_t(){}
logger_t(){
_async_file = ::spdlog::daily_logger_mt<::spdlog::async_factory_nonblock>("ENGINE", "ENGINE.log", 0, 0);
}

public:
static logger_t &instance() {
static logger_t instance;
return instance;
}
~logger_t(){}
~logger_t(){
::spdlog::shutdown();
::spdlog::drop_all();
}

template <typename T> logger_t& operator<<(const T& value) {
m_stream << value;
Expand All @@ -49,10 +68,15 @@ class logger_t
}

private:
std::ostream &m_stream = std::cout; //replace with your log lib
std::ostream &m_stream = std::cout; //consol

public:
std::shared_ptr<::spdlog::logger> _async_file;
};

}

#define LOG chr::logger_t::instance()
#define SPDLOG(level, format, ...) {chr::logger_t::instance()._async_file->log(level, format, ##__VA_ARGS__);}

#endif
87 changes: 87 additions & 0 deletions vendors/spdlog/async.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@

//
// Copyright(c) 2018 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//

#pragma once

//
// Async logging using global thread pool
// All loggers created here share same global thread pool.
// Each log message is pushed to a queue along withe a shared pointer to the
// logger.
// If a logger deleted while having pending messages in the queue, it's actual
// destruction will defer
// until all its messages are processed by the thread pool.
// This is because each message in the queue holds a shared_ptr to the
// originating logger.

#include "spdlog/async_logger.h"
#include "spdlog/details/registry.h"
#include "spdlog/details/thread_pool.h"

#include <memory>
#include <mutex>

namespace spdlog {

namespace details {
static const size_t default_async_q_size = 8192;
}

// async logger factory - creates async loggers backed with thread pool.
// if a global thread pool doesn't already exist, create it with default queue
// size of 8192 items and single thread.
template<async_overflow_policy OverflowPolicy = async_overflow_policy::block>
struct async_factory_impl
{
template<typename Sink, typename... SinkArgs>
static std::shared_ptr<async_logger> create(std::string logger_name, SinkArgs &&... args)
{
auto &registry_inst = details::registry::instance();

// create global thread pool if not already exists..
std::lock_guard<std::recursive_mutex> tp_lock(registry_inst.tp_mutex());
auto tp = registry_inst.get_tp();
if (tp == nullptr)
{
tp = std::make_shared<details::thread_pool>(details::default_async_q_size, 1);
registry_inst.set_tp(tp);
}

auto sink = std::make_shared<Sink>(std::forward<SinkArgs>(args)...);
auto new_logger = std::make_shared<async_logger>(std::move(logger_name), std::move(sink), std::move(tp), OverflowPolicy);
registry_inst.initialize_logger(new_logger);
return new_logger;
}
};

using async_factory = async_factory_impl<async_overflow_policy::block>;
using async_factory_nonblock = async_factory_impl<async_overflow_policy::overrun_oldest>;

template<typename Sink, typename... SinkArgs>
inline std::shared_ptr<spdlog::logger> create_async(std::string logger_name, SinkArgs &&... sink_args)
{
return async_factory::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
}

template<typename Sink, typename... SinkArgs>
inline std::shared_ptr<spdlog::logger> create_async_nb(std::string logger_name, SinkArgs &&... sink_args)
{
return async_factory_nonblock::create<Sink>(std::move(logger_name), std::forward<SinkArgs>(sink_args)...);
}

// set global thread pool.
inline void init_thread_pool(size_t q_size, size_t thread_count)
{
auto tp = std::make_shared<details::thread_pool>(q_size, thread_count);
details::registry::instance().set_tp(std::move(tp));
}

// get the global thread pool.
inline std::shared_ptr<spdlog::details::thread_pool> thread_pool()
{
return details::registry::instance().get_tp();
}
} // namespace spdlog
73 changes: 73 additions & 0 deletions vendors/spdlog/async_logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//

#pragma once

// Very fast asynchronous logger (millions of logs per second on an average
// desktop)
// Uses pre allocated lockfree queue for maximum throughput even under large
// number of threads.
// Creates a single back thread to pop messages from the queue and log them.
//
// Upon each log write the logger:
// 1. Checks if its log level is enough to log the message
// 2. Push a new copy of the message to a queue (or block the caller until
// space is available in the queue)
// 3. will throw spdlog_ex upon log exceptions
// Upon destruction, logs all remaining messages in the queue before
// destructing..

#include "spdlog/common.h"
#include "spdlog/logger.h"

#include <chrono>
#include <memory>
#include <string>

namespace spdlog {

// Async overflow policy - block by default.
enum class async_overflow_policy
{
block, // Block until message can be enqueued
overrun_oldest // Discard oldest message in the queue if full when trying to
// add new item.
};

namespace details {
class thread_pool;
}

class async_logger final : public std::enable_shared_from_this<async_logger>, public logger
{
friend class details::thread_pool;

public:
template<typename It>
async_logger(std::string logger_name, It begin, It end, std::weak_ptr<details::thread_pool> tp,
async_overflow_policy overflow_policy = async_overflow_policy::block);

async_logger(std::string logger_name, sinks_init_list sinks_list, std::weak_ptr<details::thread_pool> tp,
async_overflow_policy overflow_policy = async_overflow_policy::block);

async_logger(std::string logger_name, sink_ptr single_sink, std::weak_ptr<details::thread_pool> tp,
async_overflow_policy overflow_policy = async_overflow_policy::block);

std::shared_ptr<logger> clone(std::string new_name) override;

protected:
void sink_it_(details::log_msg &msg) override;
void flush_() override;

void backend_log_(const details::log_msg &incoming_log_msg);
void backend_flush_();

private:
std::weak_ptr<details::thread_pool> thread_pool_;
async_overflow_policy overflow_policy_;
};
} // namespace spdlog

#include "details/async_logger_impl.h"
Loading

0 comments on commit f7c9a9a

Please sign in to comment.