diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d5ffcfd..7af94cbd 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 23f4bc67..0fa32273 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 64ba1734..056babc1 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 30a6c7ff..8664b8f5 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 new file mode 100644 index 00000000..6850b32b --- /dev/null +++ b/include/plog/Appenders/RollingDateFileAppender.h @@ -0,0 +1,102 @@ +// +// Created by caesar kekxv on 2022/2/26. +// + +#ifndef TERMINALSERVICE_ROLLINGDATEFILEAPPENDER_H +#define TERMINALSERVICE_ROLLINGDATEFILEAPPENDER_H + +#include + +#ifdef _WIN32 +#include +#endif +namespace plog { + + template > + class PLOG_LINKAGE_HIDDEN RollingDateFileAppender : public RollingFileAppender { + private: + time_t last_date; + size_t m_fileSize; + util::nstring m_fileName; + + public: +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_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 + + 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) + { + setFileName(util::toWide(fileName).c_str()); + } +#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 setMaxFiles(size_t maxFiles) { + m_fileSize = maxFiles; + } + + inline void write(const Record &record) PLOG_OVERRIDE { + 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) + 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(); + } + 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 1ed73de9..4f50a4ac 100644 --- a/include/plog/Appenders/RollingFileAppender.h +++ b/include/plog/Appenders/RollingFileAppender.h @@ -12,16 +12,16 @@ namespace plog { 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); } -#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() @@ -65,7 +65,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 b0c199aa..37bbdbf5 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)); @@ -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 86c97432..72a1f3e9 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 615c41d6..daa7f217 100644 --- a/include/plog/Init.h +++ b/include/plog/Init.h @@ -1,17 +1,15 @@ #pragma once + #include -namespace plog -{ +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); } } diff --git a/include/plog/Record.h b/include/plog/Record.h index c0cb0947..dc5797dc 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 0987c694..96534ffa 100644 --- a/include/plog/Util.h +++ b/include/plog/Util.h @@ -1,14 +1,16 @@ #pragma once + #include #include #include +#include #include #include #include #include #ifndef PLOG_ENABLE_WCHAR_INPUT -# ifdef _WIN32 +# if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) # define PLOG_ENABLE_WCHAR_INPUT 1 # else # define PLOG_ENABLE_WCHAR_INPUT 0 @@ -50,23 +52,33 @@ # 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 @@ -85,11 +97,12 @@ # define PLOG_OVERRIDE #endif -namespace plog -{ - namespace util - { -#ifdef _WIN32 +#ifndef CP_UTF8 +#define CP_UTF8 65001 +#endif +namespace plog { + namespace util { +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) typedef std::wstring nstring; typedef std::wostringstream nostringstream; typedef std::wistringstream nistringstream; @@ -101,8 +114,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) @@ -114,8 +126,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) @@ -135,24 +146,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__) @@ -173,79 +182,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) @@ -272,7 +281,7 @@ namespace plog } #endif -#ifdef _WIN32 +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) inline std::wstring toWide(const char* str) { size_t len = ::strlen(str); @@ -301,23 +310,74 @@ namespace plog } #endif - inline std::string processFuncName(const char* func) - { + 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) && defined(_WIN32) + inline static bool exists(const nchar *path_string) { + return exists(util::toNarrow(path_string,CP_UTF8).c_str()); + } + /** + * "%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__) 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; } @@ -327,108 +387,93 @@ namespace plog #endif } - inline const nchar* findExtensionDot(const nchar* fileName) - { -#ifdef _WIN32 + inline const nchar *findExtensionDot(const nchar *fileName) { +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) return std::wcsrchr(fileName, L'.'); #else return std::strrchr(fileName, '.'); #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) - { -#if defined(_WIN32) && (defined(__BORLANDC__) || defined(__MINGW32__)) + size_t open(const nchar *fileName) { +#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(_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, 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)) + ::_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 @@ -438,18 +483,16 @@ namespace plog } } - static int unlink(const nchar* fileName) - { -#ifdef _WIN32 + static int unlink(const nchar *fileName) { +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) return ::_wunlink(fileName); #else return ::unlink(fileName); #endif } - static int rename(const nchar* oldFilename, const nchar* newFilename) - { -#ifdef _WIN32 + static int rename(const nchar *oldFilename, const nchar *newFilename) { +#if !defined(PLOG_DISABLE_WCHAR_T) && defined(_WIN32) return MoveFileW(oldFilename, newFilename); #else return ::rename(oldFilename, newFilename); @@ -460,11 +503,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__) @@ -477,8 +518,7 @@ namespace plog #endif } - ~Mutex() - { + ~Mutex() { #ifdef _WIN32 DeleteCriticalSection(&m_sync); #elif defined(__rtems__) @@ -491,8 +531,7 @@ namespace plog friend class MutexLock; private: - void lock() - { + void lock() { #ifdef _WIN32 EnterCriticalSection(&m_sync); #elif defined(__rtems__) @@ -502,8 +541,7 @@ namespace plog #endif } - void unlock() - { + void unlock() { #ifdef _WIN32 LeaveCriticalSection(&m_sync); #elif defined(__rtems__) @@ -523,21 +561,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 @@ -549,32 +584,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/include/plog/WinApi.h b/include/plog/WinApi.h index ce579986..9eb20ffb 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/CMakeLists.txt b/samples/CMakeLists.txt index 1b509dc9..bc27dded 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -72,3 +72,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 00000000..db83d3c6 --- /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 00000000..6352b591 --- /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() { + 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. + 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 + PLOG(plog::debug) << "Hello log! chinese test 中文测试"; // function-style macro + } + return 0; +}