From d900a3e16f5c136d3318a6f9309ab5c8a903d34a Mon Sep 17 00:00:00 2001 From: caesar Date: Sat, 26 Feb 2022 23:46:35 +0800 Subject: [PATCH 01/29] add RollingDateFileAppender.h rolling file appender by date --- .../plog/Appenders/RollingDateFileAppender.h | 80 +++++++++++++++++++ include/plog/Appenders/RollingFileAppender.h | 3 + include/plog/Util.h | 44 ++++++++++ samples/CMakeLists.txt | 1 + .../RollingDateFileAppender/CMakeLists.txt | 3 + samples/RollingDateFileAppender/Main.cpp | 25 ++++++ 6 files changed, 156 insertions(+) create mode 100644 include/plog/Appenders/RollingDateFileAppender.h create mode 100644 samples/RollingDateFileAppender/CMakeLists.txt create mode 100644 samples/RollingDateFileAppender/Main.cpp diff --git a/include/plog/Appenders/RollingDateFileAppender.h b/include/plog/Appenders/RollingDateFileAppender.h new file mode 100644 index 0000000..388c9b8 --- /dev/null +++ b/include/plog/Appenders/RollingDateFileAppender.h @@ -0,0 +1,80 @@ +// +// Created by caesar kekxv on 2022/2/26. +// + +#ifndef TERMINALSERVICE_ROLLINGDATEFILEAPPENDER_H +#define TERMINALSERVICE_ROLLINGDATEFILEAPPENDER_H + +#include +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#endif +namespace plog { + + template > + class RollingDateFileAppender : public RollingFileAppender { + private: + time_t last_date; + size_t m_fileSize; + const util::nchar *m_fileName; + + public: +#ifdef _WIN32 + /** + * + * @param fileName "%Y-%m-%d-%H-%M-%S" + * @param maxFileSize + * @param maxFiles + */ + inline explicit RollingDateFileAppender(const char* fileName,int maxFiles = 0) + : RollingFileAppender( + util::get_file_name(fileName).c_str()) + { + m_fileSize = maxFiles; + m_fileName = util::toWide(fileName).c_str(); + last_date = util::get_zero_time(); + } +#endif + + /** + * + * @param fileName "%Y-%m-%d-%H-%M-%S" + * @param maxFileSize + * @param maxFiles + */ + inline explicit RollingDateFileAppender(const util::nchar *fileName, size_t maxFiles = 0) + : RollingFileAppender( + util::get_file_name(fileName).c_str()) { + m_fileSize = maxFiles; + m_fileName = fileName; + last_date = 0; + } + + inline void write(const Record &record) { + if (record.getTime().time >= last_date) { + std::string path = util::get_file_name(m_fileName); + this->setFileName(path.c_str()); + if (m_fileSize > 0) { + int size = (int) m_fileSize; + util::nstring lastFileName = util::get_file_name(m_fileName, -size); + util::File::unlink(lastFileName.c_str()); + } + if (!(util::exists(path.c_str()))) { + RollingFileAppender::rollLogFiles(); + } + last_date = util::get_zero_time(); + } + RollingFileAppender::write(record); + } + }; +} + + +#endif //TERMINALSERVICE_ROLLINGDATEFILEAPPENDER_H diff --git a/include/plog/Appenders/RollingFileAppender.h b/include/plog/Appenders/RollingFileAppender.h index 69873ef..c42bf6e 100644 --- a/include/plog/Appenders/RollingFileAppender.h +++ b/include/plog/Appenders/RollingFileAppender.h @@ -7,9 +7,12 @@ namespace plog { + template + class RollingDateFileAppender; template > class RollingFileAppender : public IAppender { + friend class RollingDateFileAppender; public: RollingFileAppender(const util::nchar* fileName, size_t maxFileSize = 0, int maxFiles = 0) : m_fileSize() diff --git a/include/plog/Util.h b/include/plog/Util.h index 72bf7db..276487f 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -110,6 +110,50 @@ namespace plog #endif } + inline static bool exists(const char *path_string) { + struct stat buffer = {}; + return (stat(path_string, &buffer) == 0); + } + inline static time_t get_zero_time() { + time_t t = time(nullptr); + struct tm *tm = localtime(&t); + tm->tm_mday += 1; + tm->tm_hour = 0; + tm->tm_min = 0; + tm->tm_sec = 0; + return mktime(tm); + } + +#ifdef _WIN32 + /** + * "%Y-%m-%d-%H-%M-%S" + * @param name + * @return + */ + inline static std::string get_file_name(const util::nchar *name) { + return get_file_name(util::toNarrow(name,CP_UTF8).c_str()); + } +#endif + + /** + * "%Y-%m-%d-%H-%M-%S" + * @param name + * @return + */ + inline static std::string get_file_name(const char *name, int day = 0) { + std::stringstream str_time; + std::time_t current_time = std::time(nullptr); + if (day != 0) { + struct tm *tm = localtime(¤t_time); + tm->tm_mday += day; + current_time = mktime(tm); + } + char tAll[255]; + std::strftime(tAll, sizeof(tAll), name, std::localtime(¤t_time)); + str_time << tAll; + return str_time.str(); + } + #ifdef _WIN32 typedef timeb Time; diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 28e9302..015fc6d 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -62,3 +62,4 @@ add_subdirectory(SetFileName) add_subdirectory(Shared) add_subdirectory(SkipNativeEOL) add_subdirectory(UtcTime) +add_subdirectory(RollingDateFileAppender) diff --git a/samples/RollingDateFileAppender/CMakeLists.txt b/samples/RollingDateFileAppender/CMakeLists.txt new file mode 100644 index 0000000..db83d3c --- /dev/null +++ b/samples/RollingDateFileAppender/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable(RollingDateFileAppender Main.cpp) +target_link_libraries(RollingDateFileAppender plog) +set_target_properties(RollingDateFileAppender PROPERTIES FOLDER Samples) \ No newline at end of file diff --git a/samples/RollingDateFileAppender/Main.cpp b/samples/RollingDateFileAppender/Main.cpp new file mode 100644 index 0000000..aa40187 --- /dev/null +++ b/samples/RollingDateFileAppender/Main.cpp @@ -0,0 +1,25 @@ +// +// Hello - a minimal introduction sample, shows the basic 3 steps to start using plog. +// + +#include // Step1: include the header. +#include +#include +#include + +int main() { + using plog::TxtFormatter; + static plog::RollingDateFileAppender dateFileAppender("Hello_date.%Y-%m-%d.log"); + + + plog::init(plog::debug, &dateFileAppender); // Step2: initialize the logger. + + // Step3: write log messages using a special macro. There are several log macros, use the macro you liked the most. + for (int i = 0; i < 100; i++) { + PLOGD << "Hello log!"; // short macro + PLOG_DEBUG << "Hello log!"; // long macro + PLOG(plog::debug) << "Hello log!"; // function-style macro + std::this_thread::sleep_for(std::chrono::seconds(2)); + } + return 0; +} From 2954b3376ed7836dd7465da5e5123d9700e766ba Mon Sep 17 00:00:00 2001 From: caesar Date: Sat, 26 Feb 2022 23:57:18 +0800 Subject: [PATCH 02/29] add RollingDateFileAppender.h rolling file appender by date --- .../plog/Appenders/RollingDateFileAppender.h | 79 +++++++++++++++++++ include/plog/Appenders/RollingFileAppender.h | 3 + include/plog/Util.h | 44 +++++++++++ samples/CMakeLists.txt | 1 + .../RollingDateFileAppender/CMakeLists.txt | 3 + samples/RollingDateFileAppender/Main.cpp | 24 ++++++ 6 files changed, 154 insertions(+) create mode 100644 include/plog/Appenders/RollingDateFileAppender.h create mode 100644 samples/RollingDateFileAppender/CMakeLists.txt create mode 100644 samples/RollingDateFileAppender/Main.cpp diff --git a/include/plog/Appenders/RollingDateFileAppender.h b/include/plog/Appenders/RollingDateFileAppender.h new file mode 100644 index 0000000..f6a92dc --- /dev/null +++ b/include/plog/Appenders/RollingDateFileAppender.h @@ -0,0 +1,79 @@ +// +// Created by caesar kekxv on 2022/2/26. +// + +#ifndef TERMINALSERVICE_ROLLINGDATEFILEAPPENDER_H +#define TERMINALSERVICE_ROLLINGDATEFILEAPPENDER_H + +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#endif +namespace plog { + + template > + class RollingDateFileAppender : public RollingFileAppender { + private: + time_t last_date; + size_t m_fileSize; + const util::nchar *m_fileName; + + public: +#ifdef _WIN32 + /** + * + * @param fileName "%Y-%m-%d-%H-%M-%S" + * @param maxFileSize + * @param maxFiles + */ + inline explicit RollingDateFileAppender(const char* fileName,int maxFiles = 0) + : RollingFileAppender( + util::get_file_name(fileName).c_str()) + { + m_fileSize = maxFiles; + m_fileName = util::toWide(fileName).c_str(); + last_date = util::get_zero_time(); + } +#endif + + /** + * + * @param fileName "%Y-%m-%d-%H-%M-%S" + * @param maxFileSize + * @param maxFiles + */ + inline explicit RollingDateFileAppender(const util::nchar *fileName, size_t maxFiles = 0) + : RollingFileAppender( + util::get_file_name(fileName).c_str()) { + m_fileSize = maxFiles; + m_fileName = fileName; + last_date = 0; + } + + inline void write(const Record &record) { + if (record.getTime().time >= last_date) { + std::string path = util::get_file_name(m_fileName); + this->setFileName(path.c_str()); + if (m_fileSize > 0) { + int size = (int) m_fileSize; + util::nstring lastFileName = util::get_file_name(m_fileName, -size); + util::File::unlink(lastFileName.c_str()); + } + if (!(util::exists(path.c_str()))) { + RollingFileAppender::rollLogFiles(); + } + last_date = util::get_zero_time(); + } + RollingFileAppender::write(record); + } + }; +} + + +#endif //TERMINALSERVICE_ROLLINGDATEFILEAPPENDER_H diff --git a/include/plog/Appenders/RollingFileAppender.h b/include/plog/Appenders/RollingFileAppender.h index 82a0d90..c2a6d46 100644 --- a/include/plog/Appenders/RollingFileAppender.h +++ b/include/plog/Appenders/RollingFileAppender.h @@ -7,9 +7,12 @@ namespace plog { + template + class RollingDateFileAppender; template > class RollingFileAppender : public IAppender { + friend class RollingDateFileAppender; public: RollingFileAppender(const util::nchar* fileName, size_t maxFileSize = 0, int maxFiles = 0) : m_fileSize() diff --git a/include/plog/Util.h b/include/plog/Util.h index b9ad00b..9e9b031 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -110,6 +110,50 @@ namespace plog #endif } + inline static bool exists(const char *path_string) { + struct stat buffer = {}; + return (stat(path_string, &buffer) == 0); + } + inline static time_t get_zero_time() { + time_t t = time(nullptr); + struct tm *tm = localtime(&t); + tm->tm_mday += 1; + tm->tm_hour = 0; + tm->tm_min = 0; + tm->tm_sec = 0; + return mktime(tm); + } + +#ifdef _WIN32 + /** + * "%Y-%m-%d-%H-%M-%S" + * @param name + * @return + */ + inline static std::string get_file_name(const util::nchar *name) { + return get_file_name(util::toNarrow(name,CP_UTF8).c_str()); + } +#endif + + /** + * "%Y-%m-%d-%H-%M-%S" + * @param name + * @return + */ + inline static std::string get_file_name(const char *name, int day = 0) { + std::stringstream str_time; + std::time_t current_time = std::time(nullptr); + if (day != 0) { + struct tm *tm = localtime(¤t_time); + tm->tm_mday += day; + current_time = mktime(tm); + } + char tAll[255]; + std::strftime(tAll, sizeof(tAll), name, std::localtime(¤t_time)); + str_time << tAll; + return str_time.str(); + } + #ifdef _WIN32 typedef timeb Time; diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index 3b5428a..e763270 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -60,3 +60,4 @@ add_subdirectory(SetFileName) add_subdirectory(Shared) add_subdirectory(SkipNativeEOL) add_subdirectory(UtcTime) +add_subdirectory(RollingDateFileAppender) diff --git a/samples/RollingDateFileAppender/CMakeLists.txt b/samples/RollingDateFileAppender/CMakeLists.txt new file mode 100644 index 0000000..db83d3c --- /dev/null +++ b/samples/RollingDateFileAppender/CMakeLists.txt @@ -0,0 +1,3 @@ +add_executable(RollingDateFileAppender Main.cpp) +target_link_libraries(RollingDateFileAppender plog) +set_target_properties(RollingDateFileAppender PROPERTIES FOLDER Samples) \ No newline at end of file diff --git a/samples/RollingDateFileAppender/Main.cpp b/samples/RollingDateFileAppender/Main.cpp new file mode 100644 index 0000000..b1e4687 --- /dev/null +++ b/samples/RollingDateFileAppender/Main.cpp @@ -0,0 +1,24 @@ +// +// Hello - a minimal introduction sample, shows the basic 3 steps to start using plog. +// + +#include // Step1: include the header. +#include +#include + +int main() { + using plog::TxtFormatter; + static plog::RollingDateFileAppender dateFileAppender("Hello_date.%Y-%m-%d.log"); + + + plog::init(plog::debug, &dateFileAppender); // Step2: initialize the logger. + + // Step3: write log messages using a special macro. There are several log macros, use the macro you liked the most. + for (int i = 0; i < 100; i++) { + PLOGD << "Hello log!"; // short macro + PLOG_DEBUG << "Hello log!"; // long macro + PLOG(plog::debug) << "Hello log!"; // function-style macro + std::this_thread::sleep_for(std::chrono::seconds(2)); + } + return 0; +} From 38d8b1b13e6cd515078fec5b758e30571a412803 Mon Sep 17 00:00:00 2001 From: caesar Date: Mon, 28 Feb 2022 21:10:18 +0800 Subject: [PATCH 03/29] fix RollingDateFileAppender.h --- .../plog/Appenders/RollingDateFileAppender.h | 10 +- include/plog/Util.h | 93 ++++++++++--------- samples/RollingDateFileAppender/Main.cpp | 4 +- 3 files changed, 60 insertions(+), 47 deletions(-) diff --git a/include/plog/Appenders/RollingDateFileAppender.h b/include/plog/Appenders/RollingDateFileAppender.h index f6a92dc..0d2044f 100644 --- a/include/plog/Appenders/RollingDateFileAppender.h +++ b/include/plog/Appenders/RollingDateFileAppender.h @@ -56,14 +56,22 @@ namespace plog { last_date = 0; } + inline void setMaxFiles(int maxFiles) { + m_fileSize = maxFiles; + } + inline void write(const Record &record) { if (record.getTime().time >= last_date) { std::string path = util::get_file_name(m_fileName); this->setFileName(path.c_str()); if (m_fileSize > 0) { int size = (int) m_fileSize; - util::nstring lastFileName = util::get_file_name(m_fileName, -size); + std::string lastFileName = util::get_file_name(m_fileName, -size); +#ifdef _WIN32 + util::File::unlink(util::toWide(lastFileName.c_str()).c_str()); +#else util::File::unlink(lastFileName.c_str()); +#endif } if (!(util::exists(path.c_str()))) { RollingFileAppender::rollLogFiles(); diff --git a/include/plog/Util.h b/include/plog/Util.h index 9e9b031..623b25f 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -64,6 +65,9 @@ #ifdef _WIN32 # define _PLOG_NSTR(x) L##x # define PLOG_NSTR(x) _PLOG_NSTR(x) +#ifndef CP_UTF8 +#define CP_UTF8 65001 +#endif #else # define PLOG_NSTR(x) x #endif @@ -110,50 +114,6 @@ namespace plog #endif } - inline static bool exists(const char *path_string) { - struct stat buffer = {}; - return (stat(path_string, &buffer) == 0); - } - inline static time_t get_zero_time() { - time_t t = time(nullptr); - struct tm *tm = localtime(&t); - tm->tm_mday += 1; - tm->tm_hour = 0; - tm->tm_min = 0; - tm->tm_sec = 0; - return mktime(tm); - } - -#ifdef _WIN32 - /** - * "%Y-%m-%d-%H-%M-%S" - * @param name - * @return - */ - inline static std::string get_file_name(const util::nchar *name) { - return get_file_name(util::toNarrow(name,CP_UTF8).c_str()); - } -#endif - - /** - * "%Y-%m-%d-%H-%M-%S" - * @param name - * @return - */ - inline static std::string get_file_name(const char *name, int day = 0) { - std::stringstream str_time; - std::time_t current_time = std::time(nullptr); - if (day != 0) { - struct tm *tm = localtime(¤t_time); - tm->tm_mday += day; - current_time = mktime(tm); - } - char tAll[255]; - std::strftime(tAll, sizeof(tAll), name, std::localtime(¤t_time)); - str_time << tAll; - return str_time.str(); - } - #ifdef _WIN32 typedef timeb Time; @@ -310,6 +270,51 @@ namespace plog } #endif + inline static bool exists(const char *path_string) { + struct stat buffer = {}; + return (stat(path_string, &buffer) == 0); + } + inline static time_t get_zero_time() { + time_t t = time(nullptr); + struct tm *tm = localtime(&t); + tm->tm_mday += 1; + tm->tm_hour = 0; + tm->tm_min = 0; + tm->tm_sec = 0; + return mktime(tm); + } + + /** + * "%Y-%m-%d-%H-%M-%S" + * @param name + * @return + */ + inline static std::string get_file_name(const char *name, int day = 0) { + std::stringstream str_time; + std::time_t current_time = std::time(nullptr); + if (day != 0) { + struct tm *tm = localtime(¤t_time); + tm->tm_mday += day; + current_time = mktime(tm); + } + char tAll[255]; + std::strftime(tAll, sizeof(tAll), name, std::localtime(¤t_time)); + str_time << tAll; + return str_time.str(); + } + +#ifdef _WIN32 + /** + * "%Y-%m-%d-%H-%M-%S" + * @param name + * @return + */ + inline static std::string get_file_name(const util::nchar *name, int day = 0) { + return get_file_name(util::toNarrow(name,CP_UTF8).c_str(),day); + } +#endif + + inline std::string processFuncName(const char* func) { #if (defined(_WIN32) && !defined(__MINGW32__)) || defined(__OBJC__) diff --git a/samples/RollingDateFileAppender/Main.cpp b/samples/RollingDateFileAppender/Main.cpp index b1e4687..b618948 100644 --- a/samples/RollingDateFileAppender/Main.cpp +++ b/samples/RollingDateFileAppender/Main.cpp @@ -14,11 +14,11 @@ int main() { plog::init(plog::debug, &dateFileAppender); // Step2: initialize the logger. // Step3: write log messages using a special macro. There are several log macros, use the macro you liked the most. - for (int i = 0; i < 100; i++) { + for (int i = 0; i < 10; i++) { PLOGD << "Hello log!"; // short macro PLOG_DEBUG << "Hello log!"; // long macro PLOG(plog::debug) << "Hello log!"; // function-style macro - std::this_thread::sleep_for(std::chrono::seconds(2)); + std::this_thread::sleep_for(std::chrono::seconds(1)); } return 0; } From 279fd842ed8f1a5273ba8533e1f978ff0214997f Mon Sep 17 00:00:00 2001 From: caesar Date: Tue, 1 Mar 2022 13:03:54 +0800 Subject: [PATCH 04/29] replace nullptr to NULL --- include/plog/Util.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/plog/Util.h b/include/plog/Util.h index 623b25f..7bac416 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -275,7 +275,7 @@ namespace plog return (stat(path_string, &buffer) == 0); } inline static time_t get_zero_time() { - time_t t = time(nullptr); + time_t t = time(NULL); struct tm *tm = localtime(&t); tm->tm_mday += 1; tm->tm_hour = 0; @@ -291,7 +291,7 @@ namespace plog */ inline static std::string get_file_name(const char *name, int day = 0) { std::stringstream str_time; - std::time_t current_time = std::time(nullptr); + std::time_t current_time = std::time(NULL); if (day != 0) { struct tm *tm = localtime(¤t_time); tm->tm_mday += day; From 3216c0ba04d644b9bc4cf8abe2e06359a6d8dca4 Mon Sep 17 00:00:00 2001 From: caesar Date: Tue, 1 Mar 2022 13:13:04 +0800 Subject: [PATCH 05/29] fix --- include/plog/Util.h | 274 ++++++++++------------- samples/RollingDateFileAppender/Main.cpp | 5 +- 2 files changed, 120 insertions(+), 159 deletions(-) diff --git a/include/plog/Util.h b/include/plog/Util.h index 7bac416..baa1102 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -1,4 +1,5 @@ #pragma once + #include #include #include @@ -41,11 +42,13 @@ #endif #ifdef _WIN32 + # include # include # include # include # include + #elif defined(__rtems__) # include # include @@ -72,10 +75,8 @@ # define PLOG_NSTR(x) x #endif -namespace plog -{ - namespace util - { +namespace plog { + namespace util { #ifdef _WIN32 typedef std::wstring nstring; typedef std::wostringstream nostringstream; @@ -88,8 +89,7 @@ namespace plog typedef char nchar; #endif - inline void localtime_s(struct tm* t, const time_t* time) - { + inline void localtime_s(struct tm *t, const time_t *time) { #if defined(_WIN32) && defined(__BORLANDC__) ::localtime_s(time, t); #elif defined(_WIN32) && defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) @@ -101,8 +101,7 @@ namespace plog #endif } - inline void gmtime_s(struct tm* t, const time_t* time) - { + inline void gmtime_s(struct tm *t, const time_t *time) { #if defined(_WIN32) && defined(__BORLANDC__) ::gmtime_s(time, t); #elif defined(_WIN32) && defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) @@ -117,10 +116,10 @@ namespace plog #ifdef _WIN32 typedef timeb Time; - inline void ftime(Time* t) - { + inline void ftime(Time *t) { ::ftime(t); } + #else struct Time { @@ -138,8 +137,7 @@ namespace plog } #endif - inline unsigned int gettid() - { + inline unsigned int gettid() { #ifdef _WIN32 return GetCurrentThreadId(); #elif defined(__linux__) @@ -158,63 +156,57 @@ namespace plog } #ifdef _WIN32 - inline int vasprintf(char** strp, const char* format, va_list ap) - { - int len = _vscprintf(format, ap); - if (len < 0) - { - return -1; - } - char* str = static_cast(malloc(len + 1)); - if (!str) - { - return -1; - } + inline int vasprintf(char **strp, const char *format, va_list ap) { + int len = _vscprintf(format, ap); + if (len < 0) { + return -1; + } + + char *str = static_cast(malloc(len + 1)); + if (!str) { + return -1; + } #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) - int retval = _vsnprintf(str, len + 1, format, ap); + int retval = _vsnprintf(str, len + 1, format, ap); #else - int retval = _vsnprintf_s(str, len + 1, len, format, ap); + int retval = _vsnprintf_s(str, len + 1, len, format, ap); #endif - if (retval < 0) - { - free(str); - return -1; - } - - *strp = str; - return retval; - } + if (retval < 0) { + free(str); + return -1; + } - inline int vaswprintf(wchar_t** strp, const wchar_t* format, va_list ap) - { - int len = _vscwprintf(format, ap); - if (len < 0) - { - return -1; + *strp = str; + return retval; } - wchar_t* str = static_cast(malloc((len + 1) * sizeof(wchar_t))); - if (!str) - { - return -1; - } + inline int vaswprintf(wchar_t **strp, const wchar_t *format, va_list ap) { + int len = _vscwprintf(format, ap); + if (len < 0) { + return -1; + } + + wchar_t *str = static_cast(malloc((len + 1) * sizeof(wchar_t))); + if (!str) { + return -1; + } #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) - int retval = _vsnwprintf(str, len + 1, format, ap); + int retval = _vsnwprintf(str, len + 1, format, ap); #else - int retval = _vsnwprintf_s(str, len + 1, len, format, ap); + int retval = _vsnwprintf_s(str, len + 1, len, format, ap); #endif - if (retval < 0) - { - free(str); - return -1; + if (retval < 0) { + free(str); + return -1; + } + + *strp = str; + return retval; } - *strp = str; - return retval; - } #endif #if PLOG_ENABLE_WCHAR_INPUT && !defined(_WIN32) @@ -242,46 +234,48 @@ namespace plog #endif #ifdef _WIN32 - inline std::wstring toWide(const char* str) - { + + inline std::wstring toWide(const char *str) { size_t len = ::strlen(str); std::wstring wstr(len, 0); - if (!wstr.empty()) - { - int wlen = MultiByteToWideChar(codePage::kActive, 0, str, static_cast(len), &wstr[0], static_cast(wstr.size())); + if (!wstr.empty()) { + int wlen = MultiByteToWideChar(codePage::kActive, 0, str, static_cast(len), &wstr[0], + static_cast(wstr.size())); wstr.resize(wlen); } return wstr; } - inline std::string toNarrow(const std::wstring& wstr, long page) - { + inline std::string toNarrow(const std::wstring &wstr, long page) { std::string str(wstr.size() * sizeof(wchar_t), 0); - if (!str.empty()) - { - int len = WideCharToMultiByte(page, 0, wstr.c_str(), static_cast(wstr.size()), &str[0], static_cast(str.size()), 0, 0); + if (!str.empty()) { + int len = WideCharToMultiByte(page, 0, wstr.c_str(), static_cast(wstr.size()), &str[0], + static_cast(str.size()), 0, 0); str.resize(len); } return str; } + #endif inline static bool exists(const char *path_string) { struct stat buffer = {}; return (stat(path_string, &buffer) == 0); } + inline static time_t get_zero_time() { time_t t = time(NULL); - struct tm *tm = localtime(&t); - tm->tm_mday += 1; - tm->tm_hour = 0; - tm->tm_min = 0; - tm->tm_sec = 0; - return mktime(tm); + struct tm tm{}; + util::localtime_s(&tm, &t); + tm.tm_mday += 1; + tm.tm_hour = 0; + tm.tm_min = 0; + tm.tm_sec = 0; + return mktime(&tm); } /** @@ -304,34 +298,33 @@ namespace plog } #ifdef _WIN32 + /** * "%Y-%m-%d-%H-%M-%S" * @param name * @return */ inline static std::string get_file_name(const util::nchar *name, int day = 0) { - return get_file_name(util::toNarrow(name,CP_UTF8).c_str(),day); + return get_file_name(util::toNarrow(name, CP_UTF8).c_str(), day); } + #endif - inline std::string processFuncName(const char* func) - { + inline std::string processFuncName(const char *func) { #if (defined(_WIN32) && !defined(__MINGW32__)) || defined(__OBJC__) return std::string(func); #else - const char* funcBegin = func; - const char* funcEnd = ::strchr(funcBegin, '('); + const char *funcBegin = func; + const char *funcEnd = ::strchr(funcBegin, '('); - if (!funcEnd) - { + if (!funcEnd) { return std::string(func); } - for (const char* i = funcEnd - 1; i >= funcBegin; --i) // search backwards for the first space char + for (const char *i = funcEnd - 1; i >= funcBegin; --i) // search backwards for the first space char { - if (*i == ' ') - { + if (*i == ' ') { funcBegin = i + 1; break; } @@ -341,8 +334,7 @@ namespace plog #endif } - inline const nchar* findExtensionDot(const nchar* fileName) - { + inline const nchar *findExtensionDot(const nchar *fileName) { #ifdef _WIN32 return std::wcsrchr(fileName, L'.'); #else @@ -350,53 +342,43 @@ namespace plog #endif } - inline void splitFileName(const nchar* fileName, nstring& fileNameNoExt, nstring& fileExt) - { - const nchar* dot = findExtensionDot(fileName); + inline void splitFileName(const nchar *fileName, nstring &fileNameNoExt, nstring &fileExt) { + const nchar *dot = findExtensionDot(fileName); - if (dot) - { + if (dot) { fileNameNoExt.assign(fileName, dot); fileExt.assign(dot + 1); - } - else - { + } else { fileNameNoExt.assign(fileName); fileExt.clear(); } } - class PLOG_LINKAGE NonCopyable - { + class PLOG_LINKAGE NonCopyable { protected: - NonCopyable() - { + NonCopyable() { } private: - NonCopyable(const NonCopyable&); - NonCopyable& operator=(const NonCopyable&); + NonCopyable(const NonCopyable &); + + NonCopyable &operator=(const NonCopyable &); }; - class File : NonCopyable - { + class File : NonCopyable { public: - File() : m_file(-1) - { + File() : m_file(-1) { } - File(const nchar* fileName) : m_file(-1) - { + File(const nchar *fileName) : m_file(-1) { open(fileName); } - ~File() - { + ~File() { close(); } - size_t open(const nchar* fileName) - { + size_t open(const nchar *fileName) { #if defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__)) m_file = ::_wsopen(fileName, _O_CREAT | _O_WRONLY | _O_BINARY, SH_DENYWR, _S_IREAD | _S_IWRITE); #elif defined(_WIN32) @@ -407,40 +389,35 @@ namespace plog return seek(0, SEEK_END); } - size_t write(const void* buf, size_t count) - { + size_t write(const void *buf, size_t count) { return m_file != -1 ? static_cast( #ifdef _WIN32 - ::_write(m_file, buf, static_cast(count)) + ::_write(m_file, buf, static_cast(count)) #else - ::write(m_file, buf, count) + ::write(m_file, buf, count) #endif - ) : static_cast(-1); + ) : static_cast(-1); } template - size_t write(const std::basic_string& str) - { + size_t write(const std::basic_string &str) { return write(str.data(), str.size() * sizeof(CharType)); } - size_t seek(size_t offset, int whence) - { + size_t seek(size_t offset, int whence) { return m_file != -1 ? static_cast( #if defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__)) - ::_lseek(m_file, static_cast(offset), whence) + ::_lseek(m_file, static_cast(offset), whence) #elif defined(_WIN32) - ::_lseeki64(m_file, static_cast(offset), whence) + ::_lseeki64(m_file, static_cast(offset), whence) #else - ::lseek(m_file, static_cast(offset), whence) + ::lseek(m_file, static_cast(offset), whence) #endif - ) : static_cast(-1); + ) : static_cast(-1); } - void close() - { - if (m_file != -1) - { + void close() { + if (m_file != -1) { #ifdef _WIN32 ::_close(m_file); #else @@ -450,8 +427,7 @@ namespace plog } } - static int unlink(const nchar* fileName) - { + static int unlink(const nchar *fileName) { #ifdef _WIN32 return ::_wunlink(fileName); #else @@ -459,8 +435,7 @@ namespace plog #endif } - static int rename(const nchar* oldFilename, const nchar* newFilename) - { + static int rename(const nchar *oldFilename, const nchar *newFilename) { #ifdef _WIN32 return MoveFileW(oldFilename, newFilename); #else @@ -472,11 +447,9 @@ namespace plog int m_file; }; - class Mutex : NonCopyable - { + class Mutex : NonCopyable { public: - Mutex() - { + Mutex() { #ifdef _WIN32 InitializeCriticalSection(&m_sync); #elif defined(__rtems__) @@ -489,8 +462,7 @@ namespace plog #endif } - ~Mutex() - { + ~Mutex() { #ifdef _WIN32 DeleteCriticalSection(&m_sync); #elif defined(__rtems__) @@ -503,8 +475,7 @@ namespace plog friend class MutexLock; private: - void lock() - { + void lock() { #ifdef _WIN32 EnterCriticalSection(&m_sync); #elif defined(__rtems__) @@ -514,8 +485,7 @@ namespace plog #endif } - void unlock() - { + void unlock() { #ifdef _WIN32 LeaveCriticalSection(&m_sync); #elif defined(__rtems__) @@ -533,53 +503,47 @@ namespace plog #endif }; - class MutexLock : NonCopyable - { + class MutexLock : NonCopyable { public: - MutexLock(Mutex& mutex) : m_mutex(mutex) - { + MutexLock(Mutex &mutex) : m_mutex(mutex) { m_mutex.lock(); } - ~MutexLock() - { + ~MutexLock() { m_mutex.unlock(); } private: - Mutex& m_mutex; + Mutex &m_mutex; }; template #ifdef _WIN32 class Singleton : NonCopyable #else - class PLOG_LINKAGE Singleton : NonCopyable + class PLOG_LINKAGE Singleton : NonCopyable #endif { public: - Singleton() - { + Singleton() { assert(!m_instance); - m_instance = static_cast(this); + m_instance = static_cast(this); } - ~Singleton() - { + ~Singleton() { assert(m_instance); m_instance = 0; } - static T* getInstance() - { + static T *getInstance() { return m_instance; } private: - static T* m_instance; + static T *m_instance; }; template - T* Singleton::m_instance = NULL; + T *Singleton::m_instance = NULL; } } diff --git a/samples/RollingDateFileAppender/Main.cpp b/samples/RollingDateFileAppender/Main.cpp index b618948..75f8329 100644 --- a/samples/RollingDateFileAppender/Main.cpp +++ b/samples/RollingDateFileAppender/Main.cpp @@ -4,11 +4,9 @@ #include // Step1: include the header. #include -#include int main() { - using plog::TxtFormatter; - static plog::RollingDateFileAppender dateFileAppender("Hello_date.%Y-%m-%d.log"); + static plog::RollingDateFileAppender dateFileAppender("Hello_date.%Y-%m-%d.log"); plog::init(plog::debug, &dateFileAppender); // Step2: initialize the logger. @@ -18,7 +16,6 @@ int main() { PLOGD << "Hello log!"; // short macro PLOG_DEBUG << "Hello log!"; // long macro PLOG(plog::debug) << "Hello log!"; // function-style macro - std::this_thread::sleep_for(std::chrono::seconds(1)); } return 0; } From baec6f3d7b2e264fc217f177b85be5fdfd82e43a Mon Sep 17 00:00:00 2001 From: caesar Date: Tue, 1 Mar 2022 13:15:11 +0800 Subject: [PATCH 06/29] restore code format --- include/plog/Util.h | 261 +++++++++++++++++++++++++------------------- 1 file changed, 150 insertions(+), 111 deletions(-) diff --git a/include/plog/Util.h b/include/plog/Util.h index baa1102..0d0c2bd 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -1,5 +1,4 @@ #pragma once - #include #include #include @@ -42,13 +41,11 @@ #endif #ifdef _WIN32 - # include # include # include # include # include - #elif defined(__rtems__) # include # include @@ -75,8 +72,10 @@ # define PLOG_NSTR(x) x #endif -namespace plog { - namespace util { +namespace plog +{ + namespace util + { #ifdef _WIN32 typedef std::wstring nstring; typedef std::wostringstream nostringstream; @@ -89,7 +88,8 @@ namespace plog { typedef char nchar; #endif - inline void localtime_s(struct tm *t, const time_t *time) { + inline void localtime_s(struct tm* t, const time_t* time) + { #if defined(_WIN32) && defined(__BORLANDC__) ::localtime_s(time, t); #elif defined(_WIN32) && defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) @@ -101,7 +101,8 @@ namespace plog { #endif } - inline void gmtime_s(struct tm *t, const time_t *time) { + inline void gmtime_s(struct tm* t, const time_t* time) + { #if defined(_WIN32) && defined(__BORLANDC__) ::gmtime_s(time, t); #elif defined(_WIN32) && defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) @@ -116,10 +117,10 @@ namespace plog { #ifdef _WIN32 typedef timeb Time; - inline void ftime(Time *t) { + inline void ftime(Time* t) + { ::ftime(t); } - #else struct Time { @@ -137,7 +138,8 @@ namespace plog { } #endif - inline unsigned int gettid() { + inline unsigned int gettid() + { #ifdef _WIN32 return GetCurrentThreadId(); #elif defined(__linux__) @@ -156,57 +158,63 @@ namespace plog { } #ifdef _WIN32 + inline int vasprintf(char** strp, const char* format, va_list ap) + { + int len = _vscprintf(format, ap); + if (len < 0) + { + return -1; + } - inline int vasprintf(char **strp, const char *format, va_list ap) { - int len = _vscprintf(format, ap); - if (len < 0) { - return -1; - } - - char *str = static_cast(malloc(len + 1)); - if (!str) { - return -1; - } + char* str = static_cast(malloc(len + 1)); + if (!str) + { + return -1; + } #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) - int retval = _vsnprintf(str, len + 1, format, ap); + int retval = _vsnprintf(str, len + 1, format, ap); #else - int retval = _vsnprintf_s(str, len + 1, len, format, ap); + int retval = _vsnprintf_s(str, len + 1, len, format, ap); #endif - if (retval < 0) { - free(str); - return -1; - } - - *strp = str; - return retval; + if (retval < 0) + { + free(str); + return -1; } - inline int vaswprintf(wchar_t **strp, const wchar_t *format, va_list ap) { - int len = _vscwprintf(format, ap); - if (len < 0) { - return -1; - } + *strp = str; + return retval; + } - wchar_t *str = static_cast(malloc((len + 1) * sizeof(wchar_t))); - if (!str) { - return -1; - } + inline int vaswprintf(wchar_t** strp, const wchar_t* format, va_list ap) + { + int len = _vscwprintf(format, ap); + if (len < 0) + { + return -1; + } + + wchar_t* str = static_cast(malloc((len + 1) * sizeof(wchar_t))); + if (!str) + { + return -1; + } #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) - int retval = _vsnwprintf(str, len + 1, format, ap); + int retval = _vsnwprintf(str, len + 1, format, ap); #else - int retval = _vsnwprintf_s(str, len + 1, len, format, ap); + int retval = _vsnwprintf_s(str, len + 1, len, format, ap); #endif - if (retval < 0) { - free(str); - return -1; - } - - *strp = str; - return retval; + if (retval < 0) + { + free(str); + return -1; } + *strp = str; + return retval; + } #endif #if PLOG_ENABLE_WCHAR_INPUT && !defined(_WIN32) @@ -234,32 +242,32 @@ namespace plog { #endif #ifdef _WIN32 - - inline std::wstring toWide(const char *str) { + inline std::wstring toWide(const char* str) + { size_t len = ::strlen(str); std::wstring wstr(len, 0); - if (!wstr.empty()) { - int wlen = MultiByteToWideChar(codePage::kActive, 0, str, static_cast(len), &wstr[0], - static_cast(wstr.size())); + if (!wstr.empty()) + { + int wlen = MultiByteToWideChar(codePage::kActive, 0, str, static_cast(len), &wstr[0], static_cast(wstr.size())); wstr.resize(wlen); } return wstr; } - inline std::string toNarrow(const std::wstring &wstr, long page) { + inline std::string toNarrow(const std::wstring& wstr, long page) + { std::string str(wstr.size() * sizeof(wchar_t), 0); - if (!str.empty()) { - int len = WideCharToMultiByte(page, 0, wstr.c_str(), static_cast(wstr.size()), &str[0], - static_cast(str.size()), 0, 0); + if (!str.empty()) + { + int len = WideCharToMultiByte(page, 0, wstr.c_str(), static_cast(wstr.size()), &str[0], static_cast(str.size()), 0, 0); str.resize(len); } return str; } - #endif inline static bool exists(const char *path_string) { @@ -271,6 +279,7 @@ namespace plog { time_t t = time(NULL); struct tm tm{}; util::localtime_s(&tm, &t); + tm.tm_mday += 1; tm.tm_hour = 0; tm.tm_min = 0; @@ -298,33 +307,34 @@ namespace plog { } #ifdef _WIN32 - /** * "%Y-%m-%d-%H-%M-%S" * @param name * @return */ inline static std::string get_file_name(const util::nchar *name, int day = 0) { - return get_file_name(util::toNarrow(name, CP_UTF8).c_str(), day); + return get_file_name(util::toNarrow(name,CP_UTF8).c_str(),day); } - #endif - inline std::string processFuncName(const char *func) { + inline std::string processFuncName(const char* func) + { #if (defined(_WIN32) && !defined(__MINGW32__)) || defined(__OBJC__) return std::string(func); #else - const char *funcBegin = func; - const char *funcEnd = ::strchr(funcBegin, '('); + const char* funcBegin = func; + const char* funcEnd = ::strchr(funcBegin, '('); - if (!funcEnd) { + if (!funcEnd) + { return std::string(func); } - for (const char *i = funcEnd - 1; i >= funcBegin; --i) // search backwards for the first space char + for (const char* i = funcEnd - 1; i >= funcBegin; --i) // search backwards for the first space char { - if (*i == ' ') { + if (*i == ' ') + { funcBegin = i + 1; break; } @@ -334,7 +344,8 @@ namespace plog { #endif } - inline const nchar *findExtensionDot(const nchar *fileName) { + inline const nchar* findExtensionDot(const nchar* fileName) + { #ifdef _WIN32 return std::wcsrchr(fileName, L'.'); #else @@ -342,43 +353,53 @@ namespace plog { #endif } - inline void splitFileName(const nchar *fileName, nstring &fileNameNoExt, nstring &fileExt) { - const nchar *dot = findExtensionDot(fileName); + inline void splitFileName(const nchar* fileName, nstring& fileNameNoExt, nstring& fileExt) + { + const nchar* dot = findExtensionDot(fileName); - if (dot) { + if (dot) + { fileNameNoExt.assign(fileName, dot); fileExt.assign(dot + 1); - } else { + } + else + { fileNameNoExt.assign(fileName); fileExt.clear(); } } - class PLOG_LINKAGE NonCopyable { + class PLOG_LINKAGE NonCopyable + { protected: - NonCopyable() { + NonCopyable() + { } private: - NonCopyable(const NonCopyable &); - - NonCopyable &operator=(const NonCopyable &); + NonCopyable(const NonCopyable&); + NonCopyable& operator=(const NonCopyable&); }; - class File : NonCopyable { + class File : NonCopyable + { public: - File() : m_file(-1) { + File() : m_file(-1) + { } - File(const nchar *fileName) : m_file(-1) { + File(const nchar* fileName) : m_file(-1) + { open(fileName); } - ~File() { + ~File() + { close(); } - size_t open(const nchar *fileName) { + size_t open(const nchar* fileName) + { #if defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__)) m_file = ::_wsopen(fileName, _O_CREAT | _O_WRONLY | _O_BINARY, SH_DENYWR, _S_IREAD | _S_IWRITE); #elif defined(_WIN32) @@ -389,35 +410,40 @@ namespace plog { return seek(0, SEEK_END); } - size_t write(const void *buf, size_t count) { + size_t write(const void* buf, size_t count) + { return m_file != -1 ? static_cast( #ifdef _WIN32 - ::_write(m_file, buf, static_cast(count)) + ::_write(m_file, buf, static_cast(count)) #else - ::write(m_file, buf, count) + ::write(m_file, buf, count) #endif - ) : static_cast(-1); + ) : static_cast(-1); } template - size_t write(const std::basic_string &str) { + size_t write(const std::basic_string& str) + { return write(str.data(), str.size() * sizeof(CharType)); } - size_t seek(size_t offset, int whence) { + size_t seek(size_t offset, int whence) + { return m_file != -1 ? static_cast( #if defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__)) - ::_lseek(m_file, static_cast(offset), whence) + ::_lseek(m_file, static_cast(offset), whence) #elif defined(_WIN32) - ::_lseeki64(m_file, static_cast(offset), whence) + ::_lseeki64(m_file, static_cast(offset), whence) #else - ::lseek(m_file, static_cast(offset), whence) + ::lseek(m_file, static_cast(offset), whence) #endif - ) : static_cast(-1); + ) : static_cast(-1); } - void close() { - if (m_file != -1) { + void close() + { + if (m_file != -1) + { #ifdef _WIN32 ::_close(m_file); #else @@ -427,7 +453,8 @@ namespace plog { } } - static int unlink(const nchar *fileName) { + static int unlink(const nchar* fileName) + { #ifdef _WIN32 return ::_wunlink(fileName); #else @@ -435,7 +462,8 @@ namespace plog { #endif } - static int rename(const nchar *oldFilename, const nchar *newFilename) { + static int rename(const nchar* oldFilename, const nchar* newFilename) + { #ifdef _WIN32 return MoveFileW(oldFilename, newFilename); #else @@ -447,9 +475,11 @@ namespace plog { int m_file; }; - class Mutex : NonCopyable { + class Mutex : NonCopyable + { public: - Mutex() { + Mutex() + { #ifdef _WIN32 InitializeCriticalSection(&m_sync); #elif defined(__rtems__) @@ -462,7 +492,8 @@ namespace plog { #endif } - ~Mutex() { + ~Mutex() + { #ifdef _WIN32 DeleteCriticalSection(&m_sync); #elif defined(__rtems__) @@ -475,7 +506,8 @@ namespace plog { friend class MutexLock; private: - void lock() { + void lock() + { #ifdef _WIN32 EnterCriticalSection(&m_sync); #elif defined(__rtems__) @@ -485,7 +517,8 @@ namespace plog { #endif } - void unlock() { + void unlock() + { #ifdef _WIN32 LeaveCriticalSection(&m_sync); #elif defined(__rtems__) @@ -503,47 +536,53 @@ namespace plog { #endif }; - class MutexLock : NonCopyable { + class MutexLock : NonCopyable + { public: - MutexLock(Mutex &mutex) : m_mutex(mutex) { + MutexLock(Mutex& mutex) : m_mutex(mutex) + { m_mutex.lock(); } - ~MutexLock() { + ~MutexLock() + { m_mutex.unlock(); } private: - Mutex &m_mutex; + Mutex& m_mutex; }; template #ifdef _WIN32 class Singleton : NonCopyable #else - class PLOG_LINKAGE Singleton : NonCopyable + class PLOG_LINKAGE Singleton : NonCopyable #endif { public: - Singleton() { + Singleton() + { assert(!m_instance); - m_instance = static_cast(this); + m_instance = static_cast(this); } - ~Singleton() { + ~Singleton() + { assert(m_instance); m_instance = 0; } - static T *getInstance() { + static T* getInstance() + { return m_instance; } private: - static T *m_instance; + static T* m_instance; }; template - T *Singleton::m_instance = NULL; + T* Singleton::m_instance = NULL; } } From fdf6e1113f06cdfc1450746bc0c2076a5152ab11 Mon Sep 17 00:00:00 2001 From: caesar Date: Tue, 1 Mar 2022 13:19:13 +0800 Subject: [PATCH 07/29] fix localtime --- include/plog/Util.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/include/plog/Util.h b/include/plog/Util.h index 0d0c2bd..0c46cb6 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -277,7 +277,7 @@ namespace plog inline static time_t get_zero_time() { time_t t = time(NULL); - struct tm tm{}; + tm tm; util::localtime_s(&tm, &t); tm.tm_mday += 1; @@ -296,12 +296,15 @@ namespace plog std::stringstream str_time; std::time_t current_time = std::time(NULL); if (day != 0) { - struct tm *tm = localtime(¤t_time); - tm->tm_mday += day; - current_time = mktime(tm); + tm tm; + util::localtime_s(&tm, ¤t_time); + tm.tm_mday += day; + current_time = mktime(&tm); } char tAll[255]; - std::strftime(tAll, sizeof(tAll), name, std::localtime(¤t_time)); + tm t; + util::localtime_s(&t, ¤t_time); + std::strftime(tAll, sizeof(tAll), name, &t); str_time << tAll; return str_time.str(); } From 00d977d538441e938a3e41158ab6e9ef21906ba2 Mon Sep 17 00:00:00 2001 From: caesar Date: Tue, 1 Mar 2022 13:23:30 +0800 Subject: [PATCH 08/29] fix #include --- samples/RollingDateFileAppender/Main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/samples/RollingDateFileAppender/Main.cpp b/samples/RollingDateFileAppender/Main.cpp index 75f8329..54d13eb 100644 --- a/samples/RollingDateFileAppender/Main.cpp +++ b/samples/RollingDateFileAppender/Main.cpp @@ -3,6 +3,7 @@ // #include // Step1: include the header. +#include #include int main() { From cbb7a5652f87aaa19e9b9ff99dda6778234470ae Mon Sep 17 00:00:00 2001 From: caesar Date: Tue, 1 Mar 2022 13:26:11 +0800 Subject: [PATCH 09/29] fix include --- samples/RollingDateFileAppender/Main.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/samples/RollingDateFileAppender/Main.cpp b/samples/RollingDateFileAppender/Main.cpp index 54d13eb..cc16eae 100644 --- a/samples/RollingDateFileAppender/Main.cpp +++ b/samples/RollingDateFileAppender/Main.cpp @@ -3,7 +3,11 @@ // #include // Step1: include the header. +#include #include +#include +#include +#include #include int main() { From 61e577c8ac8889042428b2bf397311f2c792781b Mon Sep 17 00:00:00 2001 From: caesar Date: Tue, 8 Mar 2022 21:56:35 +0800 Subject: [PATCH 10/29] fix setFileName --- include/plog/Appenders/RollingDateFileAppender.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/plog/Appenders/RollingDateFileAppender.h b/include/plog/Appenders/RollingDateFileAppender.h index 0d2044f..3c555f5 100644 --- a/include/plog/Appenders/RollingDateFileAppender.h +++ b/include/plog/Appenders/RollingDateFileAppender.h @@ -41,6 +41,17 @@ namespace plog { last_date = util::get_zero_time(); } #endif + inline void setFileName(const util::nchar* fileName) + { + RollingFileAppender::setFileName(util::get_file_name(fileName).c_str()); + } +#ifdef _WIN32 + void setFileName(const char* fileName) + { + RollingFileAppender::setFileName(util::get_file_name(fileName).c_str()); + } +#endif + /** * From d4185ae7c685644977899c3c8e748021f385dc09 Mon Sep 17 00:00:00 2001 From: caesar Date: Thu, 10 Mar 2022 12:10:52 +0800 Subject: [PATCH 11/29] add PLOG_DISABLE_WCHAR_T option --- CMakeLists.txt | 5 +++++ include/plog/Appenders/ConsoleAppender.h | 2 +- include/plog/Appenders/DebugOutputAppender.h | 8 ++++++++ include/plog/Appenders/EventLogAppender.h | 6 ++++++ .../plog/Appenders/RollingDateFileAppender.h | 6 +++--- include/plog/Appenders/RollingFileAppender.h | 4 ++-- include/plog/Converters/NativeEOLConverter.h | 10 +++++----- include/plog/Converters/UTF8Converter.h | 2 +- include/plog/Init.h | 4 ++-- include/plog/Record.h | 4 ++-- include/plog/Util.h | 20 +++++++++---------- include/plog/WinApi.h | 2 ++ samples/RollingDateFileAppender/Main.cpp | 4 +++- 13 files changed, 50 insertions(+), 27 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7788a6a..f6bc972 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,9 +1,14 @@ cmake_minimum_required(VERSION 2.8) option(PLOG_BUILD_SAMPLES "Build plog's samples." ON) +option(PLOG_DISABLE_WCHAR_T "disable wchar_t ." OFF) project(plog LANGUAGES CXX) +if(PLOG_DISABLE_WCHAR_T) + add_definitions(-DPLOG_DISABLE_WCHAR_T) +endif() + # Make sure install paths work on all platforms. if(NOT CMAKE_INSTALL_INCLUDEDIR) include(GNUInstallDirs) diff --git a/include/plog/Appenders/ConsoleAppender.h b/include/plog/Appenders/ConsoleAppender.h index 211e59f..46d726c 100644 --- a/include/plog/Appenders/ConsoleAppender.h +++ b/include/plog/Appenders/ConsoleAppender.h @@ -33,7 +33,7 @@ namespace plog protected: void writestr(const util::nstring& str) { -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) if (m_isatty) { WriteConsoleW(m_stdoutHandle, str.c_str(), static_cast(str.size()), NULL, NULL); diff --git a/include/plog/Appenders/DebugOutputAppender.h b/include/plog/Appenders/DebugOutputAppender.h index 276f972..738e8f4 100644 --- a/include/plog/Appenders/DebugOutputAppender.h +++ b/include/plog/Appenders/DebugOutputAppender.h @@ -10,7 +10,15 @@ namespace plog public: virtual void write(const Record& record) { + +#if defined(PLOG_DISABLE_WCHAR_T) + std::wostringstream _str ; + _str << Formatter::format(record).c_str(); + std::wstring str = _str.str(); + OutputDebugStringW(str.c_str()); +#else OutputDebugStringW(Formatter::format(record).c_str()); +#endif } }; } diff --git a/include/plog/Appenders/EventLogAppender.h b/include/plog/Appenders/EventLogAppender.h index 587fd81..cdfcb58 100644 --- a/include/plog/Appenders/EventLogAppender.h +++ b/include/plog/Appenders/EventLogAppender.h @@ -19,7 +19,13 @@ namespace plog virtual void write(const Record& record) { +#if defined(PLOG_DISABLE_WCHAR_T) + std::wostringstream _str ; + _str << Formatter::format(record).c_str(); + std::wstring str = _str.str(); +#else std::wstring str = Formatter::format(record); +#endif const wchar_t* logMessagePtr[] = { str.c_str() }; ReportEventW(m_eventSource, logSeverityToType(record.getSeverity()), static_cast(record.getSeverity()), 0, NULL, 1, 0, logMessagePtr, NULL); diff --git a/include/plog/Appenders/RollingDateFileAppender.h b/include/plog/Appenders/RollingDateFileAppender.h index 3c555f5..f6b40fb 100644 --- a/include/plog/Appenders/RollingDateFileAppender.h +++ b/include/plog/Appenders/RollingDateFileAppender.h @@ -25,7 +25,7 @@ namespace plog { const util::nchar *m_fileName; public: -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) /** * * @param fileName "%Y-%m-%d-%H-%M-%S" @@ -45,7 +45,7 @@ namespace plog { { RollingFileAppender::setFileName(util::get_file_name(fileName).c_str()); } -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) void setFileName(const char* fileName) { RollingFileAppender::setFileName(util::get_file_name(fileName).c_str()); @@ -78,7 +78,7 @@ namespace plog { if (m_fileSize > 0) { int size = (int) m_fileSize; std::string lastFileName = util::get_file_name(m_fileName, -size); -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) util::File::unlink(util::toWide(lastFileName.c_str()).c_str()); #else util::File::unlink(lastFileName.c_str()); diff --git a/include/plog/Appenders/RollingFileAppender.h b/include/plog/Appenders/RollingFileAppender.h index c2a6d46..f12d971 100644 --- a/include/plog/Appenders/RollingFileAppender.h +++ b/include/plog/Appenders/RollingFileAppender.h @@ -23,7 +23,7 @@ namespace plog setFileName(fileName); } -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) RollingFileAppender(const char* fileName, size_t maxFileSize = 0, int maxFiles = 0) : m_fileSize() , m_maxFileSize((std::max)(maxFileSize, static_cast(1000))) // set a lower limit for the maxFileSize @@ -66,7 +66,7 @@ namespace plog m_firstWrite = true; } -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) void setFileName(const char* fileName) { setFileName(util::toWide(fileName).c_str()); diff --git a/include/plog/Converters/NativeEOLConverter.h b/include/plog/Converters/NativeEOLConverter.h index b0c199a..c7825a2 100644 --- a/include/plog/Converters/NativeEOLConverter.h +++ b/include/plog/Converters/NativeEOLConverter.h @@ -20,18 +20,18 @@ namespace plog } private: - static std::wstring fixLineEndings(const std::wstring& str) + static util::nstring fixLineEndings(const util::nstring& str) { - std::wstring output; + util::nstring output; output.reserve(str.length() * 2); for (size_t i = 0; i < str.size(); ++i) { - wchar_t ch = str[i]; + util::nchar ch = str[i]; - if (ch == L'\n') + if (ch == PLOG_NSTR('\n')) { - output.push_back(L'\r'); + output.push_back(PLOG_NSTR('\r')); } output.push_back(ch); diff --git a/include/plog/Converters/UTF8Converter.h b/include/plog/Converters/UTF8Converter.h index 86c9743..72a1f3e 100644 --- a/include/plog/Converters/UTF8Converter.h +++ b/include/plog/Converters/UTF8Converter.h @@ -13,7 +13,7 @@ namespace plog return std::string(kBOM) + convert(str); } -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) static std::string convert(const util::nstring& str) { return util::toNarrow(str, codePage::kUTF8); diff --git a/include/plog/Init.h b/include/plog/Init.h index c8cbf4f..38d6e5e 100644 --- a/include/plog/Init.h +++ b/include/plog/Init.h @@ -12,7 +12,7 @@ namespace plog bool isCsv(const util::nchar* fileName) { const util::nchar* dot = util::findExtensionDot(fileName); -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) return dot && 0 == std::wcscmp(dot, L".csv"); #else return dot && 0 == std::strcmp(dot, ".csv"); @@ -68,7 +68,7 @@ namespace plog ////////////////////////////////////////////////////////////////////////// // CHAR variants for Windows -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) template inline Logger& init(Severity maxSeverity, const char* fileName, size_t maxFileSize = 0, int maxFiles = 0) { diff --git a/include/plog/Record.h b/include/plog/Record.h index 106a19e..030ca29 100644 --- a/include/plog/Record.h +++ b/include/plog/Record.h @@ -18,9 +18,9 @@ namespace plog { data = data ? data : "(null)"; -#if defined(_WIN32) && defined(__BORLANDC__) +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) && defined(__BORLANDC__) stream << util::toWide(data); -#elif defined(_WIN32) +#elif !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) std::operator<<(stream, util::toWide(data)); #else std::operator<<(stream, data); diff --git a/include/plog/Util.h b/include/plog/Util.h index 0c46cb6..01baae7 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -9,7 +9,7 @@ #include #ifndef PLOG_ENABLE_WCHAR_INPUT -# ifdef _WIN32 +# if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) # define PLOG_ENABLE_WCHAR_INPUT 1 # else # define PLOG_ENABLE_WCHAR_INPUT 0 @@ -62,7 +62,7 @@ # endif #endif -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) # define _PLOG_NSTR(x) L##x # define PLOG_NSTR(x) _PLOG_NSTR(x) #ifndef CP_UTF8 @@ -76,7 +76,7 @@ namespace plog { namespace util { -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) typedef std::wstring nstring; typedef std::wostringstream nostringstream; typedef std::wistringstream nistringstream; @@ -241,7 +241,7 @@ namespace plog } #endif -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) inline std::wstring toWide(const char* str) { size_t len = ::strlen(str); @@ -309,7 +309,7 @@ namespace plog return str_time.str(); } -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) /** * "%Y-%m-%d-%H-%M-%S" * @param name @@ -349,7 +349,7 @@ namespace plog inline const nchar* findExtensionDot(const nchar* fileName) { -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) return std::wcsrchr(fileName, L'.'); #else return std::strrchr(fileName, '.'); @@ -403,9 +403,9 @@ namespace plog size_t open(const nchar* fileName) { -#if defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__)) +#if !defined(PLOG_DISABLE_WCHAR_T) and (defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__))) m_file = ::_wsopen(fileName, _O_CREAT | _O_WRONLY | _O_BINARY, SH_DENYWR, _S_IREAD | _S_IWRITE); -#elif defined(_WIN32) +#elif !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) ::_wsopen_s(&m_file, fileName, _O_CREAT | _O_WRONLY | _O_BINARY, _SH_DENYWR, _S_IREAD | _S_IWRITE); #else m_file = ::open(fileName, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); @@ -458,7 +458,7 @@ namespace plog static int unlink(const nchar* fileName) { -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) return ::_wunlink(fileName); #else return ::unlink(fileName); @@ -467,7 +467,7 @@ namespace plog static int rename(const nchar* oldFilename, const nchar* newFilename) { -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) return MoveFileW(oldFilename, newFilename); #else return ::rename(oldFilename, newFilename); diff --git a/include/plog/WinApi.h b/include/plog/WinApi.h index 737920d..9896869 100644 --- a/include/plog/WinApi.h +++ b/include/plog/WinApi.h @@ -139,6 +139,8 @@ namespace plog __declspec(dllimport) BOOL __stdcall SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttributes); __declspec(dllimport) void __stdcall OutputDebugStringW(LPCWSTR lpOutputString); + + __declspec(dllimport) void __stdcall OutputDebugStringA(LPCSTR lpOutputString); } inline void InitializeCriticalSection(CRITICAL_SECTION* criticalSection) diff --git a/samples/RollingDateFileAppender/Main.cpp b/samples/RollingDateFileAppender/Main.cpp index cc16eae..b843f38 100644 --- a/samples/RollingDateFileAppender/Main.cpp +++ b/samples/RollingDateFileAppender/Main.cpp @@ -6,12 +6,13 @@ #include #include #include +#include #include #include #include int main() { - static plog::RollingDateFileAppender dateFileAppender("Hello_date.%Y-%m-%d.log"); + static plog::RollingDateFileAppender dateFileAppender("Hello_date.%Y-%m-%d.log"); plog::init(plog::debug, &dateFileAppender); // Step2: initialize the logger. @@ -21,6 +22,7 @@ int main() { PLOGD << "Hello log!"; // short macro PLOG_DEBUG << "Hello log!"; // long macro PLOG(plog::debug) << "Hello log!"; // function-style macro + PLOG(plog::debug) << "Hello log! chinese test 中文测试"; // function-style macro } return 0; } From f12131a7e4e3b6776f87e52e1b802acc6630045b Mon Sep 17 00:00:00 2001 From: caesar Date: Thu, 10 Mar 2022 14:34:01 +0800 Subject: [PATCH 12/29] fix RollingDateFileAppender.h --- include/plog/Appenders/RollingDateFileAppender.h | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/include/plog/Appenders/RollingDateFileAppender.h b/include/plog/Appenders/RollingDateFileAppender.h index f6b40fb..51a36ac 100644 --- a/include/plog/Appenders/RollingDateFileAppender.h +++ b/include/plog/Appenders/RollingDateFileAppender.h @@ -22,7 +22,7 @@ namespace plog { private: time_t last_date; size_t m_fileSize; - const util::nchar *m_fileName; + util::nstring m_fileName; public: #if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) @@ -43,11 +43,13 @@ namespace plog { #endif inline void setFileName(const util::nchar* fileName) { + m_fileName = fileName; RollingFileAppender::setFileName(util::get_file_name(fileName).c_str()); } #if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) void setFileName(const char* fileName) { + m_fileName = util::toWide(fileName).c_str(); RollingFileAppender::setFileName(util::get_file_name(fileName).c_str()); } #endif @@ -73,11 +75,11 @@ namespace plog { inline void write(const Record &record) { if (record.getTime().time >= last_date) { - std::string path = util::get_file_name(m_fileName); + std::string path = util::get_file_name(m_fileName.c_str()); this->setFileName(path.c_str()); if (m_fileSize > 0) { int size = (int) m_fileSize; - std::string lastFileName = util::get_file_name(m_fileName, -size); + std::string lastFileName = util::get_file_name(m_fileName.c_str(), -size); #if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) util::File::unlink(util::toWide(lastFileName.c_str()).c_str()); #else From 61cf40f57736e113f8e9e8436d8235120aee78c2 Mon Sep 17 00:00:00 2001 From: caesar Date: Thu, 10 Mar 2022 23:29:02 +0800 Subject: [PATCH 13/29] fix m_firstWrite --- include/plog/Appenders/RollingDateFileAppender.h | 8 ++++++++ include/plog/Util.h | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/include/plog/Appenders/RollingDateFileAppender.h b/include/plog/Appenders/RollingDateFileAppender.h index 51a36ac..bb64caf 100644 --- a/include/plog/Appenders/RollingDateFileAppender.h +++ b/include/plog/Appenders/RollingDateFileAppender.h @@ -44,13 +44,21 @@ namespace plog { inline void setFileName(const util::nchar* fileName) { m_fileName = fileName; + bool flag = util::exists(m_fileName.c_str()); RollingFileAppender::setFileName(util::get_file_name(fileName).c_str()); + if(flag){ + RollingFileAppender::m_firstWrite = false; + } } #if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) void setFileName(const char* fileName) { m_fileName = util::toWide(fileName).c_str(); + bool flag = util::exists(m_fileName.c_str()); RollingFileAppender::setFileName(util::get_file_name(fileName).c_str()); + if(flag){ + RollingFileAppender::m_firstWrite = false; + } } #endif diff --git a/include/plog/Util.h b/include/plog/Util.h index 01baae7..37d653f 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -310,6 +310,10 @@ namespace plog } #if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) + inline static bool exists(const nchar *path_string) { + struct stat buffer = {}; + return (wstat(path_string, &buffer) == 0); + } /** * "%Y-%m-%d-%H-%M-%S" * @param name From d341b82baad7e3f3e979817a4a512193c81aecf5 Mon Sep 17 00:00:00 2001 From: caesar Date: Fri, 11 Mar 2022 11:53:20 +0800 Subject: [PATCH 14/29] fix setFileName --- include/plog/Appenders/RollingDateFileAppender.h | 14 ++++---------- samples/RollingDateFileAppender/Main.cpp | 4 ++-- 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/include/plog/Appenders/RollingDateFileAppender.h b/include/plog/Appenders/RollingDateFileAppender.h index bb64caf..c00beae 100644 --- a/include/plog/Appenders/RollingDateFileAppender.h +++ b/include/plog/Appenders/RollingDateFileAppender.h @@ -44,21 +44,15 @@ namespace plog { inline void setFileName(const util::nchar* fileName) { m_fileName = fileName; - bool flag = util::exists(m_fileName.c_str()); RollingFileAppender::setFileName(util::get_file_name(fileName).c_str()); - if(flag){ - RollingFileAppender::m_firstWrite = false; + if (!(util::exists(util::get_file_name(fileName).c_str()))) { + RollingFileAppender::rollLogFiles(); } } #if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) void setFileName(const char* fileName) { - m_fileName = util::toWide(fileName).c_str(); - bool flag = util::exists(m_fileName.c_str()); - RollingFileAppender::setFileName(util::get_file_name(fileName).c_str()); - if(flag){ - RollingFileAppender::m_firstWrite = false; - } + setFileName(util::toWide(fileName).c_str()); } #endif @@ -84,7 +78,7 @@ namespace plog { inline void write(const Record &record) { if (record.getTime().time >= last_date) { std::string path = util::get_file_name(m_fileName.c_str()); - this->setFileName(path.c_str()); + RollingFileAppender::setFileName(path.c_str()); if (m_fileSize > 0) { int size = (int) m_fileSize; std::string lastFileName = util::get_file_name(m_fileName.c_str(), -size); diff --git a/samples/RollingDateFileAppender/Main.cpp b/samples/RollingDateFileAppender/Main.cpp index b843f38..8038f72 100644 --- a/samples/RollingDateFileAppender/Main.cpp +++ b/samples/RollingDateFileAppender/Main.cpp @@ -12,9 +12,9 @@ #include int main() { - static plog::RollingDateFileAppender dateFileAppender("Hello_date.%Y-%m-%d.log"); - + static plog::RollingDateFileAppender dateFileAppender(""); + dateFileAppender.setFileName("Hello_date.%Y-%m-%d.log"); plog::init(plog::debug, &dateFileAppender); // Step2: initialize the logger. // Step3: write log messages using a special macro. There are several log macros, use the macro you liked the most. From f15fd264e94a586baff6d8ec28032daedceaf79d Mon Sep 17 00:00:00 2001 From: caesar Date: Wed, 16 Mar 2022 22:53:30 +0800 Subject: [PATCH 15/29] fix maxFiles --- include/plog/Appenders/RollingDateFileAppender.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/include/plog/Appenders/RollingDateFileAppender.h b/include/plog/Appenders/RollingDateFileAppender.h index c00beae..a198adf 100644 --- a/include/plog/Appenders/RollingDateFileAppender.h +++ b/include/plog/Appenders/RollingDateFileAppender.h @@ -41,14 +41,15 @@ namespace plog { last_date = util::get_zero_time(); } #endif - inline void setFileName(const util::nchar* fileName) - { + + inline void setFileName(const util::nchar *fileName) { m_fileName = fileName; RollingFileAppender::setFileName(util::get_file_name(fileName).c_str()); if (!(util::exists(util::get_file_name(fileName).c_str()))) { RollingFileAppender::rollLogFiles(); } } + #if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) void setFileName(const char* fileName) { @@ -71,7 +72,7 @@ namespace plog { last_date = 0; } - inline void setMaxFiles(int maxFiles) { + inline void setMaxFiles(size_t maxFiles) { m_fileSize = maxFiles; } @@ -79,7 +80,7 @@ namespace plog { if (record.getTime().time >= last_date) { std::string path = util::get_file_name(m_fileName.c_str()); RollingFileAppender::setFileName(path.c_str()); - if (m_fileSize > 0) { + if (path != m_fileName && m_fileSize > 0) { int size = (int) m_fileSize; std::string lastFileName = util::get_file_name(m_fileName.c_str(), -size); #if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) From 767d94d26d21537c6beb3ac888f5b331d6c3c609 Mon Sep 17 00:00:00 2001 From: caesar Date: Mon, 19 Dec 2022 14:10:34 +0800 Subject: [PATCH 16/29] update RollingDateFileAppender.h --- CMakeLists.txt | 4 + include/plog/Appenders/ConsoleAppender.h | 2 +- include/plog/Appenders/DebugOutputAppender.h | 7 + include/plog/Appenders/EventLogAppender.h | 6 + .../plog/Appenders/RollingDateFileAppender.h | 2 +- include/plog/Appenders/RollingFileAppender.h | 15 +- include/plog/Converters/NativeEOLConverter.h | 2 +- include/plog/Converters/UTF8Converter.h | 2 +- include/plog/Record.h | 12 +- include/plog/Util.h | 145 ++++++++++-------- include/plog/WinApi.h | 2 + samples/RollingDateFileAppender/Main.cpp | 2 +- 12 files changed, 117 insertions(+), 84 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d5ffcf..7af94cb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,8 +11,12 @@ endif() # options option(PLOG_BUILD_SAMPLES "Build ${PROJECT_NAME} samples" ${IS_TOPLEVEL_PROJECT}) +option(PLOG_DISABLE_WCHAR_T "disable wchar_t ." OFF) option(PLOG_INSTALL "Generate ${PROJECT_NAME} install target" ${IS_TOPLEVEL_PROJECT}) +if(PLOG_DISABLE_WCHAR_T) + add_definitions(-DPLOG_DISABLE_WCHAR_T) +endif() # make sure install paths work on all platforms include(GNUInstallDirs) diff --git a/include/plog/Appenders/ConsoleAppender.h b/include/plog/Appenders/ConsoleAppender.h index 23f4bc6..0fa3227 100644 --- a/include/plog/Appenders/ConsoleAppender.h +++ b/include/plog/Appenders/ConsoleAppender.h @@ -48,7 +48,7 @@ namespace plog protected: void writestr(const util::nstring& str) { -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) if (m_isatty) { WriteConsoleW(m_outputHandle, str.c_str(), static_cast(str.size()), NULL, NULL); diff --git a/include/plog/Appenders/DebugOutputAppender.h b/include/plog/Appenders/DebugOutputAppender.h index 64ba173..056babc 100644 --- a/include/plog/Appenders/DebugOutputAppender.h +++ b/include/plog/Appenders/DebugOutputAppender.h @@ -10,7 +10,14 @@ namespace plog public: virtual void write(const Record& record) PLOG_OVERRIDE { +#if defined(PLOG_DISABLE_WCHAR_T) + std::wostringstream _str ; + _str << Formatter::format(record).c_str(); + std::wstring str = _str.str(); + OutputDebugStringW(str.c_str()); +#else OutputDebugStringW(Formatter::format(record).c_str()); +#endif } }; } diff --git a/include/plog/Appenders/EventLogAppender.h b/include/plog/Appenders/EventLogAppender.h index 30a6c7f..8664b8f 100644 --- a/include/plog/Appenders/EventLogAppender.h +++ b/include/plog/Appenders/EventLogAppender.h @@ -19,7 +19,13 @@ namespace plog virtual void write(const Record& record) PLOG_OVERRIDE { +#if defined(PLOG_DISABLE_WCHAR_T) + std::wostringstream _str ; + _str << Formatter::format(record).c_str(); + std::wstring str = _str.str(); +#else std::wstring str = Formatter::format(record); +#endif const wchar_t* logMessagePtr[] = { str.c_str() }; ReportEventW(m_eventSource, logSeverityToType(record.getSeverity()), static_cast(record.getSeverity()), 0, NULL, 1, 0, logMessagePtr, NULL); diff --git a/include/plog/Appenders/RollingDateFileAppender.h b/include/plog/Appenders/RollingDateFileAppender.h index 388c9b8..8b78bdf 100644 --- a/include/plog/Appenders/RollingDateFileAppender.h +++ b/include/plog/Appenders/RollingDateFileAppender.h @@ -26,7 +26,7 @@ namespace plog { const util::nchar *m_fileName; public: -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) /** * * @param fileName "%Y-%m-%d-%H-%M-%S" diff --git a/include/plog/Appenders/RollingFileAppender.h b/include/plog/Appenders/RollingFileAppender.h index 1bd0755..3f00f00 100644 --- a/include/plog/Appenders/RollingFileAppender.h +++ b/include/plog/Appenders/RollingFileAppender.h @@ -7,18 +7,15 @@ namespace plog { - template - class RollingDateFileAppender; template > - class PLOG_LINKAGE_HIDDEN RollingFileAppender : public IAppender + class RollingFileAppender : public IAppender { - friend class RollingDateFileAppender; public: RollingFileAppender(const util::nchar* fileName, size_t maxFileSize = 0, int maxFiles = 0) - : m_fileSize() - , m_maxFileSize() - , m_maxFiles(maxFiles) - , m_firstWrite(true) + : m_fileSize() + , m_maxFileSize() + , m_maxFiles(maxFiles) + , m_firstWrite(true) { setFileName(fileName); setMaxFileSize(maxFileSize); @@ -36,7 +33,7 @@ namespace plog } #endif - virtual void write(const Record& record) PLOG_OVERRIDE + virtual void write(const Record& record) { util::MutexLock lock(m_mutex); diff --git a/include/plog/Converters/NativeEOLConverter.h b/include/plog/Converters/NativeEOLConverter.h index b0c199a..0bff621 100644 --- a/include/plog/Converters/NativeEOLConverter.h +++ b/include/plog/Converters/NativeEOLConverter.h @@ -8,7 +8,7 @@ namespace plog class NativeEOLConverter : public NextConverter { #ifdef _WIN32 - public: + public: static std::string header(const util::nstring& str) { return NextConverter::header(fixLineEndings(str)); diff --git a/include/plog/Converters/UTF8Converter.h b/include/plog/Converters/UTF8Converter.h index 86c9743..72a1f3e 100644 --- a/include/plog/Converters/UTF8Converter.h +++ b/include/plog/Converters/UTF8Converter.h @@ -13,7 +13,7 @@ namespace plog return std::string(kBOM) + convert(str); } -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) static std::string convert(const util::nstring& str) { return util::toNarrow(str, codePage::kUTF8); diff --git a/include/plog/Record.h b/include/plog/Record.h index c0cb094..dc5797d 100644 --- a/include/plog/Record.h +++ b/include/plog/Record.h @@ -86,7 +86,7 @@ namespace plog ////////////////////////////////////////////////////////////////////////// // Stream output operators as free functions -#if PLOG_ENABLE_WCHAR_INPUT +#if !defined(PLOG_DISABLE_WCHAR_T) && PLOG_ENABLE_WCHAR_INPUT inline void operator<<(util::nostringstream& stream, const wchar_t* data) { data = data ? data : L"(null)"; @@ -113,9 +113,9 @@ namespace plog { data = data ? data : "(null)"; -#if defined(_WIN32) && defined(__BORLANDC__) +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) && defined(__BORLANDC__) stream << util::toWide(data); -#elif defined(_WIN32) +#elif !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) std::operator<<(stream, util::toWide(data)); #else std::operator<<(stream, data); @@ -246,7 +246,7 @@ namespace plog return *this << str; } -#if PLOG_ENABLE_WCHAR_INPUT +#if !defined(PLOG_DISABLE_WCHAR_T) && PLOG_ENABLE_WCHAR_INPUT Record& operator<<(wchar_t data) { wchar_t str[] = { data, 0 }; @@ -254,7 +254,7 @@ namespace plog } #endif -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) Record& operator<<(std::wostream& (PLOG_CDECL *data)(std::wostream&)) #else Record& operator<<(std::ostream& (*data)(std::ostream&)) @@ -317,7 +317,7 @@ namespace plog return *this; } -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) Record& printf(const wchar_t* format, ...) { using namespace util; diff --git a/include/plog/Util.h b/include/plog/Util.h index 8012688..f49e276 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -4,11 +4,12 @@ #include #include #include +#include #include #include #ifndef PLOG_ENABLE_WCHAR_INPUT -# ifdef _WIN32 +# if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) # define PLOG_ENABLE_WCHAR_INPUT 1 # else # define PLOG_ENABLE_WCHAR_INPUT 0 @@ -29,7 +30,6 @@ # define PLOG_LINKAGE __attribute__ ((visibility ("default"))) # elif defined(PLOG_LOCAL) # define PLOG_LINKAGE __attribute__ ((visibility ("hidden"))) -# define PLOG_LINKAGE_HIDDEN PLOG_LINKAGE # endif # if defined(PLOG_EXPORT) || defined(PLOG_IMPORT) # error "PLOG_EXPORT/PLOG_IMPORT is supported only on Windows" @@ -50,23 +50,29 @@ # include # include # include +#elif defined(__rtems__) +# include +# include +# if PLOG_ENABLE_WCHAR_INPUT +# include +# endif #else # include # include # if defined(__linux__) || defined(__FreeBSD__) -# include +# include # elif defined(__rtems__) # include # endif # if defined(_POSIX_THREADS) -# include +# include # endif # if PLOG_ENABLE_WCHAR_INPUT # include # endif #endif -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) # define _PLOG_NSTR(x) L##x # define PLOG_NSTR(x) _PLOG_NSTR(x) #else @@ -89,7 +95,7 @@ namespace plog { namespace util { -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) typedef std::wstring nstring; typedef std::wostringstream nostringstream; typedef std::wistringstream nistringstream; @@ -127,50 +133,6 @@ namespace plog #endif } - inline static bool exists(const char *path_string) { - struct stat buffer = {}; - return (stat(path_string, &buffer) == 0); - } - inline static time_t get_zero_time() { - time_t t = time(nullptr); - struct tm *tm = localtime(&t); - tm->tm_mday += 1; - tm->tm_hour = 0; - tm->tm_min = 0; - tm->tm_sec = 0; - return mktime(tm); - } - -#ifdef _WIN32 - /** - * "%Y-%m-%d-%H-%M-%S" - * @param name - * @return - */ - inline static std::string get_file_name(const util::nchar *name) { - return get_file_name(util::toNarrow(name,CP_UTF8).c_str()); - } -#endif - - /** - * "%Y-%m-%d-%H-%M-%S" - * @param name - * @return - */ - inline static std::string get_file_name(const char *name, int day = 0) { - std::stringstream str_time; - std::time_t current_time = std::time(nullptr); - if (day != 0) { - struct tm *tm = localtime(¤t_time); - tm->tm_mday += day; - current_time = mktime(tm); - } - char tAll[255]; - std::strftime(tAll, sizeof(tAll), name, std::localtime(¤t_time)); - str_time << tAll; - return str_time.str(); - } - #ifdef _WIN32 typedef timeb Time; @@ -217,7 +179,7 @@ namespace plog } #ifdef _WIN32 - inline int vasprintf(char** strp, const char* format, va_list ap) + inline int vasprintf(char** strp, const char* format, va_list ap) { #if defined(__BORLANDC__) int charCount = 0x1000; // there is no _vscprintf on Borland/Embarcadero @@ -316,7 +278,7 @@ namespace plog } #endif -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) inline std::wstring toWide(const char* str) { size_t len = ::strlen(str); @@ -345,6 +307,61 @@ namespace plog } #endif + inline static bool exists(const char *path_string) { + struct stat buffer = {}; + return (stat(path_string, &buffer) == 0); + } + + inline static time_t get_zero_time() { + time_t t = time(NULL); + tm tm; + util::localtime_s(&tm, &t); + + tm.tm_mday += 1; + tm.tm_hour = 0; + tm.tm_min = 0; + tm.tm_sec = 0; + return mktime(&tm); + } + + /** + * "%Y-%m-%d-%H-%M-%S" + * @param name + * @return + */ + inline static std::string get_file_name(const char *name, int day = 0) { + std::stringstream str_time; + std::time_t current_time = std::time(NULL); + if (day != 0) { + tm tm; + util::localtime_s(&tm, ¤t_time); + tm.tm_mday += day; + current_time = mktime(&tm); + } + char tAll[255]; + tm t; + util::localtime_s(&t, ¤t_time); + std::strftime(tAll, sizeof(tAll), name, &t); + str_time << tAll; + return str_time.str(); + } + +#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) + inline static bool exists(const nchar *path_string) { + struct stat buffer = {}; + return (wstat(path_string, &buffer) == 0); + } + /** + * "%Y-%m-%d-%H-%M-%S" + * @param name + * @return + */ + inline static std::string get_file_name(const util::nchar *name, int day = 0) { + return get_file_name(util::toNarrow(name,CP_UTF8).c_str(),day); + } +#endif + + inline std::string processFuncName(const char* func) { #if (defined(_WIN32) && !defined(__MINGW32__)) || defined(__OBJC__) @@ -373,7 +390,7 @@ namespace plog inline const nchar* findExtensionDot(const nchar* fileName) { -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) return std::wcsrchr(fileName, L'.'); #else return std::strrchr(fileName, '.'); @@ -427,9 +444,9 @@ namespace plog size_t open(const nchar* fileName) { -#if defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__)) +#if !defined(PLOG_DISABLE_WCHAR_T) and (defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__))) m_file = ::_wsopen(fileName, _O_CREAT | _O_WRONLY | _O_BINARY | _O_NOINHERIT, SH_DENYWR, _S_IREAD | _S_IWRITE); -#elif defined(_WIN32) +#elif !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) ::_wsopen_s(&m_file, fileName, _O_CREAT | _O_WRONLY | _O_BINARY | _O_NOINHERIT, _SH_DENYWR, _S_IREAD | _S_IWRITE); #elif defined(O_CLOEXEC) m_file = ::open(fileName, O_CREAT | O_APPEND | O_WRONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); @@ -443,11 +460,11 @@ namespace plog { return m_file != -1 ? static_cast( #ifdef _WIN32 - ::_write(m_file, buf, static_cast(count)) + ::_write(m_file, buf, static_cast(count)) #else - ::write(m_file, buf, count) + ::write(m_file, buf, count) #endif - ) : static_cast(-1); + ) : static_cast(-1); } template @@ -460,13 +477,13 @@ namespace plog { return m_file != -1 ? static_cast( #if defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__)) - ::_lseek(m_file, static_cast(offset), whence) + ::_lseek(m_file, static_cast(offset), whence) #elif defined(_WIN32) - ::_lseeki64(m_file, static_cast(offset), whence) + ::_lseeki64(m_file, static_cast(offset), whence) #else - ::lseek(m_file, static_cast(offset), whence) + ::lseek(m_file, static_cast(offset), whence) #endif - ) : static_cast(-1); + ) : static_cast(-1); } void close() @@ -484,7 +501,7 @@ namespace plog static int unlink(const nchar* fileName) { -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) return ::_wunlink(fileName); #else return ::unlink(fileName); @@ -493,7 +510,7 @@ namespace plog static int rename(const nchar* oldFilename, const nchar* newFilename) { -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) return MoveFileW(oldFilename, newFilename); #else return ::rename(oldFilename, newFilename); diff --git a/include/plog/WinApi.h b/include/plog/WinApi.h index ce57998..9eb20ff 100644 --- a/include/plog/WinApi.h +++ b/include/plog/WinApi.h @@ -140,6 +140,8 @@ namespace plog __declspec(dllimport) BOOL __stdcall SetConsoleTextAttribute(HANDLE hConsoleOutput, WORD wAttributes); __declspec(dllimport) void __stdcall OutputDebugStringW(LPCWSTR lpOutputString); + + __declspec(dllimport) void __stdcall OutputDebugStringA(LPCSTR lpOutputString); } inline void InitializeCriticalSection(CRITICAL_SECTION* criticalSection) diff --git a/samples/RollingDateFileAppender/Main.cpp b/samples/RollingDateFileAppender/Main.cpp index aa40187..4d8e3fe 100644 --- a/samples/RollingDateFileAppender/Main.cpp +++ b/samples/RollingDateFileAppender/Main.cpp @@ -19,7 +19,7 @@ int main() { PLOGD << "Hello log!"; // short macro PLOG_DEBUG << "Hello log!"; // long macro PLOG(plog::debug) << "Hello log!"; // function-style macro - std::this_thread::sleep_for(std::chrono::seconds(2)); + PLOG(plog::debug) << "中文测试"; // function-style macro } return 0; } From 6297d67cfa92b5cefe58d0bd321606bd995f233a Mon Sep 17 00:00:00 2001 From: caesar Date: Mon, 19 Dec 2022 14:16:38 +0800 Subject: [PATCH 17/29] update RollingDateFileAppender.h --- samples/RollingDateFileAppender/Main.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/samples/RollingDateFileAppender/Main.cpp b/samples/RollingDateFileAppender/Main.cpp index 4d8e3fe..bd96f8b 100644 --- a/samples/RollingDateFileAppender/Main.cpp +++ b/samples/RollingDateFileAppender/Main.cpp @@ -5,7 +5,6 @@ #include // Step1: include the header. #include #include -#include int main() { using plog::TxtFormatter; From e139792256cd6f03537af9bf09fb8767cec4868b Mon Sep 17 00:00:00 2001 From: caesar Date: Mon, 19 Dec 2022 14:23:55 +0800 Subject: [PATCH 18/29] fix RollingDateFileAppender.h include --- include/plog/Appenders/RollingDateFileAppender.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/include/plog/Appenders/RollingDateFileAppender.h b/include/plog/Appenders/RollingDateFileAppender.h index 8b78bdf..4c0dd8f 100644 --- a/include/plog/Appenders/RollingDateFileAppender.h +++ b/include/plog/Appenders/RollingDateFileAppender.h @@ -5,13 +5,7 @@ #ifndef TERMINALSERVICE_ROLLINGDATEFILEAPPENDER_H #define TERMINALSERVICE_ROLLINGDATEFILEAPPENDER_H -#include -#include -#include -#include -#include #include -#include #ifdef _WIN32 #include From e7de6a857dcafd07e3ec7baae2e8dc2383a112b4 Mon Sep 17 00:00:00 2001 From: caesar Date: Mon, 19 Dec 2022 14:29:45 +0800 Subject: [PATCH 19/29] fix PLOG_LINKAGE_HIDDEN --- include/plog/Util.h | 154 +++++++++-------------- samples/RollingDateFileAppender/Main.cpp | 1 + 2 files changed, 63 insertions(+), 92 deletions(-) diff --git a/include/plog/Util.h b/include/plog/Util.h index f49e276..54048c9 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -1,4 +1,5 @@ #pragma once + #include #include #include @@ -30,6 +31,7 @@ # define PLOG_LINKAGE __attribute__ ((visibility ("default"))) # elif defined(PLOG_LOCAL) # define PLOG_LINKAGE __attribute__ ((visibility ("hidden"))) +# define PLOG_LINKAGE_HIDDEN PLOG_LINKAGE # endif # if defined(PLOG_EXPORT) || defined(PLOG_IMPORT) # error "PLOG_EXPORT/PLOG_IMPORT is supported only on Windows" @@ -57,15 +59,19 @@ # include # endif #else + # include # include + # if defined(__linux__) || defined(__FreeBSD__) # include # elif defined(__rtems__) # include # endif # if defined(_POSIX_THREADS) + # include + # endif # if PLOG_ENABLE_WCHAR_INPUT # include @@ -91,10 +97,8 @@ # define PLOG_OVERRIDE #endif -namespace plog -{ - namespace util - { +namespace plog { + namespace util { #if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) typedef std::wstring nstring; typedef std::wostringstream nostringstream; @@ -107,8 +111,7 @@ namespace plog typedef char nchar; #endif - inline void localtime_s(struct tm* t, const time_t* time) - { + inline void localtime_s(struct tm *t, const time_t *time) { #if defined(_WIN32) && defined(__BORLANDC__) ::localtime_s(time, t); #elif defined(_WIN32) && defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) @@ -120,8 +123,7 @@ namespace plog #endif } - inline void gmtime_s(struct tm* t, const time_t* time) - { + inline void gmtime_s(struct tm *t, const time_t *time) { #if defined(_WIN32) && defined(__BORLANDC__) ::gmtime_s(time, t); #elif defined(_WIN32) && defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) @@ -141,24 +143,22 @@ namespace plog ::ftime(t); } #else - struct Time - { + struct Time { time_t time; unsigned short millitm; }; - inline void ftime(Time* t) - { + inline void ftime(Time *t) { timeval tv; ::gettimeofday(&tv, NULL); t->time = tv.tv_sec; t->millitm = static_cast(tv.tv_usec / 1000); } + #endif - inline unsigned int gettid() - { + inline unsigned int gettid() { #ifdef _WIN32 return GetCurrentThreadId(); #elif defined(__linux__) @@ -308,7 +308,7 @@ namespace plog #endif inline static bool exists(const char *path_string) { - struct stat buffer = {}; + struct stat buffer; return (stat(path_string, &buffer) == 0); } @@ -348,7 +348,7 @@ namespace plog #if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) inline static bool exists(const nchar *path_string) { - struct stat buffer = {}; + struct stat buffer; return (wstat(path_string, &buffer) == 0); } /** @@ -362,23 +362,20 @@ namespace plog #endif - inline std::string processFuncName(const char* func) - { + inline std::string processFuncName(const char *func) { #if (defined(_WIN32) && !defined(__MINGW32__)) || defined(__OBJC__) return std::string(func); #else - const char* funcBegin = func; - const char* funcEnd = ::strchr(funcBegin, '('); + const char *funcBegin = func; + const char *funcEnd = ::strchr(funcBegin, '('); - if (!funcEnd) - { + if (!funcEnd) { return std::string(func); } - for (const char* i = funcEnd - 1; i >= funcBegin; --i) // search backwards for the first space char + for (const char *i = funcEnd - 1; i >= funcBegin; --i) // search backwards for the first space char { - if (*i == ' ') - { + if (*i == ' ') { funcBegin = i + 1; break; } @@ -388,8 +385,7 @@ namespace plog #endif } - inline const nchar* findExtensionDot(const nchar* fileName) - { + inline const nchar *findExtensionDot(const nchar *fileName) { #if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) return std::wcsrchr(fileName, L'.'); #else @@ -397,67 +393,57 @@ namespace plog #endif } - inline void splitFileName(const nchar* fileName, nstring& fileNameNoExt, nstring& fileExt) - { - const nchar* dot = findExtensionDot(fileName); + inline void splitFileName(const nchar *fileName, nstring &fileNameNoExt, nstring &fileExt) { + const nchar *dot = findExtensionDot(fileName); - if (dot) - { + if (dot) { fileNameNoExt.assign(fileName, dot); fileExt.assign(dot + 1); - } - else - { + } else { fileNameNoExt.assign(fileName); fileExt.clear(); } } - class PLOG_LINKAGE NonCopyable - { + class PLOG_LINKAGE NonCopyable { protected: - NonCopyable() - { + NonCopyable() { } private: - NonCopyable(const NonCopyable&); - NonCopyable& operator=(const NonCopyable&); + NonCopyable(const NonCopyable &); + + NonCopyable &operator=(const NonCopyable &); }; - class PLOG_LINKAGE_HIDDEN File : NonCopyable - { + class PLOG_LINKAGE_HIDDEN File : NonCopyable { public: - File() : m_file(-1) - { + File() : m_file(-1) { } - File(const nchar* fileName) : m_file(-1) - { + File(const nchar *fileName) : m_file(-1) { open(fileName); } - ~File() - { + ~File() { close(); } - size_t open(const nchar* fileName) - { + size_t open(const nchar *fileName) { #if !defined(PLOG_DISABLE_WCHAR_T) and (defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__))) m_file = ::_wsopen(fileName, _O_CREAT | _O_WRONLY | _O_BINARY | _O_NOINHERIT, SH_DENYWR, _S_IREAD | _S_IWRITE); #elif !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) ::_wsopen_s(&m_file, fileName, _O_CREAT | _O_WRONLY | _O_BINARY | _O_NOINHERIT, _SH_DENYWR, _S_IREAD | _S_IWRITE); #elif defined(O_CLOEXEC) - m_file = ::open(fileName, O_CREAT | O_APPEND | O_WRONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + m_file = ::open(fileName, O_CREAT | O_APPEND | O_WRONLY | O_CLOEXEC, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); #else m_file = ::open(fileName, O_CREAT | O_APPEND | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); #endif return seek(0, SEEK_END); } - size_t write(const void* buf, size_t count) - { + size_t write(const void *buf, size_t count) { return m_file != -1 ? static_cast( #ifdef _WIN32 ::_write(m_file, buf, static_cast(count)) @@ -468,13 +454,11 @@ namespace plog } template - size_t write(const std::basic_string& str) - { + size_t write(const std::basic_string &str) { return write(str.data(), str.size() * sizeof(CharType)); } - size_t seek(size_t offset, int whence) - { + size_t seek(size_t offset, int whence) { return m_file != -1 ? static_cast( #if defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__)) ::_lseek(m_file, static_cast(offset), whence) @@ -486,10 +470,8 @@ namespace plog ) : static_cast(-1); } - void close() - { - if (m_file != -1) - { + void close() { + if (m_file != -1) { #ifdef _WIN32 ::_close(m_file); #else @@ -499,8 +481,7 @@ namespace plog } } - static int unlink(const nchar* fileName) - { + static int unlink(const nchar *fileName) { #if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) return ::_wunlink(fileName); #else @@ -508,8 +489,7 @@ namespace plog #endif } - static int rename(const nchar* oldFilename, const nchar* newFilename) - { + static int rename(const nchar *oldFilename, const nchar *newFilename) { #if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) return MoveFileW(oldFilename, newFilename); #else @@ -521,11 +501,9 @@ namespace plog int m_file; }; - class PLOG_LINKAGE_HIDDEN Mutex : NonCopyable - { + class PLOG_LINKAGE_HIDDEN Mutex : NonCopyable { public: - Mutex() - { + Mutex() { #ifdef _WIN32 InitializeCriticalSection(&m_sync); #elif defined(__rtems__) @@ -538,8 +516,7 @@ namespace plog #endif } - ~Mutex() - { + ~Mutex() { #ifdef _WIN32 DeleteCriticalSection(&m_sync); #elif defined(__rtems__) @@ -552,8 +529,7 @@ namespace plog friend class MutexLock; private: - void lock() - { + void lock() { #ifdef _WIN32 EnterCriticalSection(&m_sync); #elif defined(__rtems__) @@ -563,8 +539,7 @@ namespace plog #endif } - void unlock() - { + void unlock() { #ifdef _WIN32 LeaveCriticalSection(&m_sync); #elif defined(__rtems__) @@ -584,21 +559,18 @@ namespace plog #endif }; - class PLOG_LINKAGE_HIDDEN MutexLock : NonCopyable - { + class PLOG_LINKAGE_HIDDEN MutexLock : NonCopyable { public: - MutexLock(Mutex& mutex) : m_mutex(mutex) - { + MutexLock(Mutex &mutex) : m_mutex(mutex) { m_mutex.lock(); } - ~MutexLock() - { + ~MutexLock() { m_mutex.unlock(); } private: - Mutex& m_mutex; + Mutex &m_mutex; }; template @@ -610,32 +582,30 @@ namespace plog { public: #if (defined(__clang__) || defined(__GNUC__) && __GNUC__ >= 8) && !defined(__BORLANDC__) + // This constructor is called before the `T` object is fully constructed, and // pointers are not dereferenced anyway, so UBSan shouldn't check vptrs. __attribute__((no_sanitize("vptr"))) #endif - Singleton() - { + Singleton() { assert(!m_instance); - m_instance = static_cast(this); + m_instance = static_cast(this); } - ~Singleton() - { + ~Singleton() { assert(m_instance); m_instance = 0; } - static T* getInstance() - { + static T *getInstance() { return m_instance; } private: - static T* m_instance; + static T *m_instance; }; template - T* Singleton::m_instance = NULL; + T *Singleton::m_instance = NULL; } } diff --git a/samples/RollingDateFileAppender/Main.cpp b/samples/RollingDateFileAppender/Main.cpp index bd96f8b..b21dab9 100644 --- a/samples/RollingDateFileAppender/Main.cpp +++ b/samples/RollingDateFileAppender/Main.cpp @@ -6,6 +6,7 @@ #include #include + int main() { using plog::TxtFormatter; static plog::RollingDateFileAppender dateFileAppender("Hello_date.%Y-%m-%d.log"); From 03cbf0a9d76f4cccee2274a69992745bbd46303b Mon Sep 17 00:00:00 2001 From: caesar Date: Mon, 19 Dec 2022 14:33:06 +0800 Subject: [PATCH 20/29] rollback PLOG_OVERRIDE --- include/plog/Appenders/RollingDateFileAppender.h | 2 +- include/plog/Appenders/RollingFileAppender.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/plog/Appenders/RollingDateFileAppender.h b/include/plog/Appenders/RollingDateFileAppender.h index 4c0dd8f..6e8fe80 100644 --- a/include/plog/Appenders/RollingDateFileAppender.h +++ b/include/plog/Appenders/RollingDateFileAppender.h @@ -51,7 +51,7 @@ namespace plog { last_date = 0; } - inline void write(const Record &record) { + inline void write(const Record &record) PLOG_OVERRIDE { if (record.getTime().time >= last_date) { std::string path = util::get_file_name(m_fileName); this->setFileName(path.c_str()); diff --git a/include/plog/Appenders/RollingFileAppender.h b/include/plog/Appenders/RollingFileAppender.h index 3f00f00..1d0047a 100644 --- a/include/plog/Appenders/RollingFileAppender.h +++ b/include/plog/Appenders/RollingFileAppender.h @@ -33,7 +33,7 @@ namespace plog } #endif - virtual void write(const Record& record) + virtual void write(const Record& record) PLOG_OVERRIDE { util::MutexLock lock(m_mutex); From 38f1f113a4a41157ca2b6260a3bce9eb8e8900e0 Mon Sep 17 00:00:00 2001 From: caesar Date: Mon, 19 Dec 2022 14:37:02 +0800 Subject: [PATCH 21/29] rollback PLOG_LINKAGE_HIDDEN --- include/plog/Appenders/RollingDateFileAppender.h | 2 +- include/plog/Appenders/RollingFileAppender.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/plog/Appenders/RollingDateFileAppender.h b/include/plog/Appenders/RollingDateFileAppender.h index 6e8fe80..31ab6c4 100644 --- a/include/plog/Appenders/RollingDateFileAppender.h +++ b/include/plog/Appenders/RollingDateFileAppender.h @@ -13,7 +13,7 @@ namespace plog { template > - class RollingDateFileAppender : public RollingFileAppender { + class PLOG_LINKAGE_HIDDEN RollingDateFileAppender : public RollingFileAppender { private: time_t last_date; size_t m_fileSize; diff --git a/include/plog/Appenders/RollingFileAppender.h b/include/plog/Appenders/RollingFileAppender.h index 1d0047a..5323c15 100644 --- a/include/plog/Appenders/RollingFileAppender.h +++ b/include/plog/Appenders/RollingFileAppender.h @@ -8,7 +8,7 @@ namespace plog { template > - class RollingFileAppender : public IAppender + class PLOG_LINKAGE_HIDDEN RollingFileAppender : public IAppender { public: RollingFileAppender(const util::nchar* fileName, size_t maxFileSize = 0, int maxFiles = 0) From 0c6683b0e9db66030cfd8dbecce0f81b6945c4ed Mon Sep 17 00:00:00 2001 From: caesar Date: Mon, 19 Dec 2022 15:14:24 +0800 Subject: [PATCH 22/29] update 1.1.9 --- include/plog/Init.h | 88 ++---------------------- include/plog/Util.h | 161 ++++++++++++++++++++------------------------ 2 files changed, 77 insertions(+), 172 deletions(-) diff --git a/include/plog/Init.h b/include/plog/Init.h index cc19f34..daa7f21 100644 --- a/include/plog/Init.h +++ b/include/plog/Init.h @@ -1,95 +1,15 @@ #pragma once -#include -#include -#include -#include -#include - -namespace plog -{ - namespace - { - bool isCsv(const util::nchar* fileName) - { - const util::nchar* dot = util::findExtensionDot(fileName); -#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) - return dot && 0 == std::wcscmp(dot, L".csv"); -#else - return dot && 0 == std::strcmp(dot, ".csv"); -#endif - } - } - ////////////////////////////////////////////////////////////////////////// - // Empty initializer / one appender +#include +namespace plog { template - PLOG_LINKAGE_HIDDEN inline Logger& init(Severity maxSeverity = none, IAppender* appender = NULL) - { + PLOG_LINKAGE_HIDDEN inline Logger &init(Severity maxSeverity = none, IAppender *appender = NULL) { static Logger logger(maxSeverity); return appender ? logger.addAppender(appender) : logger; } - inline Logger& init(Severity maxSeverity = none, IAppender* appender = NULL) - { + inline Logger &init(Severity maxSeverity = none, IAppender *appender = NULL) { return init(maxSeverity, appender); } - - ////////////////////////////////////////////////////////////////////////// - // RollingFileAppender with any Formatter - - template - inline Logger& init(Severity maxSeverity, const util::nchar* fileName, size_t maxFileSize = 0, int maxFiles = 0) - { - static RollingFileAppender rollingFileAppender(fileName, maxFileSize, maxFiles); - return init(maxSeverity, &rollingFileAppender); - } - - template - inline Logger& init(Severity maxSeverity, const util::nchar* fileName, size_t maxFileSize = 0, int maxFiles = 0) - { - return init(maxSeverity, fileName, maxFileSize, maxFiles); - } - - ////////////////////////////////////////////////////////////////////////// - // RollingFileAppender with TXT/CSV chosen by file extension - - template - inline Logger& init(Severity maxSeverity, const util::nchar* fileName, size_t maxFileSize = 0, int maxFiles = 0) - { - return isCsv(fileName) ? init(maxSeverity, fileName, maxFileSize, maxFiles) : init(maxSeverity, fileName, maxFileSize, maxFiles); - } - - inline Logger& init(Severity maxSeverity, const util::nchar* fileName, size_t maxFileSize = 0, int maxFiles = 0) - { - return init(maxSeverity, fileName, maxFileSize, maxFiles); - } - - ////////////////////////////////////////////////////////////////////////// - // CHAR variants for Windows - -#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) - template - inline Logger& init(Severity maxSeverity, const char* fileName, size_t maxFileSize = 0, int maxFiles = 0) - { - return init(maxSeverity, util::toWide(fileName).c_str(), maxFileSize, maxFiles); - } - - template - inline Logger& init(Severity maxSeverity, const char* fileName, size_t maxFileSize = 0, int maxFiles = 0) - { - return init(maxSeverity, fileName, maxFileSize, maxFiles); - } - - template - inline Logger& init(Severity maxSeverity, const char* fileName, size_t maxFileSize = 0, int maxFiles = 0) - { - return init(maxSeverity, util::toWide(fileName).c_str(), maxFileSize, maxFiles); - } - - inline Logger& init(Severity maxSeverity, const char* fileName, size_t maxFileSize = 0, int maxFiles = 0) - { - return init(maxSeverity, fileName, maxFileSize, maxFiles); - } -#endif } diff --git a/include/plog/Util.h b/include/plog/Util.h index 4f9da00..39b186f 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -1,4 +1,5 @@ #pragma once + #include #include #include @@ -95,10 +96,8 @@ # define PLOG_OVERRIDE #endif -namespace plog -{ - namespace util - { +namespace plog { + namespace util { #if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) typedef std::wstring nstring; typedef std::wostringstream nostringstream; @@ -111,8 +110,7 @@ namespace plog typedef char nchar; #endif - inline void localtime_s(struct tm* t, const time_t* time) - { + inline void localtime_s(struct tm *t, const time_t *time) { #if defined(_WIN32) && defined(__BORLANDC__) ::localtime_s(time, t); #elif defined(_WIN32) && defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) @@ -124,8 +122,7 @@ namespace plog #endif } - inline void gmtime_s(struct tm* t, const time_t* time) - { + inline void gmtime_s(struct tm *t, const time_t *time) { #if defined(_WIN32) && defined(__BORLANDC__) ::gmtime_s(time, t); #elif defined(_WIN32) && defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) @@ -145,24 +142,22 @@ namespace plog ::ftime(t); } #else - struct Time - { + struct Time { time_t time; unsigned short millitm; }; - inline void ftime(Time* t) - { + inline void ftime(Time *t) { timeval tv; ::gettimeofday(&tv, NULL); t->time = tv.tv_sec; t->millitm = static_cast(tv.tv_usec / 1000); } + #endif - inline unsigned int gettid() - { + inline unsigned int gettid() { #ifdef _WIN32 return GetCurrentThreadId(); #elif defined(__linux__) @@ -183,79 +178,79 @@ namespace plog } #ifdef _WIN32 - inline int vasprintf(char** strp, const char* format, va_list ap) - { + inline int vasprintf(char** strp, const char* format, va_list ap) + { #if defined(__BORLANDC__) - int charCount = 0x1000; // there is no _vscprintf on Borland/Embarcadero + int charCount = 0x1000; // there is no _vscprintf on Borland/Embarcadero #else - int charCount = _vscprintf(format, ap); - if (charCount < 0) - { - return -1; - } + int charCount = _vscprintf(format, ap); + if (charCount < 0) + { + return -1; + } #endif - size_t bufferCharCount = static_cast(charCount) + 1; + size_t bufferCharCount = static_cast(charCount) + 1; - char* str = static_cast(malloc(bufferCharCount)); - if (!str) - { - return -1; - } + char* str = static_cast(malloc(bufferCharCount)); + if (!str) + { + return -1; + } #if defined(__BORLANDC__) - int retval = vsnprintf_s(str, bufferCharCount, format, ap); + int retval = vsnprintf_s(str, bufferCharCount, format, ap); #elif defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) - int retval = _vsnprintf(str, bufferCharCount, format, ap); + int retval = _vsnprintf(str, bufferCharCount, format, ap); #else - int retval = _vsnprintf_s(str, bufferCharCount, charCount, format, ap); + int retval = _vsnprintf_s(str, bufferCharCount, charCount, format, ap); #endif - if (retval < 0) - { - free(str); - return -1; - } + if (retval < 0) + { + free(str); + return -1; + } - *strp = str; - return retval; - } + *strp = str; + return retval; + } - inline int vaswprintf(wchar_t** strp, const wchar_t* format, va_list ap) - { + inline int vaswprintf(wchar_t** strp, const wchar_t* format, va_list ap) + { #if defined(__BORLANDC__) - int charCount = 0x1000; // there is no _vscwprintf on Borland/Embarcadero + int charCount = 0x1000; // there is no _vscwprintf on Borland/Embarcadero #else - int charCount = _vscwprintf(format, ap); - if (charCount < 0) - { - return -1; - } + int charCount = _vscwprintf(format, ap); + if (charCount < 0) + { + return -1; + } #endif - size_t bufferCharCount = static_cast(charCount) + 1; + size_t bufferCharCount = static_cast(charCount) + 1; - wchar_t* str = static_cast(malloc(bufferCharCount * sizeof(wchar_t))); - if (!str) - { - return -1; - } + wchar_t* str = static_cast(malloc(bufferCharCount * sizeof(wchar_t))); + if (!str) + { + return -1; + } #if defined(__BORLANDC__) - int retval = vsnwprintf_s(str, bufferCharCount, format, ap); + int retval = vsnwprintf_s(str, bufferCharCount, format, ap); #elif defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) - int retval = _vsnwprintf(str, bufferCharCount, format, ap); + int retval = _vsnwprintf(str, bufferCharCount, format, ap); #else - int retval = _vsnwprintf_s(str, bufferCharCount, charCount, format, ap); + int retval = _vsnwprintf_s(str, bufferCharCount, charCount, format, ap); #endif - if (retval < 0) - { - free(str); - return -1; - } + if (retval < 0) + { + free(str); + return -1; + } - *strp = str; - return retval; - } + *strp = str; + return retval; + } #endif #if PLOG_ENABLE_WCHAR_INPUT && !defined(_WIN32) @@ -397,48 +392,39 @@ namespace plog #endif } - inline void splitFileName(const nchar* fileName, nstring& fileNameNoExt, nstring& fileExt) - { - const nchar* dot = findExtensionDot(fileName); + inline void splitFileName(const nchar *fileName, nstring &fileNameNoExt, nstring &fileExt) { + const nchar *dot = findExtensionDot(fileName); - if (dot) - { + if (dot) { fileNameNoExt.assign(fileName, dot); fileExt.assign(dot + 1); - } - else - { + } else { fileNameNoExt.assign(fileName); fileExt.clear(); } } - class PLOG_LINKAGE NonCopyable - { + class PLOG_LINKAGE NonCopyable { protected: - NonCopyable() - { + NonCopyable() { } private: - NonCopyable(const NonCopyable&); - NonCopyable& operator=(const NonCopyable&); + NonCopyable(const NonCopyable &); + + NonCopyable &operator=(const NonCopyable &); }; - class File : NonCopyable - { + class File : NonCopyable { public: - File() : m_file(-1) - { + File() : m_file(-1) { } - File(const nchar* fileName) : m_file(-1) - { + File(const nchar *fileName) : m_file(-1) { open(fileName); } - ~File() - { + ~File() { close(); } @@ -552,8 +538,7 @@ namespace plog #endif } - void unlock() - { + void unlock() { #ifdef _WIN32 LeaveCriticalSection(&m_sync); #elif defined(__rtems__) From 5fbc4b8556e5d1d4639cb17ef46c990488c5cc4c Mon Sep 17 00:00:00 2001 From: caesar Date: Mon, 19 Dec 2022 15:17:09 +0800 Subject: [PATCH 23/29] add #include --- include/plog/Util.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/plog/Util.h b/include/plog/Util.h index 39b186f..1f42646 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include From d8a48f08a0377b82e8d810588026431dbeebfa21 Mon Sep 17 00:00:00 2001 From: caesar Date: Mon, 19 Dec 2022 15:20:05 +0800 Subject: [PATCH 24/29] fix PLOG_LINKAGE_HIDDEN --- include/plog/Util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/plog/Util.h b/include/plog/Util.h index 1f42646..2fa79a0 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -416,7 +416,7 @@ namespace plog { NonCopyable &operator=(const NonCopyable &); }; - class File : NonCopyable { + class PLOG_LINKAGE_HIDDEN File : NonCopyable { public: File() : m_file(-1) { } From 58c21cb39d79b2e2f023f95ea7c86b06b6d40505 Mon Sep 17 00:00:00 2001 From: caesar Date: Mon, 19 Dec 2022 15:31:43 +0800 Subject: [PATCH 25/29] try fix windows wstat --- include/plog/Util.h | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/include/plog/Util.h b/include/plog/Util.h index 2fa79a0..53663a2 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -97,6 +97,9 @@ # define PLOG_OVERRIDE #endif +#ifndef CP_UTF8 +#define CP_UTF8 65001 +#endif namespace plog { namespace util { #if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) @@ -348,8 +351,7 @@ namespace plog { #if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) inline static bool exists(const nchar *path_string) { - struct stat buffer; - return (wstat(path_string, &buffer) == 0); + return (access(path_string, F_OK ) != -1 ); } /** * "%Y-%m-%d-%H-%M-%S" From af8c279d0cea8e21b188a0bc8ae8b3f4f3944e43 Mon Sep 17 00:00:00 2001 From: caesar Date: Mon, 19 Dec 2022 15:38:40 +0800 Subject: [PATCH 26/29] try fix windows wstat 2 --- include/plog/Util.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/include/plog/Util.h b/include/plog/Util.h index 53663a2..0149149 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -10,7 +10,7 @@ #include #ifndef PLOG_ENABLE_WCHAR_INPUT -# if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) +# if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) # define PLOG_ENABLE_WCHAR_INPUT 1 # else # define PLOG_ENABLE_WCHAR_INPUT 0 @@ -102,7 +102,7 @@ #endif namespace plog { namespace util { -#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) typedef std::wstring nstring; typedef std::wostringstream nostringstream; typedef std::wistringstream nistringstream; @@ -281,7 +281,7 @@ namespace plog { } #endif -#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) inline std::wstring toWide(const char* str) { size_t len = ::strlen(str); @@ -349,9 +349,9 @@ namespace plog { return str_time.str(); } -#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) inline static bool exists(const nchar *path_string) { - return (access(path_string, F_OK ) != -1 ); + return (access(path_string, 0 ) != -1 ); } /** * "%Y-%m-%d-%H-%M-%S" @@ -388,7 +388,7 @@ namespace plog { } inline const nchar *findExtensionDot(const nchar *fileName) { -#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) return std::wcsrchr(fileName, L'.'); #else return std::strrchr(fileName, '.'); @@ -432,9 +432,9 @@ namespace plog { } size_t open(const nchar *fileName) { -#if !defined(PLOG_DISABLE_WCHAR_T) and (defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__))) +#if !defined(PLOG_DISABLE_WCHAR_T) && (defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__))) m_file = ::_wsopen(fileName, _O_CREAT | _O_WRONLY | _O_BINARY | _O_NOINHERIT, SH_DENYWR, _S_IREAD | _S_IWRITE); -#elif !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) +#elif !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) ::_wsopen_s(&m_file, fileName, _O_CREAT | _O_WRONLY | _O_BINARY | _O_NOINHERIT, _SH_DENYWR, _S_IREAD | _S_IWRITE); #elif defined(O_CLOEXEC) m_file = ::open(fileName, O_CREAT | O_APPEND | O_WRONLY | O_CLOEXEC, @@ -484,7 +484,7 @@ namespace plog { } static int unlink(const nchar *fileName) { -#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) return ::_wunlink(fileName); #else return ::unlink(fileName); @@ -492,7 +492,7 @@ namespace plog { } static int rename(const nchar *oldFilename, const nchar *newFilename) { -#if !defined(PLOG_DISABLE_WCHAR_T) and defined(_WIN32) +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) return MoveFileW(oldFilename, newFilename); #else return ::rename(oldFilename, newFilename); From fea082335105d816624548283ffb8f3d02d10489 Mon Sep 17 00:00:00 2001 From: caesar Date: Mon, 19 Dec 2022 15:43:26 +0800 Subject: [PATCH 27/29] try fix windows wstat 3 --- include/plog/Util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/plog/Util.h b/include/plog/Util.h index 0149149..893e9dd 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -351,7 +351,7 @@ namespace plog { #if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) inline static bool exists(const nchar *path_string) { - return (access(path_string, 0 ) != -1 ); + return exists(util::toNarrow(name,CP_UTF8).c_str()); } /** * "%Y-%m-%d-%H-%M-%S" From fd28e812d5aaa8ad662b36d9d885acbc6c4088d7 Mon Sep 17 00:00:00 2001 From: caesar Date: Mon, 19 Dec 2022 15:47:13 +0800 Subject: [PATCH 28/29] try fix windows wstat 4 --- include/plog/Util.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/plog/Util.h b/include/plog/Util.h index 893e9dd..96534ff 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -351,7 +351,7 @@ namespace plog { #if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) inline static bool exists(const nchar *path_string) { - return exists(util::toNarrow(name,CP_UTF8).c_str()); + return exists(util::toNarrow(path_string,CP_UTF8).c_str()); } /** * "%Y-%m-%d-%H-%M-%S" From c161a8fa2776b267d2d19d44679a732add3a8c19 Mon Sep 17 00:00:00 2001 From: caesar Date: Mon, 19 Dec 2022 15:55:51 +0800 Subject: [PATCH 29/29] try fix windows ci --- include/plog/Appenders/RollingDateFileAppender.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/plog/Appenders/RollingDateFileAppender.h b/include/plog/Appenders/RollingDateFileAppender.h index 2892f43..6850b32 100644 --- a/include/plog/Appenders/RollingDateFileAppender.h +++ b/include/plog/Appenders/RollingDateFileAppender.h @@ -75,7 +75,11 @@ namespace plog { if (record.getTime().time >= last_date) { std::string path = util::get_file_name(m_fileName.c_str()); RollingFileAppender::setFileName(path.c_str()); +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) + if (path != util::toNarrow(m_fileName.c_str(),CP_UTF8) && m_fileSize > 0) { +#else if (path != m_fileName && m_fileSize > 0) { +#endif int size = (int) m_fileSize; std::string lastFileName = util::get_file_name(m_fileName.c_str(), -size); #if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32)