MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
helpers.hpp
1 /*
2  Copyright 2010 Sun Microsystems, Inc.
3  All rights reserved. Use is subject to license terms.
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; version 2 of the License.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 /*
19  * helpers.hpp
20  */
21 
22 #ifndef helpers_hpp
23 #define helpers_hpp
24 
25 #include <stdio.h> // not using namespaces yet
26 #include <stdlib.h> // not using namespaces yet
27 
28 /************************************************************
29  * Helper Macros & Functions
30  ************************************************************/
31 
32 // need two levels of macro substitution
33 #define STRINGIFY(x) #x
34 #define TOSTRING(x) STRINGIFY(x)
35 
36 #define CHECK(cond, message) \
37  if (cond) ABORT_ERROR(message);
38 
39 // gcc: beware crashes when printing source code line number '<< __LINE__'
40 // C99's __func__ not supported by some C++ compilers yet (Solaris)
41 #define PRINT_ERROR(message) \
42  do { \
43  fflush(stdout); \
44  fprintf(stderr, "\n!!! error, file: %s, line: %s, msg: %s.\n", \
45  (__FILE__), TOSTRING(__LINE__), (message)); \
46  fflush(stderr); \
47  } while (0)
48 
49 #define PRINT_ERROR_CODE(message, code) \
50  do { \
51  fflush(stdout); \
52  fprintf(stderr, "\n!!! error, file: %s, line: %s, msg: %s, " \
53  "code %d.\n", \
54  (__FILE__), TOSTRING(__LINE__), \
55  (message), (code)); \
56  fflush(stderr); \
57  } while (0)
58 
59 #define ABORT_ERROR(message) \
60  do { \
61  PRINT_ERROR(message); \
62  exit(-1); \
63  } while (0)
64 
65 // macro for printing verbose message
66 #if JTIE_VERBOSE
67 # define VERBOSE(msg) fflush(stdout); printf(" %s\n", (msg));
68 #else
69 # define VERBOSE(msg)
70 #endif
71 
72 // macros for tracing
73 #ifdef JTIE_TRACE
74 # define ENTER(name) fflush(stdout); printf("--> %s\n", (name));
75 # define LEAVE(name) printf("<-- %s\n", (name)); fflush(stdout);
76 # define TRACE(name) JTieTracer _jtie_tracer(name);
77 #else
78 # define ENTER(name)
79 # define LEAVE(name)
80 # define TRACE(name)
81 #endif // JTIE_TRACE
82 
83 // use as:
84 // myfunction() {
85 // TRACE("myfunction()");
86 // ...
87 // }
89 {
90  const char* const name;
91 public:
92  JTieTracer(const char* fname) : name(fname) {
93  ENTER(name);
94  }
95 
96  ~JTieTracer() {
97  LEAVE(name);
98  }
99 };
100 
101 #endif // helpers_hpp