Skip to content

Commit

Permalink
cpp: add PrintWarning() function to errors.h
Browse files Browse the repository at this point in the history
It provides a single and clean way to output warning messages,
and replaces the fprintf(stderr, "Warning: ...\n") or
fprintf(stderr, "WARNING: ...\n") calls.

Change-Id: I2f8a8f659085b9e57a08b5208a8b8f683a7cd72c
PiperOrigin-RevId: 155386233
  • Loading branch information
tfarina authored and kchodorow committed May 9, 2017
1 parent 05ea028 commit c32ad5e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
31 changes: 16 additions & 15 deletions src/main/cpp/blaze.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@

using blaze_util::die;
using blaze_util::pdie;
using blaze_util::PrintWarning;

namespace blaze {

Expand Down Expand Up @@ -641,13 +642,13 @@ static void StartStandalone(const WorkspaceLayout *workspace_layout,
if (!command_arguments.empty() && command == "shutdown") {
string product = globals->options->product_name;
blaze_util::ToLower(&product);
fprintf(stderr,
"WARNING: Running command \"shutdown\" in batch mode. Batch mode "
"is triggered\nwhen not running %s within a workspace. If you "
"intend to shutdown an\nexisting %s server, run \"%s "
"shutdown\" from the directory where\nit was started.\n",
globals->options->product_name.c_str(),
globals->options->product_name.c_str(), product.c_str());
PrintWarning(
"Running command \"shutdown\" in batch mode. Batch mode "
"is triggered\nwhen not running %s within a workspace. If you "
"intend to shutdown an\nexisting %s server, run \"%s "
"shutdown\" from the directory where\nit was started.",
globals->options->product_name.c_str(),
globals->options->product_name.c_str(), product.c_str());
}
vector<string> jvm_args_vector = GetArgumentArray();
if (command != "") {
Expand Down Expand Up @@ -1042,10 +1043,10 @@ static void KillRunningServerIfDifferentStartupOptions(BlazeServer *server) {
// mortal coil.
if (ServerNeedsToBeKilled(arguments, GetArgumentArray())) {
globals->restart_reason = NEW_OPTIONS;
fprintf(stderr,
"WARNING: Running %s server needs to be killed, because the "
"startup options are different.\n",
globals->options->product_name.c_str());
PrintWarning(
"Running %s server needs to be killed, because the "
"startup options are different.",
globals->options->product_name.c_str());
server->KillRunningServer();
}
}
Expand Down Expand Up @@ -1233,7 +1234,7 @@ static void ComputeBaseDirectories(const WorkspaceLayout *workspace_layout,

static void CheckEnvironment() {
if (!blaze::GetEnv("http_proxy").empty()) {
fprintf(stderr, "Warning: ignoring http_proxy in environment.\n");
PrintWarning("ignoring http_proxy in environment.");
blaze::UnsetEnv("http_proxy");
}

Expand All @@ -1242,18 +1243,18 @@ static void CheckEnvironment() {
// specified, the JVM fails to create threads. See thread_stack_regtest.
// This is also provoked by LD_LIBRARY_PATH=/usr/lib/debug,
// or anything else that causes the JVM to use LinuxThreads.
fprintf(stderr, "Warning: ignoring LD_ASSUME_KERNEL in environment.\n");
PrintWarning("ignoring LD_ASSUME_KERNEL in environment.");
blaze::UnsetEnv("LD_ASSUME_KERNEL");
}

if (!blaze::GetEnv("LD_PRELOAD").empty()) {
fprintf(stderr, "Warning: ignoring LD_PRELOAD in environment.\n");
PrintWarning("ignoring LD_PRELOAD in environment.");
blaze::UnsetEnv("LD_PRELOAD");
}

if (!blaze::GetEnv("_JAVA_OPTIONS").empty()) {
// This would override --host_jvm_args
fprintf(stderr, "Warning: ignoring _JAVA_OPTIONS in environment.\n");
PrintWarning("ignoring _JAVA_OPTIONS in environment.");
blaze::UnsetEnv("_JAVA_OPTIONS");
}

Expand Down
14 changes: 7 additions & 7 deletions src/main/cpp/blaze_util_freebsd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ namespace blaze {

using blaze_util::die;
using blaze_util::pdie;
using blaze_util::PrintWarning;
using std::string;

string GetOutputRoot() {
Expand All @@ -57,17 +58,16 @@ string GetOutputRoot() {
void WarnFilesystemType(const string &output_base) {
struct statfs buf = {};
if (statfs(output_base.c_str(), &buf) < 0) {
fprintf(stderr,
"WARNING: couldn't get file system type information for '%s': %s\n",
output_base.c_str(), strerror(errno));
PrintWaring("couldn't get file system type information for '%s': %s",
output_base.c_str(), strerror(errno));
return;
}

if (strcmp(buf.f_fstypename, "nfs") == 0) {
fprintf(stderr,
"WARNING: Output base '%s' is on NFS. This may lead "
"to surprising failures and undetermined behavior.\n",
output_base.c_str());
PrintWarning(
"Output base '%s' is on NFS. This may lead "
"to surprising failures and undetermined behavior.",
output_base.c_str());
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/main/cpp/blaze_util_linux.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace blaze {

using blaze_util::die;
using blaze_util::pdie;
using blaze_util::PrintWarning;
using std::string;
using std::vector;

Expand Down Expand Up @@ -67,16 +68,16 @@ string GetOutputRoot() {
void WarnFilesystemType(const string& output_base) {
struct statfs buf = {};
if (statfs(output_base.c_str(), &buf) < 0) {
fprintf(stderr,
"WARNING: couldn't get file system type information for '%s': %s\n",
output_base.c_str(), strerror(errno));
PrintWarning("couldn't get file system type information for '%s': %s",
output_base.c_str(), strerror(errno));
return;
}

if (buf.f_type == NFS_SUPER_MAGIC) {
fprintf(stderr, "WARNING: Output base '%s' is on NFS. This may lead "
"to surprising failures and undetermined behavior.\n",
output_base.c_str());
PrintWarning(
"Output base '%s' is on NFS. This may lead "
"to surprising failures and undetermined behavior.",
output_base.c_str());
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/main/cpp/util/errors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,14 @@ void PrintError(const char *format, ...) {
fprintf(stderr, ": %s\n", errormsg);
}

void PrintWarning(const char *format, ...) {
va_list args;

va_start(args, format);
fputs("WARNING: ", stderr);
vfprintf(stderr, format, args);
fputc('\n', stderr);
va_end(args);
}

} // namespace blaze_util
1 change: 1 addition & 0 deletions src/main/cpp/util/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ void die(const int exit_status, const char *format, ...) ATTRIBUTE_NORETURN
void pdie(const int exit_status, const char *format, ...) ATTRIBUTE_NORETURN
PRINTF_ATTRIBUTE(2, 3);
void PrintError(const char *format, ...) PRINTF_ATTRIBUTE(1, 2);
void PrintWarning(const char *format, ...) PRINTF_ATTRIBUTE(1, 2);

// Returns the last error as a platform-specific error message.
// The string will also contain the platform-specific error code itself
Expand Down

0 comments on commit c32ad5e

Please sign in to comment.