Debian Codegraph @

src/core/lib/gprpp/debug_location.h

raw
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_CORE_LIB_GPRPP_DEBUG_LOCATION_H
20 #define GRPC_CORE_LIB_GPRPP_DEBUG_LOCATION_H
21
22 #if defined(__has_builtin)
23 #if __has_builtin(__builtin_FILE)
24 #define GRPC_DEFAULT_FILE __builtin_FILE()
25 #endif
26 #endif
27 #ifndef GRPC_DEFAULT_FILE
28 #define GRPC_DEFAULT_FILE "<unknown>"
29 #endif
30 #if defined(__has_builtin)
31 #if __has_builtin(__builtin_LINE)
32 #define GRPC_DEFAULT_LINE __builtin_LINE()
33 #endif
34 #endif
35 #ifndef GRPC_DEFAULT_LINE
36 #define GRPC_DEFAULT_LINE -1
37 #endif
38
39 namespace grpc_core {
40
41 class SourceLocation {
42 public:
43 // NOLINTNEXTLINE
44 SourceLocation(const char* file = GRPC_DEFAULT_FILE,
45 int line = GRPC_DEFAULT_LINE)
46 : file_(file), line_(line) {}
47 const char* file() const { return file_; }
48 int line() const { return line_; }
49
50 private:
51 const char* file_;
52 int line_;
53 };
54
55 // Used for tracking file and line where a call is made for debug builds.
56 // No-op for non-debug builds.
57 // Callers can use the DEBUG_LOCATION macro in either case.
58 #ifndef NDEBUG
59 // TODO(roth): See if there's a way to automatically populate this,
60 // similarly to how absl::SourceLocation::current() works, so that
61 // callers don't need to explicitly pass DEBUG_LOCATION anywhere.
62 class DebugLocation {
63 public:
64 DebugLocation(const char* file = GRPC_DEFAULT_FILE,
65 int line = GRPC_DEFAULT_LINE)
66 : location_(file, line) {}
67 const char* file() const { return location_.file(); }
68 int line() const { return location_.line(); }
69
70 private:
71 SourceLocation location_;
72 };
73 #else
74 class DebugLocation {
75 public:
76 DebugLocation() {}
77 DebugLocation(const char* /* file */, int /* line */) {}
78 const char* file() const { return nullptr; }
79 int line() const { return -1; }
80 };
81 #endif
82
83 #define DEBUG_LOCATION ::grpc_core::DebugLocation(__FILE__, __LINE__)
84
85 } // namespace grpc_core
86
87 #endif /* GRPC_CORE_LIB_GPRPP_DEBUG_LOCATION_H */