Skip to content

Commit

Permalink
feature: Support unicode in paths #995
Browse files Browse the repository at this point in the history
  • Loading branch information
xfc1939 committed Dec 21, 2023
1 parent 931323d commit 0932ea5
Show file tree
Hide file tree
Showing 9 changed files with 893 additions and 84 deletions.
39 changes: 39 additions & 0 deletions src/base/commandlineflags.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@

#include "glog/logging.h"

#ifdef GLOG_OS_WINDOWS
#include "Windows.h"
#endif // GLOG_OS_WINDOWS


#define DECLARE_VARIABLE(type, shorttype, name, tn) \
namespace fL##shorttype { \
extern GLOG_EXPORT type FLAGS_##name; \
Expand Down Expand Up @@ -107,6 +112,21 @@
char FLAGS_no##name; \
} \
using fLS::FLAGS_##name

#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
#define DECLARE_wstring(name) \
namespace fLS { \
extern GLOG_EXPORT std::wstring& FLAGS_##name; \
} \
using fLS::FLAGS_##name
#define DEFINE_wstring(name, value, meaning) \
namespace fLS { \
std::wstring FLAGS_##name##_buf(value); \
GLOG_EXPORT std::wstring& FLAGS_##name = FLAGS_##name##_buf; \
wchar_t FLAGS_no##name; \
} \
using fLS::FLAGS_##name
#endif //

#endif // HAVE_LIB_GFLAGS

Expand Down Expand Up @@ -135,6 +155,25 @@
#define EnvToString(envname, dflt) \
(!getenv(envname) ? (dflt) : getenv(envname))

#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
#define GLOG_DEFINE_wstring(name, value, meaning) \
DEFINE_wstring(name, EnvToWString(TEXT("GLOG_") #name, value), meaning)

static std::wstring getenvw(wchar_t *lpName) {
auto size = GetEnvironmentVariableW(lpName, NULL, 0);
if (size == 0) {
return {};
}
std::wstring value;
value.resize(size);
GetEnvironmentVariableW(lpName, &value[0], size);
return value.substr(0,size-1);
}
#define EnvToWString(envname, dflt) \
(getenvw(envname).empty() ? dflt : getenvw(envname))
#endif


#define EnvToBool(envname, dflt) \
(!getenv(envname) ? (dflt) \
: memchr("tTyY1\0", getenv(envname)[0], 6) != nullptr)
Expand Down
5 changes: 4 additions & 1 deletion src/cleanup_with_absolute_prefix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ using namespace GOOGLE_NAMESPACE;
TEST(CleanImmediatelyWithAbsolutePrefix, logging) {
google::EnableLogCleaner(0);
google::SetLogFilenameExtension(".barfoo");
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
google::SetLogDestination(GLOG_INFO, TEXT("test_cleanup_"));
#else
google::SetLogDestination(GLOG_INFO, "test_cleanup_");

#endif
for (unsigned i = 0; i < 1000; ++i) {
LOG(INFO) << "cleanup test";
}
Expand Down
5 changes: 5 additions & 0 deletions src/cleanup_with_relative_prefix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ using namespace GOOGLE_NAMESPACE;
TEST(CleanImmediatelyWithRelativePrefix, logging) {
google::EnableLogCleaner(0);
google::SetLogFilenameExtension(".relativefoo");
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
google::SetLogDestination(GLOG_INFO, TEXT("test_subdir/test_cleanup_"));
#else
google::SetLogDestination(GLOG_INFO, "test_subdir/test_cleanup_");
#endif // (GLOG_OS_WINDOWS) && defined(UNICODE)


for (unsigned i = 0; i < 1000; ++i) {
LOG(INFO) << "cleanup test";
Expand Down
44 changes: 42 additions & 2 deletions src/glog/logging.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,9 @@ typedef void(*CustomPrefixCallback)(std::ostream& s, const LogMessageInfo& l, vo
#pragma push_macro("DECLARE_VARIABLE")
#pragma push_macro("DECLARE_bool")
#pragma push_macro("DECLARE_string")
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
#pragma push_macro("DECLARE_wstring")
#endif
#pragma push_macro("DECLARE_int32")
#pragma push_macro("DECLARE_uint32")

Expand All @@ -366,6 +369,12 @@ typedef void(*CustomPrefixCallback)(std::ostream& s, const LogMessageInfo& l, vo
#undef DECLARE_string
#endif

#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
#ifdef DECLARE_wstring
#undef DECLARE_wstring
#endif
#endif

#ifdef DECLARE_int32
#undef DECLARE_int32
#endif
Expand Down Expand Up @@ -402,8 +411,19 @@ typedef void(*CustomPrefixCallback)(std::ostream& s, const LogMessageInfo& l, vo
extern GLOG_EXPORT std::string& FLAGS_##name; \
} \
using fLS::FLAGS_##name

#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
#define DECLARE_wstring(name) \
namespace fLS { \
extern GLOG_EXPORT std::wstring& FLAGS_##name; \
} \
using fLS::FLAGS_##name
#endif

#endif



// Set whether appending a timestamp to the log file name
DECLARE_bool(timestamp_in_logfile_name);

Expand Down Expand Up @@ -448,7 +468,12 @@ DECLARE_int32(minloglevel);

// If specified, logfiles are written into this directory instead of the
// default logging directory.

#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
DECLARE_wstring(log_dir);
#else
DECLARE_string(log_dir);
#endif

// Set the log file mode.
DECLARE_int32(logfile_mode);
Expand Down Expand Up @@ -1574,9 +1599,13 @@ GLOG_EXPORT void FlushLogFilesUnsafe(LogSeverity min_severity);
// messages is sent. If base_filename is "", it means "don't log this
// severity". Thread-safe.
//
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
GLOG_EXPORT void SetLogDestination(LogSeverity severity,
const wchar_t* base_filename);
#else
GLOG_EXPORT void SetLogDestination(LogSeverity severity,
const char* base_filename);

#endif
//
// Set the basename of the symlink to the latest log file at a given
// severity. If symlink_basename is empty, do not make a symlink. If
Expand Down Expand Up @@ -1668,8 +1697,11 @@ GLOG_EXPORT void SetEmailLogging(LogSeverity min_severity,
// list of addresses. Thread-safe.
GLOG_EXPORT bool SendEmail(const char* dest, const char* subject,
const char* body);

#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
GLOG_EXPORT const std::vector<std::wstring>& GetLoggingDirectories();
#else
GLOG_EXPORT const std::vector<std::string>& GetLoggingDirectories();
#endif

// For tests only: Clear the internal [cached] list of logging directories to
// force a refresh the next time GetLoggingDirectories is called.
Expand All @@ -1679,8 +1711,13 @@ void TestOnly_ClearLoggingDirectoriesList();
// Returns a set of existing temporary directories, which will be a
// subset of the directories returned by GetLoggingDirectories().
// Thread-safe.
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
GLOG_EXPORT void GetExistingTempDirectories(
std::vector<std::wstring>* list);
#else
GLOG_EXPORT void GetExistingTempDirectories(
std::vector<std::string>* list);
#endif

// Print any fatal message again -- useful to call from signal handler
// so that the last thing in the output is the fatal message.
Expand Down Expand Up @@ -1839,6 +1876,9 @@ GLOG_EXPORT void InstallFailureWriter(
#pragma pop_macro("DECLARE_VARIABLE")
#pragma pop_macro("DECLARE_bool")
#pragma pop_macro("DECLARE_string")
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
#pragma pop_macro("DECLARE_wstring")
#endif
#pragma pop_macro("DECLARE_int32")
#pragma pop_macro("DECLARE_uint32")

Expand Down
Loading

0 comments on commit 0932ea5

Please sign in to comment.