Apache Arrow (C++)
A columnar in-memory analytics layer designed to accelerate big data.
logging.h
Go to the documentation of this file.
1 // Licensed to the Apache Software Foundation (ASF) under one
2 // or more contributor license agreements. See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership. The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17 
18 #ifndef ARROW_UTIL_LOGGING_H
19 #define ARROW_UTIL_LOGGING_H
20 
21 #include <cstdlib>
22 #include <iostream>
23 
24 #include "arrow/util/macros.h"
25 
26 namespace arrow {
27 
28 // Stubbed versions of macros defined in glog/logging.h, intended for
29 // environments where glog headers aren't available.
30 //
31 // Add more as needed.
32 
33 // Log levels. LOG ignores them, so their values are abitrary.
34 
35 #define ARROW_DEBUG (-1)
36 #define ARROW_INFO 0
37 #define ARROW_WARNING 1
38 #define ARROW_ERROR 2
39 #define ARROW_FATAL 3
40 
41 #define ARROW_LOG_INTERNAL(level) ::arrow::internal::CerrLog(level)
42 #define ARROW_LOG(level) ARROW_LOG_INTERNAL(ARROW_##level)
43 #define ARROW_IGNORE_EXPR(expr) ((void)(expr));
44 
45 #define ARROW_CHECK(condition) \
46  (condition) ? 0 \
47  : ::arrow::internal::FatalLog(ARROW_FATAL) \
48  << __FILE__ << ":" << __LINE__ << " Check failed: " #condition " "
49 
50 #ifdef NDEBUG
51 #define ARROW_DFATAL ARROW_WARNING
52 
53 #define DCHECK(condition) \
54  ARROW_IGNORE_EXPR(condition) \
55  while (false) ::arrow::internal::NullLog()
56 #define DCHECK_EQ(val1, val2) \
57  ARROW_IGNORE_EXPR(val1) \
58  while (false) ::arrow::internal::NullLog()
59 #define DCHECK_NE(val1, val2) \
60  ARROW_IGNORE_EXPR(val1) \
61  while (false) ::arrow::internal::NullLog()
62 #define DCHECK_LE(val1, val2) \
63  ARROW_IGNORE_EXPR(val1) \
64  while (false) ::arrow::internal::NullLog()
65 #define DCHECK_LT(val1, val2) \
66  ARROW_IGNORE_EXPR(val1) \
67  while (false) ::arrow::internal::NullLog()
68 #define DCHECK_GE(val1, val2) \
69  ARROW_IGNORE_EXPR(val1) \
70  while (false) ::arrow::internal::NullLog()
71 #define DCHECK_GT(val1, val2) \
72  ARROW_IGNORE_EXPR(val1) \
73  while (false) ::arrow::internal::NullLog()
74 
75 #else
76 #define ARROW_DFATAL ARROW_FATAL
77 
78 #define DCHECK(condition) ARROW_CHECK(condition)
79 #define DCHECK_EQ(val1, val2) ARROW_CHECK((val1) == (val2))
80 #define DCHECK_NE(val1, val2) ARROW_CHECK((val1) != (val2))
81 #define DCHECK_LE(val1, val2) ARROW_CHECK((val1) <= (val2))
82 #define DCHECK_LT(val1, val2) ARROW_CHECK((val1) < (val2))
83 #define DCHECK_GE(val1, val2) ARROW_CHECK((val1) >= (val2))
84 #define DCHECK_GT(val1, val2) ARROW_CHECK((val1) > (val2))
85 
86 #endif // NDEBUG
87 
88 namespace internal {
89 
90 class NullLog {
91  public:
92  template <class T>
93  NullLog& operator<<(const T& ARROW_ARG_UNUSED(t)) {
94  return *this;
95  }
96 };
97 
98 class CerrLog {
99  public:
100  CerrLog(int severity) // NOLINT(runtime/explicit)
101  : severity_(severity), has_logged_(false) {}
102 
103  virtual ~CerrLog() {
104  if (has_logged_) {
105  std::cerr << std::endl;
106  }
107  if (severity_ == ARROW_FATAL) {
108  std::exit(1);
109  }
110  }
111 
112  template <class T>
113  CerrLog& operator<<(const T& t) {
114  if (severity_ != ARROW_DEBUG) {
115  has_logged_ = true;
116  std::cerr << t;
117  }
118  return *this;
119  }
120 
121  protected:
122  const int severity_;
123  bool has_logged_;
124 };
125 
126 // Clang-tidy isn't smart enough to determine that DCHECK using CerrLog doesn't
127 // return so we create a new class to give it a hint.
128 class FatalLog : public CerrLog {
129  public:
130  explicit FatalLog(int /* severity */) // NOLINT
131  : CerrLog(ARROW_FATAL) {} // NOLINT
132 
133  ARROW_NORETURN ~FatalLog() {
134  if (has_logged_) {
135  std::cerr << std::endl;
136  }
137  std::exit(1);
138  }
139 };
140 
141 } // namespace internal
142 
143 } // namespace arrow
144 
145 #endif // ARROW_UTIL_LOGGING_H
#define ARROW_NORETURN
Definition: macros.h:47
#define ARROW_FATAL
Definition: logging.h:39
#define ARROW_DEBUG
Definition: logging.h:35
#define ARROW_ARG_UNUSED(x)
Definition: macros.h:29
Top-level namespace for Apache Arrow C++ API.
Definition: allocator.h:29