-
Notifications
You must be signed in to change notification settings - Fork 0
/
DebugStream.h
102 lines (84 loc) · 2.24 KB
/
DebugStream.h
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Copyright (C) 2024 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
// These contents may have been developed with support from one or more Intel-operated generative artificial intelligence solutions
// A simple mechanism to send debug messages to the debugger or the console
#pragma once
#include <ostream>
#include <sstream>
#include <iostream>
#ifndef DEBUGSTREAM_DEFAULT_PRINT
#define DEBUGSTREAM_DEFAULT_PRINT false
#endif
namespace XI
{
#ifndef DISABLE_DEBUGSTREAM
#define DISABLE_DEBUGSTREAM 0
#endif
#if DISABLE_DEBUGSTREAM
// Copilot: create class based on std::ostream that does nothing
class NullBuffer : public std::streambuf {
protected:
// Override the overflow function to discard output
int overflow(int c) override {
return c;
}
};
class NullBufferW : public std::wstreambuf {
protected:
// Override the overflow function to discard output
std::wstreambuf::int_type overflow(std::wstreambuf::int_type c) override {
return c;
}
};
class NullOStream : public std::ostream {
public:
NullOStream() : std::ostream(&nullBuffer) {}
private:
NullBuffer nullBuffer;
};
class NullOStreamW : public std::wostream {
public:
NullOStreamW() : std::wostream(&nullBuffer) {}
private:
NullBufferW nullBuffer;
};
#endif
class DebugStream
#if !DISABLE_DEBUGSTREAM
:public std::ostringstream
#else
:public NullOStream
#endif
{
public:
// false or default constructor prints only to debugger when present, true prints to stdout if no debugger
DebugStream(bool bPrintAlways=DEBUGSTREAM_DEFAULT_PRINT);
~DebugStream();
DebugStream(const DebugStream&) = delete;
DebugStream& operator=(const DebugStream&) = delete;
void OutputToDebugger() noexcept;
private:
bool bPrintAlways_;
};
class DebugStreamW
#if !DISABLE_DEBUGSTREAM
:public std::wostringstream
#else
: public NullOStreamW
#endif
{
public:
DebugStreamW(bool bPrintAlways = DEBUGSTREAM_DEFAULT_PRINT);
~DebugStreamW();
DebugStreamW(const DebugStreamW&) = delete;
DebugStreamW& operator=(const DebugStreamW&) = delete;
void OutputToDebugger() noexcept;
private:
bool bPrintAlways_;
};
#ifdef UNICODE
using DebugStreamT = DebugStreamW;
#else
using DebugStreamT = DebugStream;
#endif
} // XI