Use bfs::path for logger path.
This commit is contained in:
Binary file not shown.
+24
-22
@@ -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) {
|
||||||
if ((modes & LOG_FILE) != 0) {
|
// Suppose this is called from the main thread.
|
||||||
bfs::path path = InitLogPath(dir);
|
|
||||||
g_logger.Init(path.string(), modes);
|
|
||||||
} else {
|
|
||||||
g_logger.Init("", modes);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Suppose LogInit() is called from the main thread.
|
|
||||||
g_main_thread_id = DoGetThreadID();
|
g_main_thread_id = DoGetThreadID();
|
||||||
|
|
||||||
|
if ((modes & LOG_FILE) != 0) {
|
||||||
|
g_logger.Init(InitLogPath(dir), modes);
|
||||||
|
} else {
|
||||||
|
g_logger.Init({}, modes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static std::string GetTimestamp() {
|
static std::string GetTimestamp() {
|
||||||
|
|||||||
+3
-1
@@ -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, ...);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user