Skip to content

Commit

Permalink
Merge branch 'feature/support_unicode_paths_with_template' into featu…
Browse files Browse the repository at this point in the history
…re/support_unicode_paths
  • Loading branch information
xfc1939 committed Jan 30, 2024
2 parents 68b6ce3 + ff7fbed commit 7102881
Show file tree
Hide file tree
Showing 7 changed files with 449 additions and 175 deletions.
26 changes: 23 additions & 3 deletions src/base/commandlineflags.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@
#include <string>

#include "config.h"
#include "../utilities.h"

#ifdef GLOG_USE_GFLAGS

# include <gflags/gflags.h>
#include <gflags/gflags.h>

#else

# include "glog/logging.h"
#include "glog/logging.h"

# define DECLARE_VARIABLE(type, shorttype, name, tn) \
namespace fL##shorttype { \
Expand Down Expand Up @@ -107,6 +108,18 @@
} \
using fLS::FLAGS_##name

#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 // GLOG_USE_GFLAGS

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

#define GLOG_DEFINE_wstring(name, value, meaning) \
DEFINE_wstring(name, EnvToString(L"GLOG_" #name, value), meaning)


// These macros (could be functions, but I don't want to bother with a .cc
// file), make it easier to initialize flags from the environment.

#define EnvToString(envname, dflt) (!getenv(envname) ? (dflt) : getenv(envname))
#define EnvToString(envname, dflt) \
(google::logging::internal::getenv(envname).empty() \
? (dflt) \
: google::logging::internal::getenv(envname))

#define EnvToBool(envname, dflt) \
(!getenv(envname) ? (dflt) \
Expand Down
50 changes: 40 additions & 10 deletions src/flags.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,46 @@

#include <cstdlib>
#include <cstring>

#include <array>
#include "base/commandlineflags.h"
#include "glog/log_severity.h"

#include "utilities.h"
namespace {

// Compute the default value for --log_dir
static const char* DefaultLogDir() {
constexpr const char* const names[]{"GOOGLE_LOG_DIR", "TEST_TMPDIR"};
for (const char* const name : names) {
const char* const env = std::getenv(name);
if (env != nullptr && env[0] != '\0') {
return env;

template <class Ch, int N>
std::basic_string<Ch> DefaultLogDir(const std::array<const Ch*, N>& names) {
for (const Ch* name : names) {
auto val = google::logging::internal::getenv(name);
if(!val.empty()) {
return val;
}

}
return {};
}

template<class Ch>
struct LogDirEnvVar;

template <>
struct LogDirEnvVar<char> {
constexpr static std::array<const char*, 2> names() noexcept {
return {"GOOGLE_LOG_DIR", "TEST_TMPDIR"};
}
return "";
};

template <>
struct LogDirEnvVar<wchar_t> {
constexpr static std::array<const wchar_t*, 2> names() noexcept {
return {L"GOOGLE_LOG_DIR", L"TEST_TMPDIR"};
}
};

template <class Ch>
decltype(auto) DefaultLogDir() {
return DefaultLogDir<Ch, 2>(LogDirEnvVar<Ch>::names());
}

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

GLOG_DEFINE_string(
log_dir, DefaultLogDir(),
log_dir, DefaultLogDir<char>(),
"If specified, logfiles are written into this directory instead "
"of the default logging directory.");

GLOG_DEFINE_wstring(
log_wdir, DefaultLogDir<wchar_t>(),
L"If specified, logfiles are written into this directory instead "
L"of the default logging directory.");

GLOG_DEFINE_string(log_link, "",
"Put additional links to the log "
"files in this directory");
Expand Down
19 changes: 19 additions & 0 deletions src/glog/flags.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#pragma push_macro("DECLARE_VARIABLE")
#pragma push_macro("DECLARE_bool")
#pragma push_macro("DECLARE_string")
#pragma push_macro("DECLARE_wstring")
#pragma push_macro("DECLARE_int32")
#pragma push_macro("DECLARE_uint32")

Expand All @@ -66,6 +67,10 @@
# undef DECLARE_string
#endif

#ifdef DECLARE_wstring
# undef DECLARE_wstring
#endif

#ifdef DECLARE_int32
# undef DECLARE_int32
#endif
Expand Down Expand Up @@ -100,8 +105,17 @@
extern GLOG_EXPORT std::string& FLAGS_##name; \
} \
using fLS::FLAGS_##name

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

#endif



DECLARE_int32(logemaillevel);
DECLARE_int32(logcleansecs);

Expand Down Expand Up @@ -157,6 +171,10 @@ DECLARE_int32(minloglevel);
// default logging directory.
DECLARE_string(log_dir);

// If specified, logfiles are written into this directory instead of the
// default logging directory.
DECLARE_wstring(log_wdir);

// Set the log file mode.
DECLARE_int32(logfile_mode);

Expand Down Expand Up @@ -185,6 +203,7 @@ DECLARE_bool(symbolize_stacktrace);
#pragma pop_macro("DECLARE_VARIABLE")
#pragma pop_macro("DECLARE_bool")
#pragma pop_macro("DECLARE_string")
#pragma pop_macro("DECLARE_wstring")
#pragma pop_macro("DECLARE_int32")
#pragma pop_macro("DECLARE_uint32")

Expand Down
10 changes: 8 additions & 2 deletions src/glog/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -1493,6 +1493,9 @@ GLOG_EXPORT void FlushLogFilesUnsafe(LogSeverity min_severity);
GLOG_EXPORT void SetLogDestination(LogSeverity severity,
const char* base_filename);

GLOG_EXPORT void SetLogDestination(LogSeverity severity,
const wchar_t* base_filename);

//
// 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 All @@ -1501,7 +1504,8 @@ GLOG_EXPORT void SetLogDestination(LogSeverity severity,
//
GLOG_EXPORT void SetLogSymlink(LogSeverity severity,
const char* symlink_basename);

GLOG_EXPORT void SetLogSymlink(LogSeverity severity,
const wchar_t* symlink_basename);
//
// Used to send logs to some other kind of destination
// Users should subclass LogSink and override send to do whatever they want.
Expand Down Expand Up @@ -1557,6 +1561,7 @@ GLOG_EXPORT void RemoveLogSink(LogSink* destination);
// name. Thread-safe.
//
GLOG_EXPORT void SetLogFilenameExtension(const char* filename_extension);
GLOG_EXPORT void SetLogFilenameExtension(const wchar_t* filename_extension);

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

GLOG_EXPORT const std::vector<std::string>& GetLoggingDirectories();
GLOG_EXPORT const std::vector<std::string> GetLoggingDirectories();
GLOG_EXPORT const std::vector<std::wstring>& GetLoggingDirectoriesW();

// 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
15 changes: 11 additions & 4 deletions src/googletest.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,38 @@

using std::map;
using std::string;
using std::wstring;
using std::vector;

namespace google {
extern void (*g_logging_fail_func)();
extern void GetExistingTempDirectories(std::vector<std::string>& list);
extern void GetExistingTempDirectories(std::vector<std::wstring>& list);
extern int posix_strerror_r(int err, char* buf, size_t len);
extern std::string StrError(int err);
} // namespace google

#undef GLOG_EXPORT
#define GLOG_EXPORT

static inline string GetTempDir() {
vector<string> temp_directories_list;
static inline wstring GetTempDirW() {
vector<wstring> temp_directories_list;
google::GetExistingTempDirectories(temp_directories_list);

if (temp_directories_list.empty()) {
fprintf(stderr, "No temporary directory found\n");
fwprintf(stderr, L"No temporary directory found\n");
exit(EXIT_FAILURE);
}

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

static inline string GetTempDir() {
wstring temp_dir = GetTempDirW();
return google::logging::internal::StrConvert<wchar_t, char>(temp_dir);
}


#if defined(GLOG_OS_WINDOWS) && defined(_MSC_VER) && !defined(TEST_SRC_DIR)
// The test will run in glog/vsproject/<project name>
// (e.g., glog/vsproject/logging_unittest).
Expand Down
Loading

0 comments on commit 7102881

Please sign in to comment.