From effe302faa051f4162c0c7ecd44dd5838eca3001 Mon Sep 17 00:00:00 2001 From: Chunting Gu Date: Wed, 7 Aug 2019 13:45:56 +0800 Subject: [PATCH] Use bfs::path for logger path. --- examples/client_basics.cc | Bin 1565 -> 3252 bytes webcc/logger.cc | 42 ++++++++++++++++++++------------------ webcc/logger.h | 4 +++- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/examples/client_basics.cc b/examples/client_basics.cc index 27b3f1b338e8cdf34e0675315ab7120bd17303d3..abf280d921350ccbc0db4664c9379799cf0542ef 100644 GIT binary patch literal 3252 zcmd^>U2hUW6o${WiT`1xiIk*5?Ulh|0!V7LsZ}&y5(-<3rC7IHwITd<^?7HOvP*!# z#u%fS49x7D@9&>KwrpS{>)Ft5tZ$(Ow$H6=*R0SYi|v`+w|lk-ZeX8*ih&BjxJ)1y zLl^8DP^ZxDS;g@ia36u)ah$|Y^hQ>;b1N+2nWZuW{v%6r&alK^I>qR6iCj8wTZ!A) zQ4KhKu$DEf$?DjVy|x4X5AB%!4%h-~2dJ6ERjb~?HGx02Hor$uDcMs;Ra`|mi_^re zQ$5?rIny$j(|Q$^w+{2lSw~(uaW)UoI*TPw+#>^!qNa=rIF%(BP} z$k{xc^Tmm{{d*qI*2F(kF;B=mU!T{lnd%e5$X{(5Q0ON$b8JO2U)??ARAbk9)4k}b zMv=2Ru-^7;7r*D{`x3cTXMB}=j}B+{#NGa%s-rpUjGRwTC@toVI`fL=jSiDXo0BY( zw#xcd>U!;-o1HHH{))Xt9FB-iZ^6J#7Modeg5`bh>oJoA#9q%ur1EKgwrkN(B3>hV z$eZROf6GpqvYN2_SiK5052q=6KZ7;>GK2NH43ime+S4K^s5?ez8ZftbAL(_q>}k5+ qogC_VzQcR@Hw9BK{W=+_7)~ literal 1565 zcmcgs+iu!G5PkPoOpR0)iQkQsWLQ#{!dz4(7 zu!l-vOqov^$APaT@!XtmCJOD=$*M)G7z0274x!%+3`t51kFww2k7tpKT=#zdk@d3% zmg=uq-;*RcLEOZ&ee!oal)B!wD|6ey^Oka5+~nh)L3+}*rWcUDM8*FM!)s<_B@SbL zsDsHU@)(D^W+*q<>^Fy=+%((Jlq64~!8W;1{}kD_$BZ4gaoO7n9j=G@UFL zi)pqOer5FmK&FbF`8qg2^1mRsfuYBlf#XzWmjcsEtwAW>NwjClMC(Yu2F#9P5ds+U zTvxFOdcEQ$`^lvoA}GkDkrExkd3Jl|y3qWQQJd+^&Z`}F9@mnXvVZWMb+!z4c44K~ EPnhkjX8-^I diff --git a/webcc/logger.cc b/webcc/logger.cc index 2a69c6e..4509500 100644 --- a/webcc/logger.cc +++ b/webcc/logger.cc @@ -22,6 +22,8 @@ #include "boost/filesystem.hpp" +namespace bfs = boost::filesystem; + namespace webcc { // ----------------------------------------------------------------------------- @@ -34,21 +36,24 @@ static const char* kLevelNames[] = { // ----------------------------------------------------------------------------- +static FILE* FOpen(const bfs::path& path, bool overwrite) { +#if (defined(_WIN32) || defined(_WIN64)) + return _wfopen(path.wstring().c_str(), overwrite ? L"w+" : L"a+"); +#else + return fopen(path.string().c_str(), overwrite ? "w+" : "a+"); +#endif // defined(_WIN32) || defined(_WIN64) +} + struct Logger { Logger() : file(nullptr), modes(0) { } - void Init(const std::string& path, int _modes) { + void Init(const bfs::path& path, int _modes) { modes = _modes; // Create log file only if necessary. if ((modes & LOG_FILE) != 0 && !path.empty()) { - if ((modes & LOG_OVERWRITE) != 0) { - file = fopen(path.c_str(), "w+"); - } else { - // Append to existing file. - file = fopen(path.c_str(), "a+"); - } + file = FOpen(path, (modes & LOG_OVERWRITE) != 0); } } @@ -153,33 +158,30 @@ static std::string GetThreadID() { return thread_id; } -static bfs::path InitLogPath(const std::string& dir) { +static bfs::path InitLogPath(const bfs::path& dir) { if (dir.empty()) { return bfs::current_path() / WEBCC_LOG_FILE_NAME; } - bfs::path path = bfs::path(dir); - if (!bfs::exists(path) || !bfs::is_directory(path)) { + if (!bfs::exists(dir) || !bfs::is_directory(dir)) { boost::system::error_code ec; - if (!bfs::create_directories(path, ec) || ec) { + if (!bfs::create_directories(dir, ec) || ec) { return bfs::path(); } } - path /= WEBCC_LOG_FILE_NAME; - return path; + return (dir / WEBCC_LOG_FILE_NAME); } -void LogInit(const std::string& dir, int modes) { +void LogInit(const bfs::path& dir, int modes) { + // Suppose this is called from the main thread. + g_main_thread_id = DoGetThreadID(); + if ((modes & LOG_FILE) != 0) { - bfs::path path = InitLogPath(dir); - g_logger.Init(path.string(), modes); + g_logger.Init(InitLogPath(dir), modes); } else { - g_logger.Init("", modes); + g_logger.Init({}, modes); } - - // Suppose LogInit() is called from the main thread. - g_main_thread_id = DoGetThreadID(); } static std::string GetTimestamp() { diff --git a/webcc/logger.h b/webcc/logger.h index 48cbfc4..89b7cfd 100644 --- a/webcc/logger.h +++ b/webcc/logger.h @@ -9,6 +9,8 @@ #include // for strrchr() #include +#include "boost/filesystem/path.hpp" + // Log levels. // VERB is similar to DEBUG commonly used by other projects. // USER is for the users who want to log their own logs but don't want any @@ -42,7 +44,7 @@ const int LOG_FILE_OVERWRITE = LOG_FILE | LOG_OVERWRITE; // Initialize logger. // If |dir| is empty, log file will be generated in current directory. -void LogInit(const std::string& dir, int modes); +void LogInit(const boost::filesystem::path& dir, int modes); void Log(int level, const char* file, int line, const char* format, ...);