log.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. *
  3. * Copyright 2015 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. #ifndef GRPC_IMPL_CODEGEN_LOG_H
  19. #define GRPC_IMPL_CODEGEN_LOG_H
  20. // IWYU pragma: private, include <grpc/support/log.h>
  21. #include <grpc/impl/codegen/port_platform.h>
  22. #include <stdarg.h>
  23. #include <stdlib.h> /* for abort() */
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /** GPR log API.
  28. Usage (within grpc):
  29. int argument1 = 3;
  30. char* argument2 = "hello";
  31. gpr_log(GPR_DEBUG, "format string %d", argument1);
  32. gpr_log(GPR_INFO, "hello world");
  33. gpr_log(GPR_ERROR, "%d %s!!", argument1, argument2); */
  34. /** The severity of a log message - use the #defines below when calling into
  35. gpr_log to additionally supply file and line data */
  36. typedef enum gpr_log_severity {
  37. GPR_LOG_SEVERITY_DEBUG,
  38. GPR_LOG_SEVERITY_INFO,
  39. GPR_LOG_SEVERITY_ERROR
  40. } gpr_log_severity;
  41. /** Returns a string representation of the log severity */
  42. GPRAPI const char* gpr_log_severity_string(gpr_log_severity severity);
  43. /** Macros to build log contexts at various severity levels */
  44. #define GPR_DEBUG __FILE__, __LINE__, GPR_LOG_SEVERITY_DEBUG
  45. #define GPR_INFO __FILE__, __LINE__, GPR_LOG_SEVERITY_INFO
  46. #define GPR_ERROR __FILE__, __LINE__, GPR_LOG_SEVERITY_ERROR
  47. /** Log a message. It's advised to use GPR_xxx above to generate the context
  48. * for each message */
  49. GPRAPI void gpr_log(const char* file, int line, gpr_log_severity severity,
  50. const char* format, ...) GPR_PRINT_FORMAT_CHECK(4, 5);
  51. GPRAPI int gpr_should_log(gpr_log_severity severity);
  52. GPRAPI void gpr_log_message(const char* file, int line,
  53. gpr_log_severity severity, const char* message);
  54. /** Set global log verbosity */
  55. GPRAPI void gpr_set_log_verbosity(gpr_log_severity min_severity_to_print);
  56. GPRAPI void gpr_log_verbosity_init(void);
  57. /** Log overrides: applications can use this API to intercept logging calls
  58. and use their own implementations */
  59. struct gpr_log_func_args {
  60. const char* file;
  61. int line;
  62. gpr_log_severity severity;
  63. const char* message;
  64. };
  65. typedef struct gpr_log_func_args gpr_log_func_args;
  66. typedef void (*gpr_log_func)(gpr_log_func_args* args);
  67. GPRAPI void gpr_set_log_function(gpr_log_func func);
  68. /** abort() the process if x is zero, having written a line to the log.
  69. Intended for internal invariants. If the error can be recovered from,
  70. without the possibility of corruption, or might best be reflected via
  71. an exception in a higher-level language, consider returning error code. */
  72. #define GPR_ASSERT(x) \
  73. do { \
  74. if (GPR_UNLIKELY(!(x))) { \
  75. gpr_log(GPR_ERROR, "assertion failed: %s", #x); \
  76. abort(); \
  77. } \
  78. } while (0)
  79. #ifndef NDEBUG
  80. #define GPR_DEBUG_ASSERT(x) GPR_ASSERT(x)
  81. #else
  82. #define GPR_DEBUG_ASSERT(x)
  83. #endif
  84. #ifdef __cplusplus
  85. }
  86. #endif
  87. #endif /* GRPC_IMPL_CODEGEN_LOG_H */