diff options
author | ccalvarin <ccalvarin@google.com> | 2018-03-23 15:35:00 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-03-23 15:36:36 -0700 |
commit | 7383976083bfd66f6bb5c4d4a288111b81731c70 (patch) | |
tree | 9d1799b1fcb57ca580e8cb84e2b41a3641b0c5a4 /src/main/cpp/util | |
parent | 9bad84084b9fa05e2eea7093611f0a414bf63b83 (diff) |
Fold in warning and error output into client logging.
To replace blaze_util::die and blaze_util::pdie as well, FATAL statements need to accept blaze exit codes.
RELNOTES: None.
PiperOrigin-RevId: 190285798
Diffstat (limited to 'src/main/cpp/util')
-rw-r--r-- | src/main/cpp/util/BUILD | 9 | ||||
-rw-r--r-- | src/main/cpp/util/bazel_log_handler.cc | 12 | ||||
-rw-r--r-- | src/main/cpp/util/errors.cc | 24 | ||||
-rw-r--r-- | src/main/cpp/util/errors.h | 2 | ||||
-rw-r--r-- | src/main/cpp/util/file_windows.cc | 6 | ||||
-rw-r--r-- | src/main/cpp/util/logging.cc | 27 |
6 files changed, 39 insertions, 41 deletions
diff --git a/src/main/cpp/util/BUILD b/src/main/cpp/util/BUILD index 11db4ca047..66bd722dbc 100644 --- a/src/main/cpp/util/BUILD +++ b/src/main/cpp/util/BUILD @@ -54,6 +54,7 @@ cc_library( deps = [ ":blaze_exit_code", ":errors", + ":logging", ":strings", ] + select({ "//src/conditions:windows": ["//src/main/native/windows:lib-file"], @@ -72,7 +73,11 @@ cc_library( ":ijar", "//src/main/cpp:__subpackages__", ], - deps = [":port"], + deps = [ + ":logging", + ":port", + ":strings", + ], ) cc_library( @@ -93,6 +98,7 @@ cc_library( srcs = ["logging.cc"], hdrs = ["logging.h"], visibility = ["//visibility:public"], + deps = [":blaze_exit_code"], ) cc_library( @@ -101,6 +107,7 @@ cc_library( hdrs = ["bazel_log_handler.h"], visibility = ["//visibility:public"], deps = [ + ":blaze_exit_code", ":file", ":logging", ], diff --git a/src/main/cpp/util/bazel_log_handler.cc b/src/main/cpp/util/bazel_log_handler.cc index b0ebeb49f7..31c173d289 100644 --- a/src/main/cpp/util/bazel_log_handler.cc +++ b/src/main/cpp/util/bazel_log_handler.cc @@ -19,6 +19,7 @@ #include <iostream> #include <sstream> +#include "src/main/cpp/util/exit_code.h" #include "src/main/cpp/util/file.h" #include "src/main/cpp/util/logging.h" @@ -51,11 +52,13 @@ void BazelLogHandler::HandleMessage(LogLevel level, const std::string& filename, // Select the appropriate stream to log to. std::ostream* log_stream; if (logging_deactivated_) { - // Do nothing if the output stream was explicitly deactivated, unless the - // level is USER, in which case the message is meant to be user-visible - // regardless of logging settings. + // If the output stream was explicitly deactivated, never print INFO + // messages, but USER should always be printed, as should warnings and + // errors. Omit the debug-level file and line number information, though. if (level == LOGLEVEL_USER) { std::cerr << message << std::endl; + } else if (level > LOGLEVEL_USER) { + std::cerr << LogLevelName(level) << ": " << message << std::endl; } return; } else if (output_stream_ == nullptr) { @@ -72,7 +75,8 @@ void BazelLogHandler::HandleMessage(LogLevel level, const std::string& filename, if (level == LOGLEVEL_FATAL) { std::cerr << "[bazel " << LogLevelName(level) << " " << filename << ":" << line << "] " << message << std::endl; - std::abort(); + // TODO(b/32967056) pass correct exit code information. + std::exit(blaze_exit_code::INTERNAL_ERROR); } } diff --git a/src/main/cpp/util/errors.cc b/src/main/cpp/util/errors.cc index 4fc6190e98..b337809325 100644 --- a/src/main/cpp/util/errors.cc +++ b/src/main/cpp/util/errors.cc @@ -19,8 +19,11 @@ #include <stdlib.h> #include <string.h> +#include "src/main/cpp/util/logging.h" + namespace blaze_util { +// TODO(b/32967056) This should be a FATAL log statement void die(const int exit_status, const char *format, ...) { va_list ap; va_start(ap, format); @@ -30,6 +33,7 @@ void die(const int exit_status, const char *format, ...) { exit(exit_status); } +// TODO(b/32967056) This should be a FATAL log statement void pdie(const int exit_status, const char *format, ...) { const char *errormsg = GetLastErrorString().c_str(); fprintf(stderr, "Error: "); @@ -41,24 +45,4 @@ void pdie(const int exit_status, const char *format, ...) { exit(exit_status); } -void PrintError(const char *format, ...) { - const char *errormsg = GetLastErrorString().c_str(); - fprintf(stderr, "ERROR: "); - va_list ap; - va_start(ap, format); - vfprintf(stderr, format, ap); - va_end(ap); - 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 diff --git a/src/main/cpp/util/errors.h b/src/main/cpp/util/errors.h index 62f1b3c90a..4bea4228de 100644 --- a/src/main/cpp/util/errors.h +++ b/src/main/cpp/util/errors.h @@ -29,8 +29,6 @@ void die(const int exit_status, const char *format, ...) ATTRIBUTE_NORETURN // Prints "Error: <formatted-message>: <strerror(errno)>\n", and exits nonzero. 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 diff --git a/src/main/cpp/util/file_windows.cc b/src/main/cpp/util/file_windows.cc index fd63214574..4768f5cd5c 100644 --- a/src/main/cpp/util/file_windows.cc +++ b/src/main/cpp/util/file_windows.cc @@ -23,6 +23,7 @@ #include "src/main/cpp/util/errors.h" #include "src/main/cpp/util/exit_code.h" #include "src/main/cpp/util/file.h" +#include "src/main/cpp/util/logging.h" #include "src/main/cpp/util/strings.h" #include "src/main/native/windows/file.h" #include "src/main/native/windows/util.h" @@ -405,9 +406,8 @@ bool MsysRoot::Get(string* path) { } else { const char* value2 = getenv("BAZEL_SH"); if (value2 == nullptr || value2[0] == '\0') { - PrintError( - "BAZEL_SH environment variable is not defined, cannot convert MSYS " - "paths to Windows paths"); + BAZEL_LOG(ERROR) << "BAZEL_SH environment variable is not defined, " + "cannot convert MSYS paths to Windows paths"; return false; } result = value2; diff --git a/src/main/cpp/util/logging.cc b/src/main/cpp/util/logging.cc index 8ae4b5dbb3..71c9a669f5 100644 --- a/src/main/cpp/util/logging.cc +++ b/src/main/cpp/util/logging.cc @@ -20,6 +20,8 @@ #include <iostream> #include <memory> +#include "src/main/cpp/util/exit_code.h" + namespace blaze_util { const char* LogLevelName(LogLevel level) { @@ -65,17 +67,20 @@ void LogMessage::Finish() { std::string message(message_.str()); if (log_handler_ != nullptr) { log_handler_->HandleMessage(level_, filename_, line_, message); - } else if (level_ == LOGLEVEL_USER) { - // Messages directed at the user should be printed even without a log - // handler. - std::cerr << message << std::endl; - } else if (level_ == LOGLEVEL_FATAL) { - // Expect the log_handler_ to handle FATAL calls, but we should still fail - // as expected even if no log_handler_ is defined. For ease of debugging, - // we also print out the error statement. - std::cerr << filename_ << ":" << line_ << " FATAL: " << message - << std::endl; - std::abort(); + } else { + // If no custom handler was provided, never print INFO messages, + // but USER should always be printed, as should warnings and errors. + if (level_ == LOGLEVEL_USER) { + std::cerr << message << std::endl; + } else if (level_ > LOGLEVEL_USER) { + std::cerr << LogLevelName(level_) << ": " << message << std::endl; + } + + if (level_ == LOGLEVEL_FATAL) { + // Exit for fatal calls after handling the message. + // TODO(b/32967056) pass correct exit code information. + std::exit(blaze_exit_code::INTERNAL_ERROR); + } } } |