File indexing completed on 2025-08-28 08:27:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018 #pragma once
0019
0020 #include <chrono>
0021 #include <iosfwd>
0022 #include <memory>
0023 #include <string_view>
0024
0025 #include "arrow/result.h"
0026 #include "arrow/status.h"
0027 #include "arrow/util/logging.h"
0028 #include "arrow/util/macros.h"
0029 #include "arrow/util/visibility.h"
0030
0031 namespace arrow {
0032 namespace util {
0033
0034 struct SourceLocation {
0035 const char* file = "";
0036 int line = 0;
0037 };
0038
0039 struct LogDetails {
0040 ArrowLogLevel severity = ArrowLogLevel::ARROW_INFO;
0041 std::chrono::system_clock::time_point timestamp = std::chrono::system_clock::now();
0042 SourceLocation source_location{};
0043 std::string_view message = "";
0044 };
0045
0046
0047
0048
0049
0050 class Logger {
0051 public:
0052 virtual ~Logger() = default;
0053
0054 virtual void Log(const LogDetails& details) = 0;
0055
0056 virtual bool Flush(std::chrono::microseconds timeout) { return true; }
0057 bool Flush() { return this->Flush(std::chrono::microseconds::max()); }
0058
0059 virtual bool is_enabled() const { return true; }
0060
0061 virtual ArrowLogLevel severity_threshold() const { return ArrowLogLevel::ARROW_TRACE; }
0062 };
0063
0064
0065 ARROW_EXPORT std::shared_ptr<Logger> MakeOStreamLogger(ArrowLogLevel severity_threshold);
0066
0067 ARROW_EXPORT std::shared_ptr<Logger> MakeOStreamLogger(ArrowLogLevel severity_threshold,
0068 std::ostream& sink);
0069
0070 class ARROW_EXPORT LoggerRegistry {
0071 public:
0072
0073
0074
0075
0076 static Status RegisterLogger(std::string_view name, std::shared_ptr<Logger> logger);
0077
0078
0079 static void UnregisterLogger(std::string_view name);
0080
0081
0082
0083
0084
0085 static std::shared_ptr<Logger> GetLogger(std::string_view name = "");
0086
0087
0088 static std::shared_ptr<Logger> GetDefaultLogger();
0089
0090 static void SetDefaultLogger(std::shared_ptr<Logger> logger);
0091 };
0092
0093
0094 class ARROW_EXPORT LogMessage {
0095 public:
0096
0097 LogMessage(ArrowLogLevel severity, std::shared_ptr<Logger> logger,
0098 SourceLocation source_location = {});
0099
0100
0101 LogMessage(ArrowLogLevel severity, std::string_view logger_name,
0102 SourceLocation source_location = {});
0103
0104 std::ostream& Stream();
0105
0106
0107
0108 template <typename... Args>
0109 LogMessage& Append(Args&&... args) {
0110 if constexpr (sizeof...(Args) > 0) {
0111 if (CheckIsEnabled()) {
0112 (Stream() << ... << args);
0113 }
0114 }
0115 return *this;
0116 }
0117
0118 private:
0119 bool CheckIsEnabled();
0120
0121 class Impl;
0122 std::shared_ptr<Impl> impl_;
0123 };
0124
0125 }
0126 }
0127
0128
0129
0130 #ifndef ARROW_MINIMUM_LOG_LEVEL
0131 # define ARROW_MINIMUM_LOG_LEVEL -1000
0132 #endif
0133
0134 #define ARROW_LOGGER_INTERNAL(LOGGER, LEVEL) \
0135 (::arrow::util::LogMessage(::arrow::util::ArrowLogLevel::ARROW_##LEVEL, LOGGER, \
0136 ::arrow::util::SourceLocation{__FILE__, __LINE__}))
0137
0138 static_assert(static_cast<int>(::arrow::util::ArrowLogLevel::ARROW_TRACE) == -2);
0139 #if ARROW_MINIMUM_LOG_LEVEL <= -2
0140 # define ARROW_LOGGER_TRACE(LOGGER, ...) \
0141 (ARROW_LOGGER_INTERNAL(LOGGER, TRACE).Append(__VA_ARGS__))
0142 #else
0143 # define ARROW_LOGGER_TRACE(...) ARROW_UNUSED(0)
0144 #endif
0145
0146 static_assert(static_cast<int>(::arrow::util::ArrowLogLevel::ARROW_DEBUG) == -1);
0147 #if ARROW_MINIMUM_LOG_LEVEL <= -1
0148 # define ARROW_LOGGER_DEBUG(LOGGER, ...) \
0149 (ARROW_LOGGER_INTERNAL(LOGGER, DEBUG).Append(__VA_ARGS__))
0150 #else
0151 # define ARROW_LOGGER_DEBUG(...) ARROW_UNUSED(0)
0152 #endif
0153
0154 static_assert(static_cast<int>(::arrow::util::ArrowLogLevel::ARROW_INFO) == 0);
0155 #if ARROW_MINIMUM_LOG_LEVEL <= 0
0156 # define ARROW_LOGGER_INFO(LOGGER, ...) \
0157 (ARROW_LOGGER_INTERNAL(LOGGER, INFO).Append(__VA_ARGS__))
0158 #else
0159 # define ARROW_LOGGER_INFO(...) ARROW_UNUSED(0)
0160 #endif
0161
0162 static_assert(static_cast<int>(::arrow::util::ArrowLogLevel::ARROW_WARNING) == 1);
0163 #if ARROW_MINIMUM_LOG_LEVEL <= 1
0164 # define ARROW_LOGGER_WARNING(LOGGER, ...) \
0165 (ARROW_LOGGER_INTERNAL(LOGGER, WARNING).Append(__VA_ARGS__))
0166 #else
0167 # define ARROW_LOGGER_WARNING(...) ARROW_UNUSED(0)
0168 #endif
0169
0170 static_assert(static_cast<int>(::arrow::util::ArrowLogLevel::ARROW_ERROR) == 2);
0171 #if ARROW_MINIMUM_LOG_LEVEL <= 2
0172 # define ARROW_LOGGER_ERROR(LOGGER, ...) \
0173 (ARROW_LOGGER_INTERNAL(LOGGER, ERROR).Append(__VA_ARGS__))
0174 #else
0175 # define ARROW_LOGGER_ERROR(...) ARROW_UNUSED(0)
0176 #endif
0177
0178 static_assert(static_cast<int>(::arrow::util::ArrowLogLevel::ARROW_FATAL) == 3);
0179 #if ARROW_MINIMUM_LOG_LEVEL <= 3
0180 # define ARROW_LOGGER_FATAL(LOGGER, ...) \
0181 (ARROW_LOGGER_INTERNAL(LOGGER, FATAL).Append(__VA_ARGS__))
0182 #else
0183 # define ARROW_LOGGER_FATAL(...) ARROW_UNUSED(0)
0184 #endif
0185
0186 #define ARROW_LOGGER_CALL(LOGGER, LEVEL, ...) ARROW_LOGGER_##LEVEL(LOGGER, __VA_ARGS__)