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

Commit 7102881

Browse files
author
xfc1939
committed
Merge branch 'feature/support_unicode_paths_with_template' into feature/support_unicode_paths
2 parents 68b6ce3 + ff7fbed commit 7102881

File tree

7 files changed

+449
-175
lines changed

7 files changed

+449
-175
lines changed

src/base/commandlineflags.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@
5353
#include <string>
5454

5555
#include "config.h"
56+
#include "../utilities.h"
5657

5758
#ifdef GLOG_USE_GFLAGS
5859

59-
# include <gflags/gflags.h>
60+
#include <gflags/gflags.h>
6061

6162
#else
6263

63-
# include "glog/logging.h"
64+
#include "glog/logging.h"
6465

6566
# define DECLARE_VARIABLE(type, shorttype, name, tn) \
6667
namespace fL##shorttype { \
@@ -107,6 +108,18 @@
107108
} \
108109
using fLS::FLAGS_##name
109110

111+
#define DECLARE_wstring(name) \
112+
namespace fLS { \
113+
extern GLOG_EXPORT std::wstring& FLAGS_##name; \
114+
} \
115+
using fLS::FLAGS_##name
116+
#define DEFINE_wstring(name, value, meaning) \
117+
namespace fLS { \
118+
std::wstring FLAGS_##name##_buf(value); \
119+
GLOG_EXPORT std::wstring& FLAGS_##name = FLAGS_##name##_buf; \
120+
wchar_t FLAGS_no##name; \
121+
} \
122+
using fLS::FLAGS_##name
110123
#endif // GLOG_USE_GFLAGS
111124

112125
// Define GLOG_DEFINE_* using DEFINE_* . By using these macros, we
@@ -128,10 +141,17 @@
128141
#define GLOG_DEFINE_string(name, value, meaning) \
129142
DEFINE_string(name, EnvToString("GLOG_" #name, value), meaning)
130143

144+
#define GLOG_DEFINE_wstring(name, value, meaning) \
145+
DEFINE_wstring(name, EnvToString(L"GLOG_" #name, value), meaning)
146+
147+
131148
// These macros (could be functions, but I don't want to bother with a .cc
132149
// file), make it easier to initialize flags from the environment.
133150

134-
#define EnvToString(envname, dflt) (!getenv(envname) ? (dflt) : getenv(envname))
151+
#define EnvToString(envname, dflt) \
152+
(google::logging::internal::getenv(envname).empty() \
153+
? (dflt) \
154+
: google::logging::internal::getenv(envname))
135155

136156
#define EnvToBool(envname, dflt) \
137157
(!getenv(envname) ? (dflt) \

src/flags.cc

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,46 @@
3232

3333
#include <cstdlib>
3434
#include <cstring>
35-
35+
#include <array>
3636
#include "base/commandlineflags.h"
3737
#include "glog/log_severity.h"
38-
38+
#include "utilities.h"
3939
namespace {
4040

4141
// Compute the default value for --log_dir
42-
static const char* DefaultLogDir() {
43-
constexpr const char* const names[]{"GOOGLE_LOG_DIR", "TEST_TMPDIR"};
44-
for (const char* const name : names) {
45-
const char* const env = std::getenv(name);
46-
if (env != nullptr && env[0] != '\0') {
47-
return env;
42+
43+
template <class Ch, int N>
44+
std::basic_string<Ch> DefaultLogDir(const std::array<const Ch*, N>& names) {
45+
for (const Ch* name : names) {
46+
auto val = google::logging::internal::getenv(name);
47+
if(!val.empty()) {
48+
return val;
4849
}
50+
51+
}
52+
return {};
53+
}
54+
55+
template<class Ch>
56+
struct LogDirEnvVar;
57+
58+
template <>
59+
struct LogDirEnvVar<char> {
60+
constexpr static std::array<const char*, 2> names() noexcept {
61+
return {"GOOGLE_LOG_DIR", "TEST_TMPDIR"};
4962
}
50-
return "";
63+
};
64+
65+
template <>
66+
struct LogDirEnvVar<wchar_t> {
67+
constexpr static std::array<const wchar_t*, 2> names() noexcept {
68+
return {L"GOOGLE_LOG_DIR", L"TEST_TMPDIR"};
69+
}
70+
};
71+
72+
template <class Ch>
73+
decltype(auto) DefaultLogDir() {
74+
return DefaultLogDir<Ch, 2>(LogDirEnvVar<Ch>::names());
5175
}
5276

5377
bool BoolFromEnv(const char* varname, bool defval) {
@@ -123,9 +147,15 @@ GLOG_DEFINE_string(logmailer, "", "Mailer used to send logging email");
123147
GLOG_DEFINE_int32(logfile_mode, 0664, "Log file mode/permissions.");
124148

125149
GLOG_DEFINE_string(
126-
log_dir, DefaultLogDir(),
150+
log_dir, DefaultLogDir<char>(),
127151
"If specified, logfiles are written into this directory instead "
128152
"of the default logging directory.");
153+
154+
GLOG_DEFINE_wstring(
155+
log_wdir, DefaultLogDir<wchar_t>(),
156+
L"If specified, logfiles are written into this directory instead "
157+
L"of the default logging directory.");
158+
129159
GLOG_DEFINE_string(log_link, "",
130160
"Put additional links to the log "
131161
"files in this directory");

src/glog/flags.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#pragma push_macro("DECLARE_VARIABLE")
5252
#pragma push_macro("DECLARE_bool")
5353
#pragma push_macro("DECLARE_string")
54+
#pragma push_macro("DECLARE_wstring")
5455
#pragma push_macro("DECLARE_int32")
5556
#pragma push_macro("DECLARE_uint32")
5657

@@ -66,6 +67,10 @@
6667
# undef DECLARE_string
6768
#endif
6869

70+
#ifdef DECLARE_wstring
71+
# undef DECLARE_wstring
72+
#endif
73+
6974
#ifdef DECLARE_int32
7075
# undef DECLARE_int32
7176
#endif
@@ -100,8 +105,17 @@
100105
extern GLOG_EXPORT std::string& FLAGS_##name; \
101106
} \
102107
using fLS::FLAGS_##name
108+
109+
#define DECLARE_wstring(name) \
110+
namespace fLS { \
111+
extern GLOG_EXPORT std::wstring& FLAGS_##name; \
112+
} \
113+
using fLS::FLAGS_##name
114+
103115
#endif
104116

117+
118+
105119
DECLARE_int32(logemaillevel);
106120
DECLARE_int32(logcleansecs);
107121

@@ -157,6 +171,10 @@ DECLARE_int32(minloglevel);
157171
// default logging directory.
158172
DECLARE_string(log_dir);
159173

174+
// If specified, logfiles are written into this directory instead of the
175+
// default logging directory.
176+
DECLARE_wstring(log_wdir);
177+
160178
// Set the log file mode.
161179
DECLARE_int32(logfile_mode);
162180

@@ -185,6 +203,7 @@ DECLARE_bool(symbolize_stacktrace);
185203
#pragma pop_macro("DECLARE_VARIABLE")
186204
#pragma pop_macro("DECLARE_bool")
187205
#pragma pop_macro("DECLARE_string")
206+
#pragma pop_macro("DECLARE_wstring")
188207
#pragma pop_macro("DECLARE_int32")
189208
#pragma pop_macro("DECLARE_uint32")
190209

src/glog/logging.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,6 +1493,9 @@ GLOG_EXPORT void FlushLogFilesUnsafe(LogSeverity min_severity);
14931493
GLOG_EXPORT void SetLogDestination(LogSeverity severity,
14941494
const char* base_filename);
14951495

1496+
GLOG_EXPORT void SetLogDestination(LogSeverity severity,
1497+
const wchar_t* base_filename);
1498+
14961499
//
14971500
// Set the basename of the symlink to the latest log file at a given
14981501
// severity. If symlink_basename is empty, do not make a symlink. If
@@ -1501,7 +1504,8 @@ GLOG_EXPORT void SetLogDestination(LogSeverity severity,
15011504
//
15021505
GLOG_EXPORT void SetLogSymlink(LogSeverity severity,
15031506
const char* symlink_basename);
1504-
1507+
GLOG_EXPORT void SetLogSymlink(LogSeverity severity,
1508+
const wchar_t* symlink_basename);
15051509
//
15061510
// Used to send logs to some other kind of destination
15071511
// Users should subclass LogSink and override send to do whatever they want.
@@ -1557,6 +1561,7 @@ GLOG_EXPORT void RemoveLogSink(LogSink* destination);
15571561
// name. Thread-safe.
15581562
//
15591563
GLOG_EXPORT void SetLogFilenameExtension(const char* filename_extension);
1564+
GLOG_EXPORT void SetLogFilenameExtension(const wchar_t* filename_extension);
15601565

15611566
//
15621567
// Make it so that all log messages of at least a particular severity
@@ -1584,7 +1589,8 @@ GLOG_EXPORT void SetEmailLogging(LogSeverity min_severity,
15841589
GLOG_EXPORT bool SendEmail(const char* dest, const char* subject,
15851590
const char* body);
15861591

1587-
GLOG_EXPORT const std::vector<std::string>& GetLoggingDirectories();
1592+
GLOG_EXPORT const std::vector<std::string> GetLoggingDirectories();
1593+
GLOG_EXPORT const std::vector<std::wstring>& GetLoggingDirectoriesW();
15881594

15891595
// Print any fatal message again -- useful to call from signal handler
15901596
// so that the last thing in the output is the fatal message.

src/googletest.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,38 @@
7070

7171
using std::map;
7272
using std::string;
73+
using std::wstring;
7374
using std::vector;
7475

7576
namespace google {
7677
extern void (*g_logging_fail_func)();
77-
extern void GetExistingTempDirectories(std::vector<std::string>& list);
78+
extern void GetExistingTempDirectories(std::vector<std::wstring>& list);
7879
extern int posix_strerror_r(int err, char* buf, size_t len);
7980
extern std::string StrError(int err);
8081
} // namespace google
8182

8283
#undef GLOG_EXPORT
8384
#define GLOG_EXPORT
8485

85-
static inline string GetTempDir() {
86-
vector<string> temp_directories_list;
86+
static inline wstring GetTempDirW() {
87+
vector<wstring> temp_directories_list;
8788
google::GetExistingTempDirectories(temp_directories_list);
8889

8990
if (temp_directories_list.empty()) {
90-
fprintf(stderr, "No temporary directory found\n");
91+
fwprintf(stderr, L"No temporary directory found\n");
9192
exit(EXIT_FAILURE);
9293
}
9394

9495
// Use first directory from list of existing temporary directories.
9596
return temp_directories_list.front();
9697
}
9798

99+
static inline string GetTempDir() {
100+
wstring temp_dir = GetTempDirW();
101+
return google::logging::internal::StrConvert<wchar_t, char>(temp_dir);
102+
}
103+
104+
98105
#if defined(GLOG_OS_WINDOWS) && defined(_MSC_VER) && !defined(TEST_SRC_DIR)
99106
// The test will run in glog/vsproject/<project name>
100107
// (e.g., glog/vsproject/logging_unittest).

0 commit comments

Comments
 (0)