• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2017 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // 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, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPC_SRC_CORE_LIB_GPRPP_DEBUG_LOCATION_H
20 #define GRPC_SRC_CORE_LIB_GPRPP_DEBUG_LOCATION_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <utility>
25 
26 #if defined(__has_builtin)
27 #if __has_builtin(__builtin_FILE)
28 #define GRPC_DEFAULT_FILE __builtin_FILE()
29 #endif
30 #endif
31 #ifndef GRPC_DEFAULT_FILE
32 #define GRPC_DEFAULT_FILE "<unknown>"
33 #endif
34 #if defined(__has_builtin)
35 #if __has_builtin(__builtin_LINE)
36 #define GRPC_DEFAULT_LINE __builtin_LINE()
37 #endif
38 #endif
39 #ifndef GRPC_DEFAULT_LINE
40 #define GRPC_DEFAULT_LINE -1
41 #endif
42 
43 namespace grpc_core {
44 
45 class SourceLocation {
46  public:
47   // NOLINTNEXTLINE
48   SourceLocation(const char* file = GRPC_DEFAULT_FILE,
49                  int line = GRPC_DEFAULT_LINE)
file_(file)50       : file_(file), line_(line) {}
file()51   const char* file() const { return file_; }
line()52   int line() const { return line_; }
53 
54  private:
55   const char* file_;
56   int line_;
57 };
58 
59 // Used for tracking file and line where a call is made for debug builds.
60 // No-op for non-debug builds.
61 // Callers can use the DEBUG_LOCATION macro in either case.
62 #ifndef NDEBUG
63 class DebugLocation {
64  public:
65   DebugLocation(const char* file = GRPC_DEFAULT_FILE,
66                 int line = GRPC_DEFAULT_LINE)
location_(file,line)67       : location_(file, line) {}
DebugLocation(SourceLocation location)68   explicit DebugLocation(SourceLocation location) : location_(location) {}
file()69   const char* file() const { return location_.file(); }
line()70   int line() const { return location_.line(); }
71 
72  private:
73   SourceLocation location_;
74 };
75 #else
76 class DebugLocation {
77  public:
DebugLocation()78   DebugLocation() {}
DebugLocation(SourceLocation)79   explicit DebugLocation(SourceLocation) {}
DebugLocation(const char *,int)80   DebugLocation(const char* /* file */, int /* line */) {}
file()81   const char* file() const { return nullptr; }
line()82   int line() const { return -1; }
83 };
84 #endif
85 
86 template <typename T>
87 struct ValueWithDebugLocation {
88   // NOLINTNEXTLINE
89   ValueWithDebugLocation(T&& value, DebugLocation debug_location = {})
valueValueWithDebugLocation90       : value(std::forward<T>(value)), debug_location(debug_location) {}
91   T value;
92   GPR_NO_UNIQUE_ADDRESS DebugLocation debug_location;
93 };
94 
95 #define DEBUG_LOCATION ::grpc_core::DebugLocation(__FILE__, __LINE__)
96 
97 }  // namespace grpc_core
98 
99 #endif  // GRPC_SRC_CORE_LIB_GPRPP_DEBUG_LOCATION_H
100