Use bfs::path for logger path.

master
Chunting Gu 6 years ago
parent b16ba928d4
commit effe302faa

Binary file not shown.

@ -22,6 +22,8 @@
#include "boost/filesystem.hpp" #include "boost/filesystem.hpp"
namespace bfs = boost::filesystem;
namespace webcc { 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 { struct Logger {
Logger() : file(nullptr), modes(0) { Logger() : file(nullptr), modes(0) {
} }
void Init(const std::string& path, int _modes) { void Init(const bfs::path& path, int _modes) {
modes = _modes; modes = _modes;
// Create log file only if necessary. // Create log file only if necessary.
if ((modes & LOG_FILE) != 0 && !path.empty()) { if ((modes & LOG_FILE) != 0 && !path.empty()) {
if ((modes & LOG_OVERWRITE) != 0) { file = FOpen(path, (modes & LOG_OVERWRITE) != 0);
file = fopen(path.c_str(), "w+");
} else {
// Append to existing file.
file = fopen(path.c_str(), "a+");
}
} }
} }
@ -153,33 +158,30 @@ static std::string GetThreadID() {
return thread_id; return thread_id;
} }
static bfs::path InitLogPath(const std::string& dir) { static bfs::path InitLogPath(const bfs::path& dir) {
if (dir.empty()) { if (dir.empty()) {
return bfs::current_path() / WEBCC_LOG_FILE_NAME; return bfs::current_path() / WEBCC_LOG_FILE_NAME;
} }
bfs::path path = bfs::path(dir); if (!bfs::exists(dir) || !bfs::is_directory(dir)) {
if (!bfs::exists(path) || !bfs::is_directory(path)) {
boost::system::error_code ec; boost::system::error_code ec;
if (!bfs::create_directories(path, ec) || ec) { if (!bfs::create_directories(dir, ec) || ec) {
return bfs::path(); return bfs::path();
} }
} }
path /= WEBCC_LOG_FILE_NAME; return (dir / WEBCC_LOG_FILE_NAME);
return path;
} }
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) { if ((modes & LOG_FILE) != 0) {
bfs::path path = InitLogPath(dir); g_logger.Init(InitLogPath(dir), modes);
g_logger.Init(path.string(), modes);
} else { } 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() { static std::string GetTimestamp() {

@ -9,6 +9,8 @@
#include <cstring> // for strrchr() #include <cstring> // for strrchr()
#include <string> #include <string>
#include "boost/filesystem/path.hpp"
// Log levels. // Log levels.
// VERB is similar to DEBUG commonly used by other projects. // 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 // 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. // Initialize logger.
// If |dir| is empty, log file will be generated in current directory. // 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, ...); void Log(int level, const char* file, int line, const char* format, ...);

Loading…
Cancel
Save