Commit cb350102 authored by 's avatar

Use GLOG_* environment variables even when gflags is installed.

Define GLOG_DEFINE_*, which checks if the GLOG_* environment variable
is defined. If defined, GLOG_DEFINE_* passes the value and otherwise,
it passes the original default value. In this way, GLOG_DEFINE_* macro
uses the value specified by gflags first, then GLOG_* environment
variables, and finally it uses the default value if neither of them are
specified.


git-svn-id: https://google-glog.googlecode.com/svn/trunk@76 eb4d4688-79bd-11dd-afb4-1d65580434c0
parent 32735966
...@@ -116,6 +116,13 @@ environment variables, prefixing the flag name with "GLOG_", e.g. ...@@ -116,6 +116,13 @@ environment variables, prefixing the flag name with "GLOG_", e.g.
GLOG_logtostderr=1 ./your_application GLOG_logtostderr=1 ./your_application
</pre> </pre>
<!-- TODO(hamaji): Fill the version number
<p>By glog version 0.x.x, you can use GLOG_* environment variables
even if you have gflags. If both an environment variable and a flag
are specified, the value specified by a flag wins. E.g., if GLOG_v=0
and --v=1, the verbosity will be 1, not 0.
-->
<p>The following flags are most commonly used: <p>The following flags are most commonly used:
<dl> <dl>
......
...@@ -77,14 +77,13 @@ ...@@ -77,14 +77,13 @@
#define DECLARE_bool(name) \ #define DECLARE_bool(name) \
DECLARE_VARIABLE(bool, name, bool) DECLARE_VARIABLE(bool, name, bool)
#define DEFINE_bool(name, value, meaning) \ #define DEFINE_bool(name, value, meaning) \
DEFINE_VARIABLE(bool, name, EnvToBool("GLOG_" #name, value), meaning, bool) DEFINE_VARIABLE(bool, name, value, meaning, bool)
// int32 specialization // int32 specialization
#define DECLARE_int32(name) \ #define DECLARE_int32(name) \
DECLARE_VARIABLE(GOOGLE_NAMESPACE::int32, name, int32) DECLARE_VARIABLE(GOOGLE_NAMESPACE::int32, name, int32)
#define DEFINE_int32(name, value, meaning) \ #define DEFINE_int32(name, value, meaning) \
DEFINE_VARIABLE(GOOGLE_NAMESPACE::int32, name, \ DEFINE_VARIABLE(GOOGLE_NAMESPACE::int32, name, value, meaning, int32)
EnvToInt("GLOG_" #name, value), meaning, int32)
// Special case for string, because we have to specify the namespace // Special case for string, because we have to specify the namespace
// std::string, which doesn't play nicely with our FLAG__namespace hackery. // std::string, which doesn't play nicely with our FLAG__namespace hackery.
...@@ -93,14 +92,31 @@ ...@@ -93,14 +92,31 @@
extern GOOGLE_GLOG_DLL_DECL std::string FLAGS_##name; \ extern GOOGLE_GLOG_DLL_DECL std::string FLAGS_##name; \
} \ } \
using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name
#define DEFINE_string(name, value, meaning) \ #define DEFINE_string(name, value, meaning) \
namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead { \ namespace FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead { \
GOOGLE_GLOG_DLL_DECL std::string \ GOOGLE_GLOG_DLL_DECL std::string FLAGS_##name(value); \
FLAGS_##name(EnvToString("GLOG_" #name, value)); \
char FLAGS_no##name; \ char FLAGS_no##name; \
} \ } \
using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name using FLAG__namespace_do_not_use_directly_use_DECLARE_string_instead::FLAGS_##name
#endif // HAVE_LIB_GFLAGS
// Define GLOG_DEFINE_* using DEFINE_* . By using these macros, we
// have GLOG_* environ variables even if we have gflags installed.
//
// If both an environment variable and a flag are specified, the value
// specified by a flag wins. E.g., if GLOG_v=0 and --v=1, the
// verbosity will be 1, not 0.
#define GLOG_DEFINE_bool(name, value, meaning) \
DEFINE_bool(name, EnvToBool("GLOG_" #name, value), meaning)
#define GLOG_DEFINE_int32(name, value, meaning) \
DEFINE_int32(name, EnvToInt("GLOG_" #name, value), meaning)
#define GLOG_DEFINE_string(name, value, meaning) \
DEFINE_string(name, EnvToString("GLOG_" #name, value), meaning)
// These macros (could be functions, but I don't want to bother with a .cc // 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. // file), make it easier to initialize flags from the environment.
...@@ -113,6 +129,4 @@ ...@@ -113,6 +129,4 @@
#define EnvToInt(envname, dflt) \ #define EnvToInt(envname, dflt) \
(!getenv(envname) ? (dflt) : strtol(getenv(envname), NULL, 10)) (!getenv(envname) ? (dflt) : strtol(getenv(envname), NULL, 10))
#endif // HAVE_LIB_GFLAGS
#endif // BASE_COMMANDLINEFLAGS_H__ #endif // BASE_COMMANDLINEFLAGS_H__
...@@ -41,7 +41,8 @@ ...@@ -41,7 +41,8 @@
#include "googletest.h" #include "googletest.h"
#include "config.h" #include "config.h"
DEFINE_bool(demangle_filter, false, "Run demangle_unittest in filter mode"); GLOG_DEFINE_bool(demangle_filter, false,
"Run demangle_unittest in filter mode");
using namespace std; using namespace std;
using namespace GOOGLE_NAMESPACE; using namespace GOOGLE_NAMESPACE;
......
...@@ -89,15 +89,15 @@ static bool BoolFromEnv(const char *varname, bool defval) { ...@@ -89,15 +89,15 @@ static bool BoolFromEnv(const char *varname, bool defval) {
return memchr("tTyY1\0", valstr[0], 6) != NULL; return memchr("tTyY1\0", valstr[0], 6) != NULL;
} }
DEFINE_bool(logtostderr, BoolFromEnv("GOOGLE_LOGTOSTDERR", false), GLOG_DEFINE_bool(logtostderr, BoolFromEnv("GOOGLE_LOGTOSTDERR", false),
"log messages go to stderr instead of logfiles"); "log messages go to stderr instead of logfiles");
DEFINE_bool(alsologtostderr, BoolFromEnv("GOOGLE_ALSOLOGTOSTDERR", false), GLOG_DEFINE_bool(alsologtostderr, BoolFromEnv("GOOGLE_ALSOLOGTOSTDERR", false),
"log messages go to stderr in addition to logfiles"); "log messages go to stderr in addition to logfiles");
#ifdef OS_LINUX #ifdef OS_LINUX
DEFINE_bool(drop_log_memory, true, "Drop in-memory buffers of log contents. " GLOG_DEFINE_bool(drop_log_memory, true, "Drop in-memory buffers of log contents. "
"Logs can grow very quickly and they are rarely read before they " "Logs can grow very quickly and they are rarely read before they "
"need to be evicted from memory. Instead, drop them from memory " "need to be evicted from memory. Instead, drop them from memory "
"as soon as they are flushed to disk."); "as soon as they are flushed to disk.");
_START_GOOGLE_NAMESPACE_ _START_GOOGLE_NAMESPACE_
namespace logging { namespace logging {
static const int64 kPageSize = getpagesize(); static const int64 kPageSize = getpagesize();
...@@ -115,25 +115,25 @@ DEFINE_int32(stderrthreshold, ...@@ -115,25 +115,25 @@ DEFINE_int32(stderrthreshold,
"log messages at or above this level are copied to stderr in " "log messages at or above this level are copied to stderr in "
"addition to logfiles. This flag obsoletes --alsologtostderr."); "addition to logfiles. This flag obsoletes --alsologtostderr.");
DEFINE_string(alsologtoemail, "", GLOG_DEFINE_string(alsologtoemail, "",
"log messages go to these email addresses " "log messages go to these email addresses "
"in addition to logfiles"); "in addition to logfiles");
DEFINE_bool(log_prefix, true, GLOG_DEFINE_bool(log_prefix, true,
"Prepend the log prefix to the start of each log line"); "Prepend the log prefix to the start of each log line");
DEFINE_int32(minloglevel, 0, "Messages logged at a lower level than this don't " GLOG_DEFINE_int32(minloglevel, 0, "Messages logged at a lower level than this don't "
"actually get logged anywhere"); "actually get logged anywhere");
DEFINE_int32(logbuflevel, 0, GLOG_DEFINE_int32(logbuflevel, 0,
"Buffer log messages logged at this level or lower" "Buffer log messages logged at this level or lower"
" (-1 means don't buffer; 0 means buffer INFO only;" " (-1 means don't buffer; 0 means buffer INFO only;"
" ...)"); " ...)");
DEFINE_int32(logbufsecs, 30, GLOG_DEFINE_int32(logbufsecs, 30,
"Buffer log messages for at most this many seconds"); "Buffer log messages for at most this many seconds");
DEFINE_int32(logemaillevel, 999, GLOG_DEFINE_int32(logemaillevel, 999,
"Email log messages logged at this level or higher" "Email log messages logged at this level or higher"
" (0 means email all; 3 means email FATAL only;" " (0 means email all; 3 means email FATAL only;"
" ...)"); " ...)");
DEFINE_string(logmailer, "/bin/mail", GLOG_DEFINE_string(logmailer, "/bin/mail",
"Mailer used to send logging email"); "Mailer used to send logging email");
// Compute the default value for --log_dir // Compute the default value for --log_dir
static const char* DefaultLogDir() { static const char* DefaultLogDir() {
...@@ -149,21 +149,21 @@ static const char* DefaultLogDir() { ...@@ -149,21 +149,21 @@ static const char* DefaultLogDir() {
return ""; return "";
} }
DEFINE_string(log_dir, DefaultLogDir(), GLOG_DEFINE_string(log_dir, DefaultLogDir(),
"If specified, logfiles are written into this directory instead " "If specified, logfiles are written into this directory instead "
"of the default logging directory."); "of the default logging directory.");
DEFINE_string(log_link, "", "Put additional links to the log " GLOG_DEFINE_string(log_link, "", "Put additional links to the log "
"files in this directory"); "files in this directory");
DEFINE_int32(max_log_size, 1800, GLOG_DEFINE_int32(max_log_size, 1800,
"approx. maximum log file size (in MB). A value of 0 will " "approx. maximum log file size (in MB). A value of 0 will "
"be silently overridden to 1."); "be silently overridden to 1.");
DEFINE_bool(stop_logging_if_full_disk, false, GLOG_DEFINE_bool(stop_logging_if_full_disk, false,
"Stop attempting to log to disk if the disk is full."); "Stop attempting to log to disk if the disk is full.");
DEFINE_string(log_backtrace_at, "", GLOG_DEFINE_string(log_backtrace_at, "",
"Emit a backtrace when logging at file:linenum."); "Emit a backtrace when logging at file:linenum.");
// TODO(hamaji): consider windows // TODO(hamaji): consider windows
#define PATH_SEPARATOR '/' #define PATH_SEPARATOR '/'
......
...@@ -63,8 +63,8 @@ _END_GOOGLE_NAMESPACE_ ...@@ -63,8 +63,8 @@ _END_GOOGLE_NAMESPACE_
#include "symbolize.h" #include "symbolize.h"
#include "base/commandlineflags.h" #include "base/commandlineflags.h"
DEFINE_bool(symbolize_stacktrace, true, GLOG_DEFINE_bool(symbolize_stacktrace, true,
"Symbolize the stack trace in the tombstone"); "Symbolize the stack trace in the tombstone");
_START_GOOGLE_NAMESPACE_ _START_GOOGLE_NAMESPACE_
......
...@@ -49,10 +49,10 @@ ...@@ -49,10 +49,10 @@
using std::string; using std::string;
DEFINE_int32(v, 0, "Show all VLOG(m) messages for m <= this." GLOG_DEFINE_int32(v, 0, "Show all VLOG(m) messages for m <= this."
" Overridable by --vmodule."); " Overridable by --vmodule.");
DEFINE_string(vmodule, "", "per-module verbose level." GLOG_DEFINE_string(vmodule, "", "per-module verbose level."
" Argument is a comma-separated list of <module name>=<log level>." " Argument is a comma-separated list of <module name>=<log level>."
" <module name> is a glob pattern, matched against the filename base" " <module name> is a glob pattern, matched against the filename base"
" (that is, name ignoring .cc/.h./-inl.h)." " (that is, name ignoring .cc/.h./-inl.h)."
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment