summaryrefslogtreecommitdiffstats
path: root/debug.hpp
blob: c53fc45a5b671feb9335cc65d6ccba3b6ecd8824 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once

#include <iostream>
#include <sstream>

#ifdef DEBUG
	#ifdef __builtin_strrchr
		#define __FILENAME__ (\
			__builtin_strrchr(__FILE__, '/') ? \
			__builtin_strrchr(__FILE__, '/') + 1 : __FILE__)

		#define npdebug_fname(); { \
				std::cerr << "[" << __FILENAME__ << ":" << __LINE__ << "] "; \
		}

		#define npdebug(...); { \
			npdebug_fname(); \
			np::va_debug(__VA_ARGS__); \
		}
	#else
		#define npdebug(...); np::va_debug(__VA_ARGS__);
	#endif

	namespace np {
		template<typename... Args>
		inline void va_debug(Args&... args) {
			(std::cerr << ... << args) << std::endl;
		}

		template<typename T>
		void range_debug(const T& t) {
			range_debug("", t);
		}

		template<typename T>
		void range_debug(const std::string& msg, const T& t) {
			std::string out;
			for (auto elem : t)
				out += elem += ", ";

			npdebug(msg, out);
		}

		template<typename T>
		T inspect(const T& t) {
			npdebug(t);
			return t;
		}

		template<typename T>
		T inspect(const std::string& msg, const T& t) {
			npdebug(msg, t);
			return t;
		}
	}
#else
	#define npdebug(...) {}

	namespace np {
		template<typename... Args>
		inline void va_debug(Args&... args) {}

		template<typename T>
		inline void range_debug(const T& t) {}

		template<typename T>
		inline void range_debug(const std::string& msg, const T& t) {}

		template<typename T>
		T inspect(const T& t) { return t; }

		template<typename T>
		T inspect(const std::string& msg, const T& t) { return t; }
	}
#endif