GRPC Core  1.0.0
log.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPC_IMPL_CODEGEN_LOG_H
35 #define GRPC_IMPL_CODEGEN_LOG_H
36 
37 #include <inttypes.h>
38 #include <stdarg.h>
39 #include <stdlib.h> /* for abort() */
40 
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 /* GPR log API.
48 
49  Usage (within grpc):
50 
51  int argument1 = 3;
52  char* argument2 = "hello";
53  gpr_log(GPR_DEBUG, "format string %d", argument1);
54  gpr_log(GPR_INFO, "hello world");
55  gpr_log(GPR_ERROR, "%d %s!!", argument1, argument2); */
56 
57 /* The severity of a log message - use the #defines below when calling into
58  gpr_log to additionally supply file and line data */
59 typedef enum gpr_log_severity {
64 
65 #define GPR_LOG_VERBOSITY_UNSET -1
66 
67 /* Returns a string representation of the log severity */
68 const char *gpr_log_severity_string(gpr_log_severity severity);
69 
70 /* Macros to build log contexts at various severity levels */
71 #define GPR_DEBUG __FILE__, __LINE__, GPR_LOG_SEVERITY_DEBUG
72 #define GPR_INFO __FILE__, __LINE__, GPR_LOG_SEVERITY_INFO
73 #define GPR_ERROR __FILE__, __LINE__, GPR_LOG_SEVERITY_ERROR
74 
75 /* Log a message. It's advised to use GPR_xxx above to generate the context
76  * for each message */
77 GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity,
78  const char *format, ...) GPRC_PRINT_FORMAT_CHECK(4, 5);
79 
80 GPRAPI void gpr_log_message(const char *file, int line,
81  gpr_log_severity severity, const char *message);
82 
83 /* Set global log verbosity */
84 GPRAPI void gpr_set_log_verbosity(gpr_log_severity min_severity_to_print);
85 
87 
88 /* Log overrides: applications can use this API to intercept logging calls
89  and use their own implementations */
90 
91 typedef struct {
92  const char *file;
93  int line;
94  gpr_log_severity severity;
95  const char *message;
97 
98 typedef void (*gpr_log_func)(gpr_log_func_args *args);
100 
101 /* abort() the process if x is zero, having written a line to the log.
102 
103  Intended for internal invariants. If the error can be recovered from,
104  without the possibility of corruption, or might best be reflected via
105  an exception in a higher-level language, consider returning error code. */
106 #define GPR_ASSERT(x) \
107  do { \
108  if (!(x)) { \
109  gpr_log(GPR_ERROR, "assertion failed: %s", #x); \
110  abort(); \
111  } \
112  } while (0)
113 
114 #ifdef __cplusplus
115 }
116 #endif
117 
118 #endif /* GRPC_IMPL_CODEGEN_LOG_H */
Definition: log.h:60
Definition: log.h:62
int line
Definition: log.h:93
Definition: log.h:91
GPRAPI void gpr_log_verbosity_init()
GPRAPI void gpr_set_log_verbosity(gpr_log_severity min_severity_to_print)
#define GPRAPI
Definition: port_platform.h:463
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPRC_PRINT_FORMAT_CHECK(4
const char * gpr_log_severity_string(gpr_log_severity severity)
GPRAPI void gpr_set_log_function(gpr_log_func func)
const char * message
Definition: log.h:95
const char * file
Definition: log.h:92
#define GPRC_PRINT_FORMAT_CHECK(FORMAT_STR, ARGS)
Definition: port_platform.h:447
void(* gpr_log_func)(gpr_log_func_args *args)
Definition: log.h:98
Definition: log.h:61
gpr_log_severity
Definition: log.h:59
gpr_log_severity severity
Definition: log.h:94
GPRAPI void GPRAPI void gpr_log_message(const char *file, int line, gpr_log_severity severity, const char *message)