diff options
| author | Odd Stranne <odd@mullvad.net> | 2018-04-26 15:56:12 +0200 |
|---|---|---|
| committer | Odd Stranne <odd@mullvad.net> | 2018-06-18 08:45:11 +0200 |
| commit | 562879a1b2ddb1f139a789952bb673965fc506dd (patch) | |
| tree | 9a4fcb70538773e84beae2014d4c876dc0438eb1 /windns/src | |
| parent | ee7fc70aa3b1fef15a596c37aa277d00e868f5af (diff) | |
| download | mullvadvpn-562879a1b2ddb1f139a789952bb673965fc506dd.tar.xz mullvadvpn-562879a1b2ddb1f139a789952bb673965fc506dd.zip | |
Add trace functionality for debug builds
Diffstat (limited to 'windns/src')
| -rw-r--r-- | windns/src/windns/consoletracesink.cpp | 8 | ||||
| -rw-r--r-- | windns/src/windns/consoletracesink.h | 8 | ||||
| -rw-r--r-- | windns/src/windns/itracesink.h | 48 | ||||
| -rw-r--r-- | windns/src/windns/macroargument.h | 23 |
4 files changed, 87 insertions, 0 deletions
diff --git a/windns/src/windns/consoletracesink.cpp b/windns/src/windns/consoletracesink.cpp new file mode 100644 index 0000000000..6e3d415303 --- /dev/null +++ b/windns/src/windns/consoletracesink.cpp @@ -0,0 +1,8 @@ +#include "stdafx.h" +#include "consoletracesink.h" +#include <iostream> + +void ConsoleTraceSink::trace(const wchar_t *msg) +{ + std::wcout << msg << std::endl; +} diff --git a/windns/src/windns/consoletracesink.h b/windns/src/windns/consoletracesink.h new file mode 100644 index 0000000000..765fb4a2a0 --- /dev/null +++ b/windns/src/windns/consoletracesink.h @@ -0,0 +1,8 @@ +#pragma once + +#include "itracesink.h" + +struct ConsoleTraceSink : public ITraceSink +{ + void trace(const wchar_t *msg) override; +}; diff --git a/windns/src/windns/itracesink.h b/windns/src/windns/itracesink.h new file mode 100644 index 0000000000..496a7df1b9 --- /dev/null +++ b/windns/src/windns/itracesink.h @@ -0,0 +1,48 @@ +#pragma once + +#include <sstream> + +struct ITraceSink +{ + virtual ~ITraceSink() = 0 + { + } + + virtual void trace(const wchar_t *msg) = 0; +}; + +struct NullTraceSink : public ITraceSink +{ + void trace(const wchar_t *) override + { + } +}; + +#ifdef _DEBUG +#define TRACING_ENABLED 1 +#else +#define TRACING_ENABLED 0 +#endif + +#if TRACING_ENABLED == 1 + +#include "macroargument.h" +#define XTRACE(...) VFUNC(XTRACE, __VA_ARGS__) + +#define XTRACE1(x)\ +{\ +std::wstringstream xtrace_ss;\ +xtrace_ss << __FUNCTIONW__ << L": " << x;\ +m_traceSink->trace(xtrace_ss.str().c_str());\ +} + +#define XTRACE2(x, y)\ +{\ +std::wstringstream xtrace_ss;\ +xtrace_ss << __FUNCTIONW__ << L": " << x << L" " << y;\ +m_traceSink->trace(xtrace_ss.str().c_str());\ +} + +#else +#define XTRACE(...) +#endif diff --git a/windns/src/windns/macroargument.h b/windns/src/windns/macroargument.h new file mode 100644 index 0000000000..bb27c124c8 --- /dev/null +++ b/windns/src/windns/macroargument.h @@ -0,0 +1,23 @@ +#pragma once + +//http://www.neff.co.at/2017/04/04/Overloading-Macros-on-the-Number-of-Arguments.html + +// Some auxiliary macros +#define EMPTY() +#define EXPAND(X) X +#define CONCAT(X,Y) X##Y + +// Get number of arguments passed by __VA_ARGS__ +// http://stackoverflow.com/questions/2124339/c-preprocessor-va-args-number-of-arguments +// NUMARGS: all arguments must be castable to int +// NARGS: pure preprocessor macro, maximum 9 arguments +#define NUMARGS(...) (sizeof((int[]){__VA_ARGS__})/sizeof(int)) +#define NARGS(...) _NARGS_I(_AUGMENT(__VA_ARGS__)) +#define _AUGMENT(...) _UNUSED_, __VA_ARGS__ +#define _NARGS_I(...) EXPAND(_ARG_N(__VA_ARGS__, 8, 7, 6, 5, 4, 3, 2, 1, 0)) +#define _ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, count, ...) count + +// Overloading Macro on Number of Arguments +// http://stackoverflow.com/questions/11761703/overloading-macro-on-number-of-arguments +#define _VFUNC(NAME, N) CONCAT(NAME, N) +#define VFUNC(FUNC, ...) CONCAT(_VFUNC(FUNC, NARGS(__VA_ARGS__))(__VA_ARGS__),EMPTY()) |
