• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #ifndef GRPC_SRC_CORE_LIB_GPR_LOG_INTERNAL_H
15 #define GRPC_SRC_CORE_LIB_GPR_LOG_INTERNAL_H
16 
17 #include <grpc/support/port_platform.h>
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 #include <cstring>
23 
24 /// abort() the process if x is zero, with rudimentary logging to prevent
25 /// circular dependencies with gpr_log.
26 
27 /// Intended for internal invariants.  If the error can be recovered from,
28 /// without the possibility of corruption, or might best be reflected via
29 /// an exception in a higher-level language, consider returning error code.
30 #define GPR_ASSERT_INTERNAL(x)                     \
31   do {                                             \
32     if (GPR_UNLIKELY(!(x))) {                      \
33       fprintf(stderr, "assertion failed: %s", #x); \
34       abort();                                     \
35     }                                              \
36   } while (0)
37 
38 #ifndef NDEBUG
39 #define GPR_DEBUG_ASSERT_INTERNAL(x) GPR_ASSERT_INTERNAL(x)
40 #else
41 #define GPR_DEBUG_ASSERT_INTERNAL(x)
42 #endif
43 
44 #define GPR_LOG_ERROR_INTERNAL(format, ...)                       \
45   do {                                                            \
46     char f[] = __FILE__;                                          \
47     char* display_file = f;                                       \
48     char* slash_pos = strrchr(f, '/');                            \
49     if (slash_pos != nullptr) display_file = slash_pos + 1;       \
50     char prefix[60];                                              \
51     sprintf(prefix, "INTERNAL %37s:%d]", display_file, __LINE__); \
52     fprintf(stderr, "%-60s " format "\n", prefix, __VA_ARGS__);   \
53   } while (0)
54 
55 #endif  // GRPC_SRC_CORE_LIB_GPR_LOG_INTERNAL_H
56