Electroneum
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 /************************************************************************/
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:
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();
115  }
116  std::cout << std::endl;
117  }
118  }
119 };
120 
121 inline scoped_message_writer success_msg_writer(bool color = true)
122 {
124 }
125 
127 {
128  return scoped_message_writer(color, false, std::string(), el::Level::Info);
129 }
130 
132 {
134 }
135 
136 } // namespace tools
const uint32_t T[512]
Information representing errors in application but application will keep running. ...
::std::string string
Definition: gtest-port.h:1097
std::ostream & operator<<(const T &val)
STL namespace.
Level
Represents enumeration for severity level used to determine level of logging.
void reset_console_color()
Definition: mlog.cpp:460
return true
scoped_message_writer & operator=(scoped_message_writer &rhs)=delete
Various Tools.
Definition: tools.cpp:31
void set_console_color(int color, bool bright)
Definition: mlog.cpp:341
scoped_message_writer success_msg_writer(bool color=true)
scoped_message_writer fail_msg_writer()
Mainly useful to represent current progress of application.
const T & move(const T &t)
Definition: gtest-port.h:1317
#define PAUSE_READLINE()
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)
#define MCLOG_FILE(level, cat, x)
Definition: misc_log_ex.h:48
console_colors
Definition: misc_log_ex.h:206
else if(0==res)
scoped_message_writer msg_writer(epee::console_colors color=epee::console_color_default)
scoped_message_writer(scoped_message_writer &&rhs)