1 /* 2 Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved. 3 4 This program is free software; you can redistribute it and/or modify 5 it under the terms of the GNU General Public License, version 2.0, 6 as published by the Free Software Foundation. 7 8 This program is also distributed with certain software (including 9 but not limited to OpenSSL) that is licensed under separate terms, 10 as designated in a particular file or component or in included license 11 documentation. The authors of MySQL hereby grant you an additional 12 permission to link the program and your derivative works with the 13 separately licensed software that they have included with MySQL. 14 15 This program is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License, version 2.0, for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with this program; if not, write to the Free Software 22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 */ 24 /* 25 * helpers.hpp 26 */ 27 28 #ifndef helpers_hpp 29 #define helpers_hpp 30 31 #include <my_config.h> 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 #ifdef 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