| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 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 |
|
| 48 |
class scoped_message_writer |
| 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: |
| 57 |
scoped_message_writer( |
| 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 |
|
| 71 |
scoped_message_writer(scoped_message_writer&& rhs) |
| 72 |
: m_flush(std::move(rhs.m_flush)) |
| 73 |
#if defined(_MSC_VER) |
| 74 |
, m_oss(std::move(rhs.m_oss)) |
| 75 |
#else |
| 76 |
|
| 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 |
|
| 85 |
scoped_message_writer(scoped_message_writer& rhs) = delete; |
| 86 |
scoped_message_writer& operator=(scoped_message_writer& rhs) = delete; |
| 87 |
scoped_message_writer& operator=(scoped_message_writer&& rhs) = delete; |
| 88 |
|
| 89 |
template<typename T> |
| 90 |
std::ostream& operator<<(const T& val) |
| 91 |
{ |
| 92 |
m_oss << val; |
| 93 |
return m_oss; |
| 94 |
} |
| 95 |
|
| 96 |
~scoped_message_writer() |
| 97 |
{ |
| 98 |
if (m_flush) |
| 99 |
{ |
| 100 |
m_flush = false; |
| 101 |
|
| 102 |
MCLOG_FILE(m_log_level, "msgwriter", m_oss.str()); |
| 103 |
|
| 104 |
PAUSE_READLINE(); |
| 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(); |
| 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 |
|
| 130 |
inline scoped_message_writer fail_msg_writer() |
| 131 |
{ |
| 132 |
return scoped_message_writer(epee::console_color_red, true, "Error: ", el::Level::Error); |
| 133 |
} |
| 134 |
|
| 135 |
} |