Electroneum
Toggle main menu visibility
Loading...
Searching...
No Matches
scoped_message_writer.h
Go to the documentation of this file.
1
// Copyrights(c) 2017-2021, The Electroneum Project
2
// Copyrights(c) 2014-2019, The Monero Project
3
//
4
// All rights reserved.
5
//
6
// Redistribution and use in source and binary forms, with or without modification, are
7
// permitted provided that the following conditions are met:
8
//
9
// 1. Redistributions of source code must retain the above copyright notice, this list of
10
// conditions and the following disclaimer.
11
//
12
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13
// of conditions and the following disclaimer in the documentation and/or other
14
// materials provided with the distribution.
15
//
16
// 3. Neither the name of the copyright holder nor the names of its contributors may be
17
// used to endorse or promote products derived from this software without specific
18
// prior written permission.
19
//
20
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30
#pragma once
31
32
#include "
misc_log_ex.h
"
33
#include <iostream>
34
35
#ifdef HAVE_READLINE
36
#include "
readline_buffer.h
"
37
#define PAUSE_READLINE() \
38
rdln::suspend_readline pause_readline;
39
#else
40
#define PAUSE_READLINE()
41
#endif
42
43
namespace
tools
44
{
45
46
/************************************************************************/
47
/* */
48
/************************************************************************/
49
class
scoped_message_writer
50
{
51
private
:
52
bool
m_flush;
53
std::stringstream m_oss;
54
epee::console_colors
m_color;
55
bool
m_bright;
56
el::Level
m_log_level;
57
public
:
58
scoped_message_writer
(
59
epee::console_colors
color =
epee::console_color_default
60
,
bool
bright =
false
61
, std::string&& prefix = std::string()
62
,
el::Level
log_level =
el::Level::Info
63
)
64
: m_flush(
true
)
65
, m_color(color)
66
, m_bright(bright)
67
, m_log_level(log_level)
68
{
69
m_oss << prefix;
70
}
71
72
scoped_message_writer
(
scoped_message_writer
&& rhs)
73
: m_flush(
std
::move(rhs.m_flush))
74
#
if
defined(_MSC_VER)
75
, m_oss(
std
::move(rhs.m_oss))
76
#else
77
// GCC bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54316
78
, m_oss(rhs.m_oss.str(),
std
::ios_base::out |
std
::ios_base::ate)
79
#endif
80
, m_color(
std
::move(rhs.m_color))
81
, m_log_level(
std
::move(rhs.m_log_level))
82
{
83
rhs.m_flush =
false
;
84
}
85
86
scoped_message_writer
(
scoped_message_writer
& rhs) =
delete
;
87
scoped_message_writer
&
operator=
(
scoped_message_writer
& rhs) =
delete
;
88
scoped_message_writer
&
operator=
(
scoped_message_writer
&& rhs) =
delete
;
89
90
template
<
typename
T>
91
std::ostream&
operator<<
(
const
T
& val)
92
{
93
m_oss << val;
94
return
m_oss;
95
}
96
97
~scoped_message_writer
()
98
{
99
if
(m_flush)
100
{
101
m_flush =
false
;
102
103
MCLOG_FILE
(m_log_level,
"msgwriter"
, m_oss.str());
104
105
PAUSE_READLINE
();
106
if
(
epee::console_color_default
== m_color)
107
{
108
std::cout << m_oss.str();
109
}
110
else
111
{
112
set_console_color(m_color, m_bright);
113
std::cout << m_oss.str();
114
epee::reset_console_color
();
115
}
116
std::cout << std::endl;
117
}
118
}
119
};
120
121
inline
scoped_message_writer
success_msg_writer
(
bool
color =
true
)
122
{
123
return
scoped_message_writer
(color ?
epee::console_color_green
:
epee::console_color_default
,
false
, std::string(),
el::Level::Info
);
124
}
125
126
inline
scoped_message_writer
msg_writer
(
epee::console_colors
color =
epee::console_color_default
)
127
{
128
return
scoped_message_writer
(color,
false
, std::string(),
el::Level::Info
);
129
}
130
131
inline
scoped_message_writer
fail_msg_writer
()
132
{
133
return
scoped_message_writer
(
epee::console_color_red
,
true
,
"Error: "
,
el::Level::Error
);
134
}
135
136
}
// namespace tools
if
else if(0==res)
Definition
abstract_tcp_server_cp.h:265
tools::scoped_message_writer
Definition
scoped_message_writer.h:50
tools::scoped_message_writer::operator=
scoped_message_writer & operator=(scoped_message_writer &rhs)=delete
tools::scoped_message_writer::operator<<
std::ostream & operator<<(const T &val)
Definition
scoped_message_writer.h:91
tools::scoped_message_writer::~scoped_message_writer
~scoped_message_writer()
Definition
scoped_message_writer.h:97
tools::scoped_message_writer::scoped_message_writer
scoped_message_writer(scoped_message_writer &&rhs)
Definition
scoped_message_writer.h:72
tools::scoped_message_writer::scoped_message_writer
scoped_message_writer(scoped_message_writer &rhs)=delete
tools::scoped_message_writer::operator=
scoped_message_writer & operator=(scoped_message_writer &&rhs)=delete
tools::scoped_message_writer::scoped_message_writer
scoped_message_writer(epee::console_colors color=epee::console_color_default, bool bright=false, std::string &&prefix=std::string(), el::Level log_level=el::Level::Info)
Definition
scoped_message_writer.h:58
misc_log_ex.h
MCLOG_FILE
#define MCLOG_FILE(level, cat, x)
Definition
misc_log_ex.h:48
el::Level
Level
Represents enumeration for severity level used to determine level of logging.
Definition
easylogging++.h:585
el::Level::Info
@ Info
Mainly useful to represent current progress of application.
Definition
easylogging++.h:601
el::Level::Error
@ Error
Information representing errors in application but application will keep running.
Definition
easylogging++.h:595
epee::reset_console_color
void reset_console_color()
Definition
mlog.cpp:460
epee::console_colors
console_colors
Definition
misc_log_ex.h:207
epee::console_color_red
@ console_color_red
Definition
misc_log_ex.h:210
epee::console_color_default
@ console_color_default
Definition
misc_log_ex.h:208
epee::console_color_green
@ console_color_green
Definition
misc_log_ex.h:211
std
STL namespace.
tools
Various Tools.
Definition
tools.cpp:31
tools::fail_msg_writer
scoped_message_writer fail_msg_writer()
Definition
scoped_message_writer.h:131
tools::msg_writer
scoped_message_writer msg_writer(epee::console_colors color=epee::console_color_default)
Definition
scoped_message_writer.h:126
tools::success_msg_writer
scoped_message_writer success_msg_writer(bool color=true)
Definition
scoped_message_writer.h:121
readline_buffer.h
PAUSE_READLINE
#define PAUSE_READLINE()
Definition
scoped_message_writer.h:40
true
#define true
T
#define T(x)
src
common
scoped_message_writer.h
Generated on
for Electroneum by
1.17.0