Skip to content
This repository was archived by the owner on Jun 30, 2025. It is now read-only.

Commit 0932ea5

Browse files
author
xfc1939
committed
feature: Support unicode in paths #995
1 parent 931323d commit 0932ea5

File tree

9 files changed

+893
-84
lines changed

9 files changed

+893
-84
lines changed

src/base/commandlineflags.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161

6262
#include "glog/logging.h"
6363

64+
#ifdef GLOG_OS_WINDOWS
65+
#include "Windows.h"
66+
#endif // GLOG_OS_WINDOWS
67+
68+
6469
#define DECLARE_VARIABLE(type, shorttype, name, tn) \
6570
namespace fL##shorttype { \
6671
extern GLOG_EXPORT type FLAGS_##name; \
@@ -107,6 +112,21 @@
107112
char FLAGS_no##name; \
108113
} \
109114
using fLS::FLAGS_##name
115+
116+
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
117+
#define DECLARE_wstring(name) \
118+
namespace fLS { \
119+
extern GLOG_EXPORT std::wstring& FLAGS_##name; \
120+
} \
121+
using fLS::FLAGS_##name
122+
#define DEFINE_wstring(name, value, meaning) \
123+
namespace fLS { \
124+
std::wstring FLAGS_##name##_buf(value); \
125+
GLOG_EXPORT std::wstring& FLAGS_##name = FLAGS_##name##_buf; \
126+
wchar_t FLAGS_no##name; \
127+
} \
128+
using fLS::FLAGS_##name
129+
#endif //
110130

111131
#endif // HAVE_LIB_GFLAGS
112132

@@ -135,6 +155,25 @@
135155
#define EnvToString(envname, dflt) \
136156
(!getenv(envname) ? (dflt) : getenv(envname))
137157

158+
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
159+
#define GLOG_DEFINE_wstring(name, value, meaning) \
160+
DEFINE_wstring(name, EnvToWString(TEXT("GLOG_") #name, value), meaning)
161+
162+
static std::wstring getenvw(wchar_t *lpName) {
163+
auto size = GetEnvironmentVariableW(lpName, NULL, 0);
164+
if (size == 0) {
165+
return {};
166+
}
167+
std::wstring value;
168+
value.resize(size);
169+
GetEnvironmentVariableW(lpName, &value[0], size);
170+
return value.substr(0,size-1);
171+
}
172+
#define EnvToWString(envname, dflt) \
173+
(getenvw(envname).empty() ? dflt : getenvw(envname))
174+
#endif
175+
176+
138177
#define EnvToBool(envname, dflt) \
139178
(!getenv(envname) ? (dflt) \
140179
: memchr("tTyY1\0", getenv(envname)[0], 6) != nullptr)

src/cleanup_with_absolute_prefix_unittest.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ using namespace GOOGLE_NAMESPACE;
5757
TEST(CleanImmediatelyWithAbsolutePrefix, logging) {
5858
google::EnableLogCleaner(0);
5959
google::SetLogFilenameExtension(".barfoo");
60+
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
61+
google::SetLogDestination(GLOG_INFO, TEXT("test_cleanup_"));
62+
#else
6063
google::SetLogDestination(GLOG_INFO, "test_cleanup_");
61-
64+
#endif
6265
for (unsigned i = 0; i < 1000; ++i) {
6366
LOG(INFO) << "cleanup test";
6467
}

src/cleanup_with_relative_prefix_unittest.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,12 @@ using namespace GOOGLE_NAMESPACE;
5757
TEST(CleanImmediatelyWithRelativePrefix, logging) {
5858
google::EnableLogCleaner(0);
5959
google::SetLogFilenameExtension(".relativefoo");
60+
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
61+
google::SetLogDestination(GLOG_INFO, TEXT("test_subdir/test_cleanup_"));
62+
#else
6063
google::SetLogDestination(GLOG_INFO, "test_subdir/test_cleanup_");
64+
#endif // (GLOG_OS_WINDOWS) && defined(UNICODE)
65+
6166

6267
for (unsigned i = 0; i < 1000; ++i) {
6368
LOG(INFO) << "cleanup test";

src/glog/logging.h.in

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,9 @@ typedef void(*CustomPrefixCallback)(std::ostream& s, const LogMessageInfo& l, vo
351351
#pragma push_macro("DECLARE_VARIABLE")
352352
#pragma push_macro("DECLARE_bool")
353353
#pragma push_macro("DECLARE_string")
354+
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
355+
#pragma push_macro("DECLARE_wstring")
356+
#endif
354357
#pragma push_macro("DECLARE_int32")
355358
#pragma push_macro("DECLARE_uint32")
356359

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

372+
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
373+
#ifdef DECLARE_wstring
374+
#undef DECLARE_wstring
375+
#endif
376+
#endif
377+
369378
#ifdef DECLARE_int32
370379
#undef DECLARE_int32
371380
#endif
@@ -402,8 +411,19 @@ typedef void(*CustomPrefixCallback)(std::ostream& s, const LogMessageInfo& l, vo
402411
extern GLOG_EXPORT std::string& FLAGS_##name; \
403412
} \
404413
using fLS::FLAGS_##name
414+
415+
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
416+
#define DECLARE_wstring(name) \
417+
namespace fLS { \
418+
extern GLOG_EXPORT std::wstring& FLAGS_##name; \
419+
} \
420+
using fLS::FLAGS_##name
421+
#endif
422+
405423
#endif
406424

425+
426+
407427
// Set whether appending a timestamp to the log file name
408428
DECLARE_bool(timestamp_in_logfile_name);
409429

@@ -448,7 +468,12 @@ DECLARE_int32(minloglevel);
448468

449469
// If specified, logfiles are written into this directory instead of the
450470
// default logging directory.
471+
472+
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
473+
DECLARE_wstring(log_dir);
474+
#else
451475
DECLARE_string(log_dir);
476+
#endif
452477

453478
// Set the log file mode.
454479
DECLARE_int32(logfile_mode);
@@ -1574,9 +1599,13 @@ GLOG_EXPORT void FlushLogFilesUnsafe(LogSeverity min_severity);
15741599
// messages is sent. If base_filename is "", it means "don't log this
15751600
// severity". Thread-safe.
15761601
//
1602+
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
1603+
GLOG_EXPORT void SetLogDestination(LogSeverity severity,
1604+
const wchar_t* base_filename);
1605+
#else
15771606
GLOG_EXPORT void SetLogDestination(LogSeverity severity,
15781607
const char* base_filename);
1579-
1608+
#endif
15801609
//
15811610
// Set the basename of the symlink to the latest log file at a given
15821611
// severity. If symlink_basename is empty, do not make a symlink. If
@@ -1668,8 +1697,11 @@ GLOG_EXPORT void SetEmailLogging(LogSeverity min_severity,
16681697
// list of addresses. Thread-safe.
16691698
GLOG_EXPORT bool SendEmail(const char* dest, const char* subject,
16701699
const char* body);
1671-
1700+
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
1701+
GLOG_EXPORT const std::vector<std::wstring>& GetLoggingDirectories();
1702+
#else
16721703
GLOG_EXPORT const std::vector<std::string>& GetLoggingDirectories();
1704+
#endif
16731705

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

16851722
// Print any fatal message again -- useful to call from signal handler
16861723
// so that the last thing in the output is the fatal message.
@@ -1839,6 +1876,9 @@ GLOG_EXPORT void InstallFailureWriter(
18391876
#pragma pop_macro("DECLARE_VARIABLE")
18401877
#pragma pop_macro("DECLARE_bool")
18411878
#pragma pop_macro("DECLARE_string")
1879+
#if defined(GLOG_OS_WINDOWS) && defined(UNICODE)
1880+
#pragma pop_macro("DECLARE_wstring")
1881+
#endif
18421882
#pragma pop_macro("DECLARE_int32")
18431883
#pragma pop_macro("DECLARE_uint32")
18441884

0 commit comments

Comments
 (0)