Debian Codegraph @

apps/verbose.hpp

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2018 Haivision Systems Inc.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11 #ifndef INC_SRT_VERBOSE_HPP
12 #define INC_SRT_VERBOSE_HPP
13
14 #include <iostream>
15 #include "sync.h"
16
17 namespace Verbose
18 {
19
20 extern bool on;
21 extern std::ostream* cverb;
22
23 struct LogNoEol { LogNoEol() {} };
24 struct LogLock { LogLock() {} };
25
26 class Log
27 {
28 bool noeol = false;
29 srt::sync::atomic<bool> lockline;
30
31 // Disallow creating dynamic objects
32 void* operator new(size_t) = delete;
33
34 public:
35
36 Log() {}
37 Log(const Log& ) {}
38
39
40 template <class V>
41 Log& operator<<(const V& arg)
42 {
43 // Template - must be here; extern template requires
44 // predefined specializations.
45 if (on)
46 (*cverb) << arg;
47 return *this;
48 }
49
50 Log& operator<<(LogNoEol);
51 Log& operator<<(LogLock);
52 ~Log();
53 };
54
55
56 class ErrLog: public Log
57 {
58 public:
59
60 template <class V>
61 ErrLog& operator<<(const V& arg)
62 {
63 // Template - must be here; extern template requires
64 // predefined specializations.
65 if (on)
66 (*cverb) << arg;
67 else
68 std::cerr << arg;
69 return *this;
70 }
71 };
72
73 // terminal
74 inline void Print(Log& ) {}
75
76 template <typename Arg1, typename... Args>
77 inline void Print(Log& out, Arg1&& arg1, Args&&... args)
78 {
79 out << arg1;
80 Print(out, args...);
81 }
82
83 }
84
85 inline Verbose::Log Verb() { return Verbose::Log(); }
86 inline Verbose::ErrLog Verror() { return Verbose::ErrLog(); }
87
88 template <typename... Args>
89 inline void Verb(Args&&... args)
90 {
91 Verbose::Log log;
92 Verbose::Print(log, args...);
93 }
94
95 template <typename... Args>
96 inline void Verror(Args&&... args)
97 {
98 Verbose::ErrLog log;
99 Verbose::Print(log, args...);
100 }
101
102
103 // Manipulator tags
104 static const Verbose::LogNoEol VerbNoEOL;
105 static const Verbose::LogLock VerbLock;
106
107 #endif