1 /* Copyright (c) 2015, 2021, Oracle and/or its affiliates.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
22 
23 #ifndef TASK_DEBUG_H
24 #define TASK_DEBUG_H
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #include "xcom_common.h"
31 #include "x_platform.h"
32 
33 #ifdef TASK_DBUG_ON
34 #error "TASK_DBUG_ON already defined"
35 #else
36 #define TASK_DBUG_ON 0
37 #endif
38 
39 
40 #define TX_FMT "{" SY_FMT_DEF " %lu}"
41 #define TX_MEM(x) SY_MEM((x).cfg), (x).pc
42 
43 #include <stdio.h>
44 #include <stdlib.h>
45 
46 double       task_now();
47 
48 #ifdef DBGOUT
49 #error "DBGOUT defined"
50 #endif
51 
52 /* Log levels definition for use without external logger */
53 typedef enum
54 {
55   LOG_FATAL= 0,
56   LOG_ERROR= 1,
57   LOG_WARN= 2,
58   LOG_INFO= 3,
59   LOG_DEBUG= 4,
60   LOG_TRACE= 5
61 } xcom_log_level_t;
62 
63 static const char* const log_levels[]=
64 {
65   "[XCOM_FATAL] ",
66   "[XCOM_ERROR] ",
67   "[XCOM_WARN] ",
68   "[XCOM_INFO] ",
69   "[XCOM_DEBUG] ",
70   "[XCOM_TRACE] "
71 };
72 
73 typedef void (*xcom_logger)(int level, const char *message);
74 
75 
76 /**
77   Logger callback used in the logging macros.
78 */
79 
80 extern xcom_logger xcom_log;
81 
82 
83 /**
84   Prints the logging message to the console. It is invoked when no logger
85   callback was set.
86 */
87 
88 void xcom_simple_log(int l, const char *msg);
89 
90 
91 /**
92   Concatenates two strings and returns pointer to last character of final
93   string, allowing further concatenations without having to cycle through the
94   entire string again.
95 
96   @param dest pointer to last character of destination string
97   @param size pointer to the number of characters currently added to
98     xcom_log_buffer
99   @param src pointer to the string to append to dest
100   @return pointer to the last character of destination string after appending
101     dest, which corresponds to the position of the '\0' character
102 */
103 
104 char *mystrcat(char *dest, int *size, const char *src);
105 
106 
107 /**
108   This function allocates a new string where the format string and optional
109   arguments are rendered to.
110   Finally, it invokes mystr_cat to concatenate the rendered string to the
111   string received in the first parameter.
112 */
113 
114 char *mystrcat_sprintf(char *dest, int *size, const char *format, ...)
115   MY_ATTRIBUTE((format(printf, 3, 4)));
116 
117 #define STR_SIZE 2047
118 
119 #define GET_GOUT char xcom_log_buffer[STR_SIZE+1]; char *xcom_temp_buf= xcom_log_buffer; int xcom_log_buffer_size= 0; xcom_log_buffer[0]= 0
120 #define GET_NEW_GOUT char *xcom_log_buffer= (char *) malloc((STR_SIZE+1)*sizeof(char)); char *xcom_temp_buf= xcom_log_buffer; int xcom_log_buffer_size= 0; xcom_log_buffer[0]= 0
121 #define FREE_GOUT xcom_log_buffer[0]= 0
122 #define ADD_GOUT(s) xcom_temp_buf= mystrcat(xcom_temp_buf, &xcom_log_buffer_size, s)
123 #define COPY_AND_FREE_GOUT(s) {char *__funny = s; ADD_GOUT(__funny); free(__funny);}
124 #define ADD_F_GOUT(...) xcom_temp_buf= mystrcat_sprintf(xcom_temp_buf, &xcom_log_buffer_size, __VA_ARGS__)
125 #define PRINT_LEVEL_GOUT(level) xcom_log(level, xcom_log_buffer)
126 #define PRINT_GOUT PRINT_LEVEL_GOUT(LOG_DEBUG)
127 #define RET_GOUT return xcom_log_buffer
128 
129 #define G_LOG(l,...) { GET_GOUT; ADD_F_GOUT(__VA_ARGS__); PRINT_LEVEL_GOUT(l); FREE_GOUT; }
130 
131 #ifdef WITH_LOG_DEBUG
132 #define G_DEBUG(...) G_LOG(LOG_DEBUG,__VA_ARGS__)
133 #else
134 #define G_DEBUG(...)
135 #endif
136 
137 #ifdef WITH_LOG_TRACE
138 #define G_TRACE(...) G_LOG(LOG_TRACE,__VA_ARGS__)
139 #else
140 #define G_TRACE(...)
141 #endif
142 
143 #define G_WARNING(...) G_LOG(LOG_WARN,__VA_ARGS__)
144 #define g_critical(...) G_LOG(LOG_FATAL,__VA_ARGS__)
145 #define G_ERROR(...) G_LOG(LOG_ERROR,__VA_ARGS__)
146 #define G_MESSAGE(...) G_LOG(LOG_INFO, __VA_ARGS__)
147 
148 
149 #if TASK_DBUG_ON
150 #define DBGOHK(x) { GET_GOUT; ADD_F_GOUT("%f ",task_now()); NEXP(get_nodeno(get_site_def()),u); x; PRINT_GOUT; FREE_GOUT; }
151 #define DBGOUT(x) { GET_GOUT; ADD_F_GOUT("%f ",task_now()); x; PRINT_GOUT; FREE_GOUT; }
152 #define MAY_DBG(x) DBGOUT(x)
153 #else
154 #define DBGOHK(x)
155 #define DBGOUT(x)
156 #define MAY_DBG(x)
157 #endif
158 
159 #define g_string_append_printf fprintf
160 #define XDBG #error
161 #define FN ADD_GOUT(__func__); ADD_F_GOUT(" " __FILE__ ":%d ", __LINE__);
162 #define PTREXP(x) ADD_F_GOUT(#x ": %p ",(void*)(x))
163 #define PPUT(x) ADD_F_GOUT("0x%p ",(void*)(x))
164 #define STREXP(x) ADD_F_GOUT(#x ": %s ",x)
165 #define STRLIT(x) ADD_GOUT(x)
166 #define NPUT(x,f) ADD_F_GOUT("%" #f " ",x)
167 #define NDBG(x,f) ADD_F_GOUT(#x " = "); NPUT(x,f);
168 #define NEXP(x,f) ADD_F_GOUT(#x ": %" #f " ",x)
169 #define NUMEXP(x) NEXP(x,d)
170 #define g_strerror strerror
171 #define LOUT(pri,x) ADD_F_GOUT(x);
172 #define SYCEXP(exp) ADD_F_GOUT(#exp "={%x %llu %u} ", (exp).group_id, (long long unsigned int)((exp).msgno), ((exp).node))
173 #define TIDCEXP(exp) ADD_F_GOUT(#exp "={%x %llu %u %u} ", (exp).cfg.group_id, (long long unsigned int)(exp).cfg.msgno, (exp).cfg.node, (exp).pc)
174 #define TIMECEXP(exp) ADD_F_GOUT(#exp "=%f sec ", (exp))
175 #define BALCEXP(exp) ADD_F_GOUT(#exp "={%d %d} ", (exp).cnt, (exp).node)
176 
177 #include <string.h>
178 #include "result.h"
179 
180 #ifdef XCOM_HAVE_OPENSSL
task_dump_err(int err)181 static inline void task_dump_err(int err)
182 {
183 	if (err) {
184 		if (is_ssl_err(err)) {
185 			MAY_DBG(FN; NDBG(from_ssl_err(err), d));
186 		} else {
187 			MAY_DBG(FN; NDBG(from_errno(err), d); STREXP(strerror(err)));
188 		}
189 	}
190 }
191 #else
task_dump_err(int err)192 static inline void task_dump_err(int err)
193 {
194 	if (err) {
195 		MAY_DBG(FN; NDBG(to_errno(err), d); STREXP(strerror(err)));
196 	}
197 }
198 #endif
199 
200 #ifdef __cplusplus
201 }
202 #endif
203 
204 #endif
205 
206