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