1
0
mirror of https://github.com/gabime/spdlog.git synced 2025-04-01 02:42:41 +08:00

Fixed linux port of v1.x

This commit is contained in:
gabime 2018-04-20 13:20:19 +03:00
parent c80cc3306f
commit c962c88342
18 changed files with 821 additions and 841 deletions

View File

@ -7,6 +7,7 @@
// bench.cpp : spdlog benchmarks // bench.cpp : spdlog benchmarks
// //
#include "spdlog/async.h" #include "spdlog/async.h"
#include "spdlog/sinks/file_sinks.h"
#include "spdlog/sinks/null_sink.h" #include "spdlog/sinks/null_sink.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"

View File

@ -1,4 +1,4 @@
// //
// Copyright(c) 2015 Gabi Melman. // Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT) // Distributed under the MIT License (http://opensource.org/licenses/MIT)
// //
@ -10,9 +10,8 @@
#define SPDLOG_TRACE_ON #define SPDLOG_TRACE_ON
#define SPDLOG_DEBUG_ON #define SPDLOG_DEBUG_ON
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/sinks/file_sinks.h" #include "spdlog/sinks/file_sinks.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include <iostream> #include <iostream>
#include <memory> #include <memory>
@ -80,7 +79,6 @@ int main(int, char *[])
// Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous.. // Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous..
async_example(); async_example();
// Log user-defined types example // Log user-defined types example
user_defined_example(); user_defined_example();
@ -117,7 +115,7 @@ void async_example()
// syslog example (linux/osx/freebsd) // syslog example (linux/osx/freebsd)
#ifndef _WIN32 #ifndef _WIN32
#incude "spdlog/sinks/syslog_sink.h" #include "spdlog/sinks/syslog_sink.h"
void syslog_example() void syslog_example()
{ {
std::string ident = "spdlog-example"; std::string ident = "spdlog-example";

View File

@ -5,9 +5,6 @@
#pragma once #pragma once
#include "../logger.h"
#include "../sinks/stdout_sinks.h"
#include <memory> #include <memory>
#include <string> #include <string>

View File

@ -13,11 +13,11 @@
#include <cstring> #include <cstring>
#include <ctime> #include <ctime>
#include <functional> #include <functional>
#include <mutex>
#include <string> #include <string>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <thread> #include <thread>
#include <mutex>
#ifdef _WIN32 #ifdef _WIN32
@ -476,18 +476,6 @@ inline bool in_terminal(FILE *file)
#endif #endif
} }
// stdout/stderr global mutexes
inline std::mutex& stdout_mutex()
{
static std::mutex &mutex = std::mutex{};
return mutex;
}
inline std::mutex& stderr_mutex()
{
static std::mutex &mutex = std::mutex{};
return mutex;
}
} // namespace os } // namespace os
} // namespace details } // namespace details
} // namespace spdlog } // namespace spdlog

View File

@ -157,7 +157,6 @@ public:
std::lock_guard<Mutex> lock(_tp_mutex); std::lock_guard<Mutex> lock(_tp_mutex);
_tp.reset(); _tp.reset();
} }
} }
Mutex &tp_mutex() Mutex &tp_mutex()

View File

@ -9,17 +9,29 @@ namespace spdlog {
namespace details { namespace details {
struct console_stdout_trait struct console_stdout_trait
{ {
static FILE* stream() {return stdout;} static FILE *stream()
{
return stdout;
}
#ifdef _WIN32 #ifdef _WIN32
static HANDLE handle() { return ::GetStdHandle(STD_OUTPUT_HANDLE); } static HANDLE handle()
{
return ::GetStdHandle(STD_OUTPUT_HANDLE);
}
#endif #endif
}; };
struct console_stderr_trait struct console_stderr_trait
{ {
static FILE* stream() { return stdout; } static FILE *stream()
{
return stdout;
}
#ifdef _WIN32 #ifdef _WIN32
static HANDLE handle() { return ::GetStdHandle(STD_ERROR_HANDLE); } static HANDLE handle()
{
return ::GetStdHandle(STD_ERROR_HANDLE);
}
#endif #endif
}; };
@ -28,7 +40,7 @@ namespace spdlog {
using mutex_t = std::mutex; using mutex_t = std::mutex;
static mutex_t &console_mutex() static mutex_t &console_mutex()
{ {
static auto &mutex = mutex_t{}; static mutex_t mutex;
return mutex; return mutex;
} }
}; };
@ -38,9 +50,9 @@ namespace spdlog {
using mutex_t = null_mutex; using mutex_t = null_mutex;
static mutex_t &console_mutex() static mutex_t &console_mutex()
{ {
static auto mutex = mutex_t{}; static mutex_t mutex;
return mutex; return mutex;
} }
}; };
} } // namespace details
} } // namespace spdlog

View File

@ -13,8 +13,8 @@
// 3. Pass the formatted message to its sinks to performa the actual logging // 3. Pass the formatted message to its sinks to performa the actual logging
#include "common.h" #include "common.h"
#include "sinks/sink.h"
#include "formatter.h" #include "formatter.h"
#include "sinks/sink.h"
#include <memory> #include <memory>
#include <string> #include <string>

View File

@ -87,7 +87,6 @@ namespace spdlog {
} // namespace sinks } // namespace sinks
// Create and register android syslog logger // Create and register android syslog logger
template<typename Factory = default_factory> template<typename Factory = default_factory>
@ -96,7 +95,6 @@ namespace spdlog {
return return Factory::template create<sinks::android_sink>(logger_name, tag); return return Factory::template create<sinks::android_sink>(logger_name, tag);
} }
} // namespace spdlog } // namespace spdlog
#endif #endif

View File

@ -9,10 +9,10 @@
#include "../details/os.h" #include "../details/os.h"
#include "../details/traits.h" #include "../details/traits.h"
#include <string>
#include <unordered_map>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <string>
#include <unordered_map>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
@ -28,11 +28,11 @@ namespace spdlog {
public: public:
using mutex_t = typename ConsoleMutexTrait::mutex_t; using mutex_t = typename ConsoleMutexTrait::mutex_t;
ansicolor_sink() ansicolor_sink()
: target_file_(StdoutTrait::stream()), : target_file_(StdoutTrait::stream())
_mutex(ConsoleMutexTrait::mutex()) , _mutex(ConsoleMutexTrait::console_mutex())
{ {
should_do_colors_ = details::os::in_terminal(file) && details::os::is_color_terminal(); should_do_colors_ = details::os::in_terminal(target_file_) && details::os::is_color_terminal();
colors_[level::trace] = white; colors_[level::trace] = white;
colors_[level::debug] = cyan; colors_[level::debug] = cyan;
colors_[level::info] = green; colors_[level::info] = green;
@ -42,10 +42,7 @@ namespace spdlog {
colors_[level::off] = reset; colors_[level::off] = reset;
} }
~ansicolor_sink() override ~ansicolor_sink() override = default;
{
_flush();
}
ansicolor_sink(const ansicolor_sink &other) = delete; ansicolor_sink(const ansicolor_sink &other) = delete;
ansicolor_sink &operator=(const ansicolor_sink &other) = delete; ansicolor_sink &operator=(const ansicolor_sink &other) = delete;
@ -86,7 +83,6 @@ namespace spdlog {
const std::string on_cyan = "\033[46m"; const std::string on_cyan = "\033[46m";
const std::string on_white = "\033[47m"; const std::string on_white = "\033[47m";
void log(const details::log_msg &msg) SPDLOG_FINAL override void log(const details::log_msg &msg) SPDLOG_FINAL override
{ {
// Wrap the originally formatted message in color codes. // Wrap the originally formatted message in color codes.
@ -116,9 +112,7 @@ namespace spdlog {
fflush(target_file_); fflush(target_file_);
} }
private: private:
void _print_ccode(const std::string &color_code) void _print_ccode(const std::string &color_code)
{ {
fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_); fwrite(color_code.data(), sizeof(char), color_code.size(), target_file_);

View File

@ -4,10 +4,10 @@
// //
#pragma once #pragma once
#include "../details/file_helper.h" #include "../details/file_helper.h"
#include "../details/null_mutex.h" #include "../details/null_mutex.h"
#include "../fmt/fmt.h" #include "../fmt/fmt.h"
#include "../spdlog.h"
#include "base_sink.h" #include "base_sink.h"
#include <algorithm> #include <algorithm>

View File

@ -26,10 +26,9 @@ namespace {
using stderr_color_sink_st = ansicolor_stderr_sink_st; using stderr_color_sink_st = ansicolor_stderr_sink_st;
#endif #endif
} } // namespace
namespace spdlog namespace spdlog {
{
template<typename Factory = default_factory> template<typename Factory = default_factory>
inline std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name) inline std::shared_ptr<logger> stdout_color_mt(const std::string &logger_name)
{ {
@ -52,4 +51,4 @@ namespace spdlog
{ {
return Factory::template createstderr_color_sink_mt > (logger_name); return Factory::template createstderr_color_sink_mt > (logger_name);
} }
} } // namespace spdlog

View File

@ -7,12 +7,14 @@
#include "../details/null_mutex.h" #include "../details/null_mutex.h"
#include "../details/traits.h" #include "../details/traits.h"
#include "../spdlog.h"
#include <cstdio> #include <cstdio>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
namespace spdlog { namespace spdlog {
namespace sinks { namespace sinks {
template<class StdoutTrait, class ConsoleMutexTrait> template<class StdoutTrait, class ConsoleMutexTrait>
@ -20,9 +22,11 @@ namespace spdlog {
{ {
public: public:
using mutex_t = typename ConsoleMutexTrait::mutex_t; using mutex_t = typename ConsoleMutexTrait::mutex_t;
stdout_sink() : stdout_sink()
_mutex(ConsoleMutexTrait::console_mutex()), : _mutex(ConsoleMutexTrait::console_mutex())
_file(StdoutTrait::stream()) {} , _file(StdoutTrait::stream())
{
}
~stdout_sink() = default; ~stdout_sink() = default;
stdout_sink(const stdout_sink &other) = delete; stdout_sink(const stdout_sink &other) = delete;
@ -40,8 +44,9 @@ namespace spdlog {
std::lock_guard<mutex_t> lock(_mutex); std::lock_guard<mutex_t> lock(_mutex);
fflush(StdoutTrait::stream()); fflush(StdoutTrait::stream());
} }
private: private:
typename mutex_t& _mutex; mutex_t &_mutex;
FILE *_file; FILE *_file;
}; };

View File

@ -7,8 +7,6 @@
#include "../common.h" #include "../common.h"
#ifdef SPDLOG_ENABLE_SYSLOG
#include "../details/log_msg.h" #include "../details/log_msg.h"
#include "sink.h" #include "sink.h"
@ -75,10 +73,8 @@ namespace spdlog {
// Create and register a syslog logger // Create and register a syslog logger
template<typename Factory = default_factory> template<typename Factory = default_factory>
inline std::shared_ptr<logger> syslog_logger( inline std::shared_ptr<logger> syslog_logger(
const std::string &logger_name, const std::string &ident = "", int syslog_option = 0, int syslog_facilty = (1 << 3)) const std::string &logger_name, const std::string &syslog_ident = "", int syslog_option = 0, int syslog_facility = (1 << 3))
{ {
return return Factory::template create<sinks::syslog_sink>(logger_name, syslog_ident, syslog_option, syslog_facility); return Factory::template create<sinks::syslog_sink>(logger_name, syslog_ident, syslog_option, syslog_facility);
} }
} // namespace spdlog } // namespace spdlog
#endif

View File

@ -5,14 +5,13 @@
#pragma once #pragma once
#include "sink.h"
#include "../common.h" #include "../common.h"
#include "../details/null_mutex.h" #include "../details/null_mutex.h"
#include "../details/traits.h" #include "../details/traits.h"
#include "sink.h"
#include <mutex>
#include <memory> #include <memory>
#include <mutex>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <wincon.h> #include <wincon.h>
@ -26,7 +25,6 @@ namespace spdlog {
class wincolor_sink : public sink class wincolor_sink : public sink
{ {
public: public:
const WORD BOLD = FOREGROUND_INTENSITY; const WORD BOLD = FOREGROUND_INTENSITY;
const WORD RED = FOREGROUND_RED; const WORD RED = FOREGROUND_RED;
const WORD GREEN = FOREGROUND_GREEN; const WORD GREEN = FOREGROUND_GREEN;
@ -35,8 +33,8 @@ namespace spdlog {
const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN; const WORD YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
wincolor_sink() wincolor_sink()
: out_handle_(HandleTrait::handle()), : out_handle_(HandleTrait::handle())
_mutex(ConsoleMutexTrait::console_mutex()) , _mutex(ConsoleMutexTrait::console_mutex())
{ {
colors_[level::trace] = WHITE; colors_[level::trace] = WHITE;
colors_[level::debug] = CYAN; colors_[level::debug] = CYAN;
@ -47,7 +45,6 @@ namespace spdlog {
colors_[level::off] = 0; colors_[level::off] = 0;
} }
~wincolor_sink() override ~wincolor_sink() override
{ {
this->flush(); this->flush();
@ -117,7 +114,6 @@ namespace spdlog {
std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_; std::unordered_map<level::level_enum, WORD, level::level_hasher> colors_;
}; };
using wincolor_stdout_sink_mt = wincolor_sink<details::console_stdout_trait, details::console_mutex_trait>; using wincolor_stdout_sink_mt = wincolor_sink<details::console_stdout_trait, details::console_mutex_trait>;
using wincolor_stdout_sink_st = wincolor_sink<details::console_stdout_trait, details::console_null_mutex_trait>; using wincolor_stdout_sink_st = wincolor_sink<details::console_stdout_trait, details::console_null_mutex_trait>;

View File

@ -7,8 +7,8 @@
#pragma once #pragma once
#include "details/registry.h"
#include "common.h" #include "common.h"
#include "details/registry.h"
#include "logger.h" #include "logger.h"
#include <chrono> #include <chrono>
@ -32,7 +32,6 @@ struct default_factory
} }
}; };
// Create and register a logger with a templated sink type // Create and register a logger with a templated sink type
// The logger's level, formatter and flush level will be set according the global settings. // The logger's level, formatter and flush level will be set according the global settings.
// Example: // Example:
@ -116,7 +115,6 @@ inline void drop_all()
details::registry::instance().drop_all(); details::registry::instance().drop_all();
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// //
// Trace & Debug can be switched on/off at compile time for zero cost debug statements. // Trace & Debug can be switched on/off at compile time for zero cost debug statements.

View File

@ -12,9 +12,8 @@
#define SPDLOG_TRACE_ON #define SPDLOG_TRACE_ON
#define SPDLOG_DEBUG_ON #define SPDLOG_DEBUG_ON
#include "../include/spdlog/spdlog.h"
#include "../include/spdlog/async.h" #include "../include/spdlog/async.h"
#include "../include/spdlog/sinks/null_sink.h"
#include "../include/spdlog/sinks/file_sinks.h" #include "../include/spdlog/sinks/file_sinks.h"
#include "../include/spdlog/sinks/null_sink.h"
#include "../include/spdlog/sinks/ostream_sink.h" #include "../include/spdlog/sinks/ostream_sink.h"
#include "../include/spdlog/spdlog.h"