From a02d846a5b5e9b9842eb088015b7354b2ed0e278 Mon Sep 17 00:00:00 2001 From: Sidyhe Date: Tue, 7 Mar 2017 22:42:52 +0800 Subject: [PATCH 1/2] add wide string to utf8 string support --- include/spdlog/details/logger_impl.h | 58 ++++++++++++++++++++++++++++ include/spdlog/logger.h | 11 +++++- include/spdlog/tweakme.h | 7 ++++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 2b27f105..6dcb6fe0 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -296,3 +296,61 @@ inline const std::vector& spdlog::logger::sinks() const { return _sinks; } + +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT +#include + +template +inline void spdlog::logger::log(level::level_enum lvl, const wchar_t* msg) +{ + std::wstring_convert > conv; + + log(lvl, conv.to_bytes(msg)); +} + +template +inline void spdlog::logger::log(level::level_enum lvl, const wchar_t* fmt, const Args&... args) +{ + fmt::WMemoryWriter wWriter; + + wWriter.write(fmt, args...); + log(lvl, wWriter.c_str()); +} + +template +inline void spdlog::logger::trace(const wchar_t* fmt, const Args&... args) +{ + log(level::trace, fmt, args...); +} + +template +inline void spdlog::logger::debug(const wchar_t* fmt, const Args&... args) +{ + log(level::debug, fmt, args...); +} + +template +inline void spdlog::logger::info(const wchar_t* fmt, const Args&... args) +{ + log(level::info, fmt, args...); +} + + +template +inline void spdlog::logger::warn(const wchar_t* fmt, const Args&... args) +{ + log(level::warn, fmt, args...); +} + +template +inline void spdlog::logger::error(const wchar_t* fmt, const Args&... args) +{ + log(level::err, fmt, args...); +} + +template +inline void spdlog::logger::critical(const wchar_t* fmt, const Args&... args) +{ + log(level::critical, fmt, args...); +} +#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT \ No newline at end of file diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index a2deb51d..e63bdf0f 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -69,7 +69,16 @@ public: virtual void flush(); const std::vector& sinks() const; - +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + template void log(level::level_enum lvl, const wchar_t* msg); + template void log(level::level_enum lvl, const wchar_t* fmt, const Args&... args); + template void trace(const wchar_t* fmt, const Args&... args); + template void debug(const wchar_t* fmt, const Args&... args); + template void info(const wchar_t* fmt, const Args&... args); + template void warn(const wchar_t* fmt, const Args&... args); + template void error(const wchar_t* fmt, const Args&... args); + template void critical(const wchar_t* fmt, const Args&... args); +#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT protected: virtual void _sink_it(details::log_msg&); virtual void _set_pattern(const std::string&); diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h index 86f66b9e..31e105c3 100644 --- a/include/spdlog/tweakme.h +++ b/include/spdlog/tweakme.h @@ -106,3 +106,10 @@ // // #define SPDLOG_PREVENT_CHILD_FD /////////////////////////////////////////////////////////////////////////////// + + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment to enable wchar_t support (convert to utf8) +// +// #define SPDLOG_WCHAR_TO_UTF8_SUPPORT +/////////////////////////////////////////////////////////////////////////////// From 8e62d1f250fe5195b2a098403ebaa7ebc7fd2041 Mon Sep 17 00:00:00 2001 From: Sid Date: Thu, 30 Mar 2017 12:17:09 +0800 Subject: [PATCH 2/2] Revert "update" --- README.md | 8 ++++---- include/spdlog/details/logger_impl.h | 8 ++++---- include/spdlog/details/os.h | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index c1a74489..f5e5fe34 100644 --- a/README.md +++ b/README.md @@ -149,13 +149,13 @@ int main(int, char*[]) err_handler_example(); // Apply a function on all registered loggers - spd::apply_all([&](std::shared_ptr l) + spd::apply_all([&](std::shared_ptr l) { l->info("End of example."); }); // Release and close all loggers - spd::drop_all(); + spdlog::drop_all(); } // Exceptions will only be thrown upon failed logger or sink construction (not during logging) catch (const spd::spdlog_ex& ex) @@ -168,7 +168,7 @@ int main(int, char*[]) void async_example() { size_t q_size = 4096; //queue size must be power of 2 - spd::set_async_mode(q_size); + spdlog::set_async_mode(q_size); auto async_file = spd::daily_logger_st("async_file_logger", "logs/async_log.txt"); for (int i = 0; i < 100; ++i) async_file->info("Async message #{}", i); @@ -206,7 +206,7 @@ void user_defined_example() // void err_handler_example() { - spd::set_error_handler([](const std::string& msg) { + spdlog::set_error_handler([](const std::string& msg) { std::cerr << "my err handler: " << msg << std::endl; }); // (or logger->set_error_handler(..) to set for specific logger) diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index dfe50b14..d77e87aa 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -18,11 +18,11 @@ template inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end): _name(logger_name), _sinks(begin, end), - _formatter(std::make_shared("%+")), - _level(level::info), - _flush_level(level::off), - _last_err_time(0) + _formatter(std::make_shared("%+")) { + _level = level::info; + _flush_level = level::off; + _last_err_time = 0; _err_handler = [this](const std::string &msg) { this->_default_err_handler(msg); diff --git a/include/spdlog/details/os.h b/include/spdlog/details/os.h index 4d9f60a5..b63ce667 100644 --- a/include/spdlog/details/os.h +++ b/include/spdlog/details/os.h @@ -375,7 +375,7 @@ inline std::string errno_str(int err_num) if(strerror_s(buf, buf_size, err_num) == 0) return std::string(buf); else - return "Unknown error"; + return "Unkown error"; #elif defined(__FreeBSD__) || defined(__APPLE__) || defined(ANDROID) || defined(__SUNPRO_CC) || \ ((_POSIX_C_SOURCE >= 200112L) && ! defined(_GNU_SOURCE)) // posix version @@ -383,7 +383,7 @@ inline std::string errno_str(int err_num) if (strerror_r(err_num, buf, buf_size) == 0) return std::string(buf); else - return "Unknown error"; + return "Unkown error"; #else // gnu version (might not use the given buf, so its retval pointer must be used) return std::string(strerror_r(err_num, buf, buf_size));