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