forked from taichi-dev/taichi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
663f807
commit ee2c484
Showing
40 changed files
with
22,362 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// 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 <functional> | ||
#include <string> | ||
#include <memory> | ||
|
||
namespace spdlog | ||
{ | ||
|
||
namespace details | ||
{ | ||
class async_log_helper; | ||
} | ||
|
||
class async_logger SPDLOG_FINAL :public logger | ||
{ | ||
public: | ||
template<class It> | ||
async_logger(const std::string& name, | ||
const It& begin, | ||
const It& end, | ||
size_t queue_size, | ||
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, | ||
const std::function<void()>& worker_warmup_cb = nullptr, | ||
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), | ||
const std::function<void()>& worker_teardown_cb = nullptr); | ||
|
||
async_logger(const std::string& logger_name, | ||
sinks_init_list sinks, | ||
size_t queue_size, | ||
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, | ||
const std::function<void()>& worker_warmup_cb = nullptr, | ||
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), | ||
const std::function<void()>& worker_teardown_cb = nullptr); | ||
|
||
async_logger(const std::string& logger_name, | ||
sink_ptr single_sink, | ||
size_t queue_size, | ||
const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, | ||
const std::function<void()>& worker_warmup_cb = nullptr, | ||
const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), | ||
const std::function<void()>& worker_teardown_cb = nullptr); | ||
|
||
//Wait for the queue to be empty, and flush synchronously | ||
//Warning: this can potentially last forever as we wait it to complete | ||
void flush() override; | ||
|
||
// Error handler | ||
virtual void set_error_handler(log_err_handler) override; | ||
virtual log_err_handler error_handler() override; | ||
|
||
protected: | ||
void _sink_it(details::log_msg& msg) override; | ||
void _set_formatter(spdlog::formatter_ptr msg_formatter) override; | ||
void _set_pattern(const std::string& pattern, pattern_time_type pattern_time) override; | ||
|
||
private: | ||
std::unique_ptr<details::async_log_helper> _async_log_helper; | ||
}; | ||
} | ||
|
||
|
||
#include "spdlog/details/async_logger_impl.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// | ||
// Copyright(c) 2015 Gabi Melman. | ||
// Distributed under the MIT License (http://opensource.org/licenses/MIT) | ||
// | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <initializer_list> | ||
#include <chrono> | ||
#include <memory> | ||
#include <atomic> | ||
#include <exception> | ||
#include<functional> | ||
|
||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) | ||
#include <codecvt> | ||
#include <locale> | ||
#endif | ||
|
||
#include "spdlog/details/null_mutex.h" | ||
|
||
//visual studio upto 2013 does not support noexcept nor constexpr | ||
#if defined(_MSC_VER) && (_MSC_VER < 1900) | ||
#define SPDLOG_NOEXCEPT throw() | ||
#define SPDLOG_CONSTEXPR | ||
#else | ||
#define SPDLOG_NOEXCEPT noexcept | ||
#define SPDLOG_CONSTEXPR constexpr | ||
#endif | ||
|
||
// See tweakme.h | ||
#if !defined(SPDLOG_FINAL) | ||
#define SPDLOG_FINAL | ||
#endif | ||
|
||
#if defined(__GNUC__) || defined(__clang__) | ||
#define SPDLOG_DEPRECATED __attribute__((deprecated)) | ||
#elif defined(_MSC_VER) | ||
#define SPDLOG_DEPRECATED __declspec(deprecated) | ||
#else | ||
#define SPDLOG_DEPRECATED | ||
#endif | ||
|
||
|
||
#include "spdlog/fmt/fmt.h" | ||
|
||
namespace spdlog | ||
{ | ||
|
||
class formatter; | ||
|
||
namespace sinks | ||
{ | ||
class sink; | ||
} | ||
|
||
using log_clock = std::chrono::system_clock; | ||
using sink_ptr = std::shared_ptr < sinks::sink >; | ||
using sinks_init_list = std::initializer_list < sink_ptr >; | ||
using formatter_ptr = std::shared_ptr<spdlog::formatter>; | ||
#if defined(SPDLOG_NO_ATOMIC_LEVELS) | ||
using level_t = details::null_atomic_int; | ||
#else | ||
using level_t = std::atomic<int>; | ||
#endif | ||
|
||
using log_err_handler = std::function<void(const std::string &err_msg)>; | ||
|
||
//Log level enum | ||
namespace level | ||
{ | ||
typedef enum | ||
{ | ||
trace = 0, | ||
debug = 1, | ||
info = 2, | ||
warn = 3, | ||
err = 4, | ||
critical = 5, | ||
off = 6 | ||
} level_enum; | ||
|
||
#if !defined(SPDLOG_LEVEL_NAMES) | ||
#define SPDLOG_LEVEL_NAMES { "trace", "debug", "info", "warning", "error", "critical", "off" }; | ||
#endif | ||
static const char* level_names[] SPDLOG_LEVEL_NAMES | ||
|
||
static const char* short_level_names[] { "T", "D", "I", "W", "E", "C", "O" }; | ||
|
||
inline const char* to_str(spdlog::level::level_enum l) | ||
{ | ||
return level_names[l]; | ||
} | ||
|
||
inline const char* to_short_str(spdlog::level::level_enum l) | ||
{ | ||
return short_level_names[l]; | ||
} | ||
} //level | ||
|
||
|
||
// | ||
// Async overflow policy - block by default. | ||
// | ||
enum class async_overflow_policy | ||
{ | ||
block_retry, // Block / yield / sleep until message can be enqueued | ||
discard_log_msg // Discard the message it enqueue fails | ||
}; | ||
|
||
// | ||
// Pattern time - specific time getting to use for pattern_formatter. | ||
// local time by default | ||
// | ||
enum class pattern_time_type | ||
{ | ||
local, // log localtime | ||
utc // log utc | ||
}; | ||
|
||
// | ||
// Log exception | ||
// | ||
namespace details | ||
{ | ||
namespace os | ||
{ | ||
std::string errno_str(int err_num); | ||
} | ||
} | ||
class spdlog_ex: public std::exception | ||
{ | ||
public: | ||
spdlog_ex(const std::string& msg):_msg(msg) | ||
{} | ||
spdlog_ex(const std::string& msg, int last_errno) | ||
{ | ||
_msg = msg + ": " + details::os::errno_str(last_errno); | ||
} | ||
const char* what() const SPDLOG_NOEXCEPT override | ||
{ | ||
return _msg.c_str(); | ||
} | ||
private: | ||
std::string _msg; | ||
|
||
}; | ||
|
||
// | ||
// wchar support for windows file names (SPDLOG_WCHAR_FILENAMES must be defined) | ||
// | ||
#if defined(_WIN32) && defined(SPDLOG_WCHAR_FILENAMES) | ||
using filename_t = std::wstring; | ||
#else | ||
using filename_t = std::string; | ||
#endif | ||
|
||
|
||
} //spdlog |
Oops, something went wrong.