Electroneum
scoped_message_writer.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2020, 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 /************************************************************************/
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:
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 
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 
89 
90  template<typename T>
91  std::ostream& operator<<(const T& val)
92  {
93  m_oss << val;
94  return m_oss;
95  }
96 
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 
132 {
133  return scoped_message_writer(epee::console_color_red, true, "Error: ", el::Level::Error);
134 }
135 
136 } // namespace tools
Definition: scoped_message_writer.h:50
bool m_flush
Definition: scoped_message_writer.h:52
el::Level m_log_level
Definition: scoped_message_writer.h:56
std::ostream & operator<<(const T &val)
Definition: scoped_message_writer.h:91
scoped_message_writer & operator=(scoped_message_writer &rhs)=delete
bool m_bright
Definition: scoped_message_writer.h:55
~scoped_message_writer()
Definition: scoped_message_writer.h:97
epee::console_colors m_color
Definition: scoped_message_writer.h:54
scoped_message_writer(scoped_message_writer &&rhs)
Definition: scoped_message_writer.h:72
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:58
std::stringstream m_oss
Definition: scoped_message_writer.h:53
const uint32_t T[512]
Definition: groestl_tables.h:37
Definition: blockchain_ancestry.cpp:73
Various Tools.
Definition: apply_permutation.h:40
scoped_message_writer fail_msg_writer()
Definition: scoped_message_writer.h:131
scoped_message_writer msg_writer(epee::console_colors color=epee::console_color_default)
Definition: scoped_message_writer.h:126
scoped_message_writer success_msg_writer(bool color=true)
Definition: scoped_message_writer.h:121
#define PAUSE_READLINE()
Definition: scoped_message_writer.h:40
#define true
Definition: stdbool.h:37