Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions cpp/src/arrow/util/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <iostream>

#ifdef ARROW_USE_GLOG
#include <signal.h>
#include <vector>
#include "glog/logging.h"
#endif

Expand Down Expand Up @@ -83,7 +85,8 @@ typedef CerrLog LoggingProvider;
#endif

ArrowLogLevel ArrowLog::severity_threshold_ = ArrowLogLevel::ARROW_INFO;
std::unique_ptr<std::string> ArrowLog::app_name_;
// Keep the log directory.
static std::unique_ptr<std::string> log_dir_;

#ifdef ARROW_USE_GLOG

Expand Down Expand Up @@ -113,10 +116,14 @@ void ArrowLog::StartArrowLog(const std::string& app_name,
ArrowLogLevel severity_threshold,
const std::string& log_dir) {
severity_threshold_ = severity_threshold;
app_name_.reset(new std::string(app_name.c_str()));
// In InitGoogleLogging, it simply keeps the pointer.
// We need to make sure the app name passed to InitGoogleLogging exist.
// We should avoid using static string is a dynamic lib.
static std::unique_ptr<std::string> app_name_;
app_name_.reset(new std::string(app_name));
log_dir_.reset(new std::string(log_dir));
#ifdef ARROW_USE_GLOG
int mapped_severity_threshold = GetMappedSeverity(severity_threshold_);
google::InitGoogleLogging(app_name_->c_str());
google::SetStderrLogging(mapped_severity_threshold);
// Enble log file if log_dir is not empty.
if (!log_dir.empty()) {
Expand All @@ -134,15 +141,41 @@ void ArrowLog::StartArrowLog(const std::string& app_name,
app_name_without_path = app_name.substr(pos + 1);
}
}
// If InitGoogleLogging is called but SetLogDestination is not called,
// the log will be output to /tmp besides stderr. If log_dir is not
// provided, we'd better not call InitGoogleLogging.
google::InitGoogleLogging(app_name_->c_str());
google::SetLogFilenameExtension(app_name_without_path.c_str());
google::SetLogDestination(mapped_severity_threshold, log_dir.c_str());
for (int i = static_cast<int>(severity_threshold_);
i <= static_cast<int>(ArrowLogLevel::ARROW_FATAL); ++i) {
int level = GetMappedSeverity(static_cast<ArrowLogLevel>(i));
google::SetLogDestination(level, dir_ends_with_slash.c_str());
}
}
#endif
}

void ArrowLog::UninstallSignalAction() {
#ifdef ARROW_USE_GLOG
ARROW_LOG(DEBUG) << "Uninstall signal handlers.";
// This signal list comes from glog's signalhandler.cc.
// https://github.com/google/glog/blob/master/src/signalhandler.cc#L58-L70
std::vector<int> installed_signals({SIGSEGV, SIGILL, SIGFPE, SIGABRT, SIGTERM});
struct sigaction sig_action;
memset(&sig_action, 0, sizeof(sig_action));
sigemptyset(&sig_action.sa_mask);
sig_action.sa_handler = SIG_DFL;
for (int signal_num : installed_signals) {
ARROW_CHECK(sigaction(signal_num, &sig_action, NULL) == 0);
}
#endif
}

void ArrowLog::ShutDownArrowLog() {
#ifdef ARROW_USE_GLOG
google::ShutdownGoogleLogging();
if (!log_dir_->empty()) {
google::ShutdownGoogleLogging();
}
#endif
}

Expand Down
6 changes: 3 additions & 3 deletions cpp/src/arrow/util/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ class ARROW_EXPORT ArrowLog : public ArrowLogBase {
/// If glog is not installed, this function won't do anything.
static void InstallFailureSignalHandler();

/// Uninstall the signal actions installed by InstallFailureSignalHandler.
static void UninstallSignalAction();

/// Return whether or not the log level is enabled in current setting.
///
/// \param log_level The input log level to test.
Expand All @@ -183,9 +186,6 @@ class ARROW_EXPORT ArrowLog : public ArrowLogBase {
bool is_enabled_;

static ArrowLogLevel severity_threshold_;
// In InitGoogleLogging, it simply keeps the pointer.
// We need to make sure the app name passed to InitGoogleLogging exist.
static std::unique_ptr<std::string> app_name_;

protected:
virtual std::ostream& Stream();
Expand Down
1 change: 1 addition & 0 deletions cpp/src/plasma/store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,7 @@ int main(int argc, char* argv[]) {
plasma::g_runner->Shutdown();
plasma::g_runner = nullptr;

ArrowLog::UninstallSignalAction();
ArrowLog::ShutDownArrowLog();
return 0;
}