forked from Haivision/srt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verbose.hpp
106 lines (85 loc) · 1.98 KB
/
verbose.hpp
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
103
104
105
106
/*
* SRT - Secure, Reliable, Transport
* Copyright (c) 2018 Haivision Systems Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
#ifndef INC_SRT_VERBOSE_HPP
#define INC_SRT_VERBOSE_HPP
#include <iostream>
#if SRT_ENABLE_VERBOSE_LOCK
#include <mutex>
#endif
namespace Verbose
{
extern bool on;
extern std::ostream* cverb;
struct LogNoEol { LogNoEol() {} };
#if SRT_ENABLE_VERBOSE_LOCK
struct LogLock { LogLock() {} };
#endif
class Log
{
bool noeol = false;
#if SRT_ENABLE_VERBOSE_LOCK
bool lockline = false;
#endif
// Disallow creating dynamic objects
void* operator new(size_t);
public:
template <class V>
Log& operator<<(const V& arg)
{
// Template - must be here; extern template requires
// predefined specializations.
if (on)
(*cverb) << arg;
return *this;
}
Log& operator<<(LogNoEol);
#if SRT_ENABLE_VERBOSE_LOCK
Log& operator<<(LogLock);
#endif
~Log();
};
class ErrLog: public Log
{
public:
template <class V>
ErrLog& operator<<(const V& arg)
{
// Template - must be here; extern template requires
// predefined specializations.
if (on)
(*cverb) << arg;
else
std::cerr << arg;
return *this;
}
};
// terminal
inline void Print(Log& ) {}
template <typename Arg1, typename... Args>
inline void Print(Log& out, Arg1&& arg1, Args&&... args)
{
out << arg1;
Print(out, args...);
}
}
inline Verbose::Log Verb() { return Verbose::Log(); }
inline Verbose::ErrLog Verror() { return Verbose::ErrLog(); }
template <typename... Args>
inline void Verb(Args&&... args)
{
Verbose::Log log;
Verbose::Print(log, args...);
}
// Manipulator tags
static const Verbose::LogNoEol VerbNoEOL;
#if SRT_ENABLE_VERBOSE_LOCK
static const Verbose::LogLock VerbLock;
#endif
#endif