Monero
scoped_message_writer.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2018, 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 
42 namespace tools
43 {
44 
45 /************************************************************************/
46 /* */
47 /************************************************************************/
49 {
50 private:
51  bool m_flush;
52  std::stringstream m_oss;
53  epee::console_colors m_color;
54  bool m_bright;
55  el::Level m_log_level;
56 public:
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 
104  if (epee::console_color_default == m_color)
105  {
106  std::cout << m_oss.str();
107  }
108  else
109  {
110  PAUSE_READLINE();
111  set_console_color(m_color, m_bright);
112  std::cout << m_oss.str();
113  epee::reset_console_color();
114  }
115  std::cout << std::endl;
116  }
117  }
118 };
119 
120 inline scoped_message_writer success_msg_writer(bool color = true)
121 {
122  return scoped_message_writer(color ? epee::console_color_green : epee::console_color_default, false, std::string(), el::Level::Info);
123 }
124 
125 inline 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
const uint32_t T[512]
Definition: groestl_tables.h:33
std::ostream & operator<<(const T &val)
Definition: scoped_message_writer.h:90
Definition: blockchain_ancestry.cpp:70
scoped_message_writer & operator=(scoped_message_writer &rhs)=delete
epee::console_colors m_color
Definition: scoped_message_writer.h:53
Various Tools.
Definition: apply_permutation.h:39
bool m_bright
Definition: scoped_message_writer.h:54
Definition: scoped_message_writer.h:48
scoped_message_writer success_msg_writer(bool color=true)
Definition: scoped_message_writer.h:120
scoped_message_writer fail_msg_writer()
Definition: scoped_message_writer.h:130
#define PAUSE_READLINE()
Definition: scoped_message_writer.h:39
std::stringstream m_oss
Definition: scoped_message_writer.h:52
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
scoped_message_writer msg_writer(epee::console_colors color=epee::console_color_default)
Definition: scoped_message_writer.h:125
el::Level m_log_level
Definition: scoped_message_writer.h:55
scoped_message_writer(scoped_message_writer &&rhs)
Definition: scoped_message_writer.h:71
#define true
Definition: stdbool.h:36
~scoped_message_writer()
Definition: scoped_message_writer.h:96
bool m_flush
Definition: scoped_message_writer.h:51