KaCanOpen
 All Classes Functions Variables Typedefs Enumerations Pages
logger.h
1 /*
2  * Copyright (c) 2015, Thomas Keh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #pragma once
33 
34 #include <iostream>
35 
36 // DO NOT INCLUDE THIS IN A LIBRARY HEADER!
37 
38 // defined/not defined by CMake
39 // #define EXHAUSTIVE_DEBUGGING
40 
41 #define PRINT(x) std::cout << x << std::endl;
42 #define WARN(x) PRINT("WARNING: " << x);
43 #define ERROR(x) std::cerr << "ERROR: " << x << std::endl;
44 #define DUMP(x) PRINT(#x << " = " << std::dec << x);
45 #define DUMP_HEX(x) PRINT(#x << " = 0x" << std::hex << x);
46 
47 #ifdef NDEBUG
48  #define DEBUG(x)
49  #define DEBUG_LOG(x)
50  #define DEBUG_DUMP(x)
51  #define DEBUG_DUMP_HEX(x)
52 
53  #define DEBUG_EXHAUSTIVE(x)
54  #define DEBUG_LOG_EXHAUSTIVE(x)
55 #else
56  #define DEBUG(x) if (debug) { x }
57  #define DEBUG_LOG(x) if (debug) { PRINT("DEBUG: " << x); }
58  #define DEBUG_DUMP(x) if (debug) { PRINT("DEBUG: " << #x << " = " << std::dec << x); }
59  #define DEBUG_DUMP_HEX(x) if (debug) { PRINT("DEBUG: " << #x << " = 0x" << std::hex << x); }
60 
61  #ifdef EXHAUSTIVE_DEBUGGING
62  #define DEBUG_EXHAUSTIVE(x) if (debug) { x }
63  #define DEBUG_LOG_EXHAUSTIVE(x) if (debug) { PRINT("DEBUG_EXHAUSTIVE: " << x); }
64  #else
65  #define DEBUG_EXHAUSTIVE(x)
66  #define DEBUG_LOG_EXHAUSTIVE(x)
67  #endif
68 
69 #endif
70 
71 // Fix printing uint8_t and int8_t types using std::cout
72 namespace kaco {
73  namespace int8_printers {
74 
75  inline std::ostream &operator<<(std::ostream &os, char c) {
76  return os << (std::is_signed<char>::value ? static_cast<int>(c)
77  : static_cast<unsigned int>(c));
78  }
79 
80  inline std::ostream &operator<<(std::ostream &os, signed char c) {
81  return os << static_cast<int>(c);
82  }
83 
84  inline std::ostream &operator<<(std::ostream &os, unsigned char c) {
85  return os << static_cast<unsigned int>(c);
86  }
87  }
88 }
89 
90 // For correct usage of logger.h one should use the int8_printers, so
91 // the int8_printers namespace is automatically included. Because of
92 // this, logger.h should not be included in library headers.
93 using namespace kaco::int8_printers;