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, version 2.0,
7  as published by the Free Software Foundation.
8 
9  This program is also distributed with certain software (including
10  but not limited to OpenSSL) that is licensed under separate terms,
11  as designated in a particular file or component or in included license
12  documentation.  The authors of MySQL hereby grant you an additional
13  permission to link the program and your derivative works with the
14  separately licensed software that they have included with MySQL.
15 
16  This program is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  GNU General Public License, version 2.0, for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with this program; if not, write to the Free Software
23  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 */
25 /*
26  * helpers.hpp
27  */
28 
29 #ifndef helpers_hpp
30 #define helpers_hpp
31 
32 #include <stdio.h> // not using namespaces yet
33 #include <stdlib.h> // not using namespaces yet
34 
35 /************************************************************
36  * Helper Macros & Functions
37  ************************************************************/
38 
39 // need two levels of macro substitution
40 #define STRINGIFY(x) #x
41 #define TOSTRING(x) STRINGIFY(x)
42 
43 #define CHECK(cond, message)                    \
44     if (cond) ABORT_ERROR(message);
45 
46 // gcc: beware crashes when printing source code line number '<< __LINE__'
47 // C99's __func__ not supported by some C++ compilers yet (Solaris)
48 #define PRINT_ERROR(message)                                            \
49     do {                                                                \
50         fflush(stdout);                                                 \
51         fprintf(stderr, "\n!!! error, file: %s, line: %s, msg: %s.\n",  \
52                 (__FILE__), TOSTRING(__LINE__), (message));             \
53         fflush(stderr);                                                 \
54     } while (0)
55 
56 #define PRINT_ERROR_CODE(message, code)                                 \
57     do {                                                                \
58         fflush(stdout);                                                 \
59         fprintf(stderr, "\n!!! error, file: %s, line: %s, msg: %s, "    \
60                 "code %d.\n",                                           \
61                 (__FILE__), TOSTRING(__LINE__),                         \
62                 (message), (code));                                     \
63         fflush(stderr);                                                 \
64     } while (0)
65 
66 #define ABORT_ERROR(message)                                            \
67     do {                                                                \
68         PRINT_ERROR(message);                                           \
69         exit(-1);                                                       \
70     } while (0)
71 
72 // macro for printing verbose message
73 #if JTIE_VERBOSE
74 #  define VERBOSE(msg) fflush(stdout); printf("    %s\n", (msg));
75 #else
76 #  define VERBOSE(msg)
77 #endif
78 
79 // macros for tracing
80 #ifdef JTIE_TRACE
81 #  define ENTER(name) fflush(stdout); printf("--> %s\n", (name));
82 #  define LEAVE(name) printf("<-- %s\n", (name)); fflush(stdout);
83 #  define TRACE(name) JTieTracer _jtie_tracer(name);
84 #else
85 #  define ENTER(name)
86 #  define LEAVE(name)
87 #  define TRACE(name)
88 #endif // JTIE_TRACE
89 
90 // use as:
91 // myfunction() {
92 //   TRACE("myfunction()");
93 //   ...
94 // }
95 class JTieTracer
96 {
97     const char* const name;
98 public:
JTieTracer(const char * fname)99     JTieTracer(const char* fname) : name(fname) {
100         ENTER(name);
101     }
102 
~JTieTracer()103     ~JTieTracer() {
104         LEAVE(name);
105     }
106 };
107 
108 #endif // helpers_hpp
109