| 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 |
#ifndef _MISC_LOG_EX_H_ |
| 29 |
#define _MISC_LOG_EX_H_ |
| 30 |
|
| 31 |
#ifdef __cplusplus |
| 32 |
|
| 33 |
#include <sstream> |
| 34 |
#include <string> |
| 35 |
|
| 36 |
#include "easylogging++.h" |
| 37 |
|
| 38 |
#undef MONERO_DEFAULT_LOG_CATEGORY |
| 39 |
#define MONERO_DEFAULT_LOG_CATEGORY "default" |
| 40 |
|
| 41 |
#define MAX_LOG_FILE_SIZE 104850000 // 100 MB - 7600 bytes |
| 42 |
#define MAX_LOG_FILES 50 |
| 43 |
|
| 44 |
#define LOG_TO_STRING(x) \ |
| 45 |
std::stringstream ss; \ |
| 46 |
ss << x; \ |
| 47 |
const std::string str = ss.str(); |
| 48 |
|
| 49 |
#define MCLOG_TYPE(level, cat, color, type, x) do { \ |
| 50 |
if (el::Loggers::allowed(level, cat)) { \ |
| 51 |
LOG_TO_STRING(x); \ |
| 52 |
el::base::Writer(level, color, __FILE__, __LINE__, ELPP_FUNC, type).construct(cat) << str; \ |
| 53 |
} \ |
| 54 |
} while (0) |
| 55 |
|
| 56 |
#define MCLOG(level, cat, color, x) MCLOG_TYPE(level, cat, color, el::base::DispatchAction::NormalLog, x) |
| 57 |
#define MCLOG_FILE(level, cat, x) MCLOG_TYPE(level, cat, el::Color::Default, el::base::DispatchAction::FileOnlyLog, x) |
| 58 |
|
| 59 |
#define MCFATAL(cat,x) MCLOG(el::Level::Fatal,cat, el::Color::Default, x) |
| 60 |
#define MCERROR(cat,x) MCLOG(el::Level::Error,cat, el::Color::Default, x) |
| 61 |
#define MCWARNING(cat,x) MCLOG(el::Level::Warning,cat, el::Color::Default, x) |
| 62 |
#define MCINFO(cat,x) MCLOG(el::Level::Info,cat, el::Color::Default, x) |
| 63 |
#define MCDEBUG(cat,x) MCLOG(el::Level::Debug,cat, el::Color::Default, x) |
| 64 |
#define MCTRACE(cat,x) MCLOG(el::Level::Trace,cat, el::Color::Default, x) |
| 65 |
|
| 66 |
#define MCLOG_COLOR(level,cat,color,x) MCLOG(level,cat,color,x) |
| 67 |
#define MCLOG_RED(level,cat,x) MCLOG_COLOR(level,cat,el::Color::Red,x) |
| 68 |
#define MCLOG_GREEN(level,cat,x) MCLOG_COLOR(level,cat,el::Color::Green,x) |
| 69 |
#define MCLOG_YELLOW(level,cat,x) MCLOG_COLOR(level,cat,el::Color::Yellow,x) |
| 70 |
#define MCLOG_BLUE(level,cat,x) MCLOG_COLOR(level,cat,el::Color::Blue,x) |
| 71 |
#define MCLOG_MAGENTA(level,cat,x) MCLOG_COLOR(level,cat,el::Color::Magenta,x) |
| 72 |
#define MCLOG_CYAN(level,cat,x) MCLOG_COLOR(level,cat,el::Color::Cyan,x) |
| 73 |
|
| 74 |
#define MLOG_RED(level,x) MCLOG_RED(level,MONERO_DEFAULT_LOG_CATEGORY,x) |
| 75 |
#define MLOG_GREEN(level,x) MCLOG_GREEN(level,MONERO_DEFAULT_LOG_CATEGORY,x) |
| 76 |
#define MLOG_YELLOW(level,x) MCLOG_YELLOW(level,MONERO_DEFAULT_LOG_CATEGORY,x) |
| 77 |
#define MLOG_BLUE(level,x) MCLOG_BLUE(level,MONERO_DEFAULT_LOG_CATEGORY,x) |
| 78 |
#define MLOG_MAGENTA(level,x) MCLOG_MAGENTA(level,MONERO_DEFAULT_LOG_CATEGORY,x) |
| 79 |
#define MLOG_CYAN(level,x) MCLOG_CYAN(level,MONERO_DEFAULT_LOG_CATEGORY,x) |
| 80 |
|
| 81 |
#define MFATAL(x) MCFATAL(MONERO_DEFAULT_LOG_CATEGORY,x) |
| 82 |
#define MERROR(x) MCERROR(MONERO_DEFAULT_LOG_CATEGORY,x) |
| 83 |
#define MWARNING(x) MCWARNING(MONERO_DEFAULT_LOG_CATEGORY,x) |
| 84 |
#define MINFO(x) MCINFO(MONERO_DEFAULT_LOG_CATEGORY,x) |
| 85 |
#define MDEBUG(x) MCDEBUG(MONERO_DEFAULT_LOG_CATEGORY,x) |
| 86 |
#define MTRACE(x) MCTRACE(MONERO_DEFAULT_LOG_CATEGORY,x) |
| 87 |
#define MLOG(level,x) MCLOG(level,MONERO_DEFAULT_LOG_CATEGORY,el::Color::Default,x) |
| 88 |
|
| 89 |
#define MGINFO(x) MCINFO("global",x) |
| 90 |
#define MGINFO_RED(x) MCLOG_RED(el::Level::Info, "global",x) |
| 91 |
#define MGINFO_GREEN(x) MCLOG_GREEN(el::Level::Info, "global",x) |
| 92 |
#define MGINFO_YELLOW(x) MCLOG_YELLOW(el::Level::Info, "global",x) |
| 93 |
#define MGINFO_BLUE(x) MCLOG_BLUE(el::Level::Info, "global",x) |
| 94 |
#define MGINFO_MAGENTA(x) MCLOG_MAGENTA(el::Level::Info, "global",x) |
| 95 |
#define MGINFO_CYAN(x) MCLOG_CYAN(el::Level::Info, "global",x) |
| 96 |
|
| 97 |
#define IFLOG(level, cat, color, type, init, x) \ |
| 98 |
do { \ |
| 99 |
if (el::Loggers::allowed(level, cat)) { \ |
| 100 |
init; \ |
| 101 |
LOG_TO_STRING(x); \ |
| 102 |
el::base::Writer(level, color, __FILE__, __LINE__, ELPP_FUNC, type).construct(cat) << str; \ |
| 103 |
} \ |
| 104 |
} while(0) |
| 105 |
#define MIDEBUG(init, x) IFLOG(el::Level::Debug, MONERO_DEFAULT_LOG_CATEGORY, el::Color::Default, el::base::DispatchAction::NormalLog, init, x) |
| 106 |
|
| 107 |
|
| 108 |
#define LOG_ERROR(x) MERROR(x) |
| 109 |
#define LOG_PRINT_L0(x) MWARNING(x) |
| 110 |
#define LOG_PRINT_L1(x) MINFO(x) |
| 111 |
#define LOG_PRINT_L2(x) MDEBUG(x) |
| 112 |
#define LOG_PRINT_L3(x) MTRACE(x) |
| 113 |
#define LOG_PRINT_L4(x) MTRACE(x) |
| 114 |
|
| 115 |
#define _dbg3(x) MTRACE(x) |
| 116 |
#define _dbg2(x) MDEBUG(x) |
| 117 |
#define _dbg1(x) MDEBUG(x) |
| 118 |
#define _info(x) MINFO(x) |
| 119 |
#define _note(x) MDEBUG(x) |
| 120 |
#define _fact(x) MDEBUG(x) |
| 121 |
#define _mark(x) MDEBUG(x) |
| 122 |
#define _warn(x) MWARNING(x) |
| 123 |
#define _erro(x) MERROR(x) |
| 124 |
|
| 125 |
#define MLOG_SET_THREAD_NAME(x) el::Helpers::setThreadName(x) |
| 126 |
|
| 127 |
#ifndef LOCAL_ASSERT |
| 128 |
#include <assert.h> |
| 129 |
#if (defined _MSC_VER) |
| 130 |
#define LOCAL_ASSERT(expr) {if(epee::debug::get_set_enable_assert()){_ASSERTE(expr);}} |
| 131 |
#else |
| 132 |
#define LOCAL_ASSERT(expr) |
| 133 |
#endif |
| 134 |
|
| 135 |
#endif |
| 136 |
|
| 137 |
std::string mlog_get_default_log_path(const char *default_filename); |
| 138 |
void mlog_configure(const std::string &filename_base, bool console, const std::size_t max_log_file_size = MAX_LOG_FILE_SIZE, const std::size_t max_log_files = MAX_LOG_FILES); |
| 139 |
void mlog_set_categories(const char *categories); |
| 140 |
std::string mlog_get_categories(); |
| 141 |
void mlog_set_log_level(int level); |
| 142 |
void mlog_set_log(const char *log); |
| 143 |
|
| 144 |
namespace epee |
| 145 |
{ |
| 146 |
namespace debug |
| 147 |
{ |
| 148 |
inline bool get_set_enable_assert(bool set = false, bool v = false) |
| 149 |
{ |
| 150 |
static bool e = true; |
| 151 |
if(set) |
| 152 |
e = v; |
| 153 |
return e; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
#define ENDL std::endl |
| 160 |
|
| 161 |
#define TRY_ENTRY() try { |
| 162 |
#define CATCH_ENTRY(location, return_val) } \ |
| 163 |
catch(const std::exception& ex) \ |
| 164 |
{ \ |
| 165 |
(void)(ex); \ |
| 166 |
LOG_ERROR("Exception at [" << location << "], what=" << ex.what()); \ |
| 167 |
return return_val; \ |
| 168 |
}\ |
| 169 |
catch(...)\ |
| 170 |
{\ |
| 171 |
LOG_ERROR("Exception at [" << location << "], generic exception \"...\"");\ |
| 172 |
return return_val; \ |
| 173 |
} |
| 174 |
|
| 175 |
#define CATCH_ENTRY_SWALLOW_EX(location) } \ |
| 176 |
catch(const std::exception& ex) \ |
| 177 |
{ \ |
| 178 |
(void)(ex); \ |
| 179 |
LOG_ERROR("Exception at [" << location << "], what=" << ex.what()); \ |
| 180 |
}\ |
| 181 |
catch(...)\ |
| 182 |
{\ |
| 183 |
LOG_ERROR("Exception at [" << location << "], generic exception \"...\"");\ |
| 184 |
} |
| 185 |
|
| 186 |
#define CATCH_ENTRY_L0(lacation, return_val) CATCH_ENTRY(lacation, return_val) |
| 187 |
#define CATCH_ENTRY_L1(lacation, return_val) CATCH_ENTRY(lacation, return_val) |
| 188 |
#define CATCH_ENTRY_L2(lacation, return_val) CATCH_ENTRY(lacation, return_val) |
| 189 |
#define CATCH_ENTRY_L3(lacation, return_val) CATCH_ENTRY(lacation, return_val) |
| 190 |
#define CATCH_ENTRY_L4(lacation, return_val) CATCH_ENTRY(lacation, return_val) |
| 191 |
|
| 192 |
|
| 193 |
#define ASSERT_MES_AND_THROW(message) {LOG_ERROR(message); std::stringstream ss; ss << message; throw std::runtime_error(ss.str());} |
| 194 |
#define CHECK_AND_ASSERT_THROW_MES(expr, message) do {if(!(expr)) ASSERT_MES_AND_THROW(message);} while(0) |
| 195 |
|
| 196 |
|
| 197 |
#ifndef CHECK_AND_ASSERT |
| 198 |
#define CHECK_AND_ASSERT(expr, fail_ret_val) do{if(!(expr)){LOCAL_ASSERT(expr); return fail_ret_val;};}while(0) |
| 199 |
#endif |
| 200 |
|
| 201 |
#ifndef CHECK_AND_ASSERT_MES |
| 202 |
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message) do{if(!(expr)) {LOG_ERROR(message); return fail_ret_val;};}while(0) |
| 203 |
#endif |
| 204 |
|
| 205 |
#ifndef CHECK_AND_NO_ASSERT_MES_L |
| 206 |
#define CHECK_AND_NO_ASSERT_MES_L(expr, fail_ret_val, l, message) do{if(!(expr)) {LOG_PRINT_L##l(message); return fail_ret_val;};}while(0) |
| 207 |
#endif |
| 208 |
|
| 209 |
#ifndef CHECK_AND_NO_ASSERT_MES |
| 210 |
#define CHECK_AND_NO_ASSERT_MES(expr, fail_ret_val, message) CHECK_AND_NO_ASSERT_MES_L(expr, fail_ret_val, 0, message) |
| 211 |
#endif |
| 212 |
|
| 213 |
#ifndef CHECK_AND_NO_ASSERT_MES_L1 |
| 214 |
#define CHECK_AND_NO_ASSERT_MES_L1(expr, fail_ret_val, message) CHECK_AND_NO_ASSERT_MES_L(expr, fail_ret_val, 1, message) |
| 215 |
#endif |
| 216 |
|
| 217 |
|
| 218 |
#ifndef CHECK_AND_ASSERT_MES_NO_RET |
| 219 |
#define CHECK_AND_ASSERT_MES_NO_RET(expr, message) do{if(!(expr)) {LOG_ERROR(message); return;};}while(0) |
| 220 |
#endif |
| 221 |
|
| 222 |
|
| 223 |
#ifndef CHECK_AND_ASSERT_MES2 |
| 224 |
#define CHECK_AND_ASSERT_MES2(expr, message) do{if(!(expr)) {LOG_ERROR(message); };}while(0) |
| 225 |
#endif |
| 226 |
|
| 227 |
enum console_colors |
| 228 |
{ |
| 229 |
console_color_default, |
| 230 |
console_color_white, |
| 231 |
console_color_red, |
| 232 |
console_color_green, |
| 233 |
console_color_blue, |
| 234 |
console_color_cyan, |
| 235 |
console_color_magenta, |
| 236 |
console_color_yellow |
| 237 |
}; |
| 238 |
|
| 239 |
bool is_stdout_a_tty(); |
| 240 |
void set_console_color(int color, bool bright); |
| 241 |
void reset_console_color(); |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
extern "C" |
| 246 |
{ |
| 247 |
|
| 248 |
#endif |
| 249 |
|
| 250 |
#ifdef __GNUC__ |
| 251 |
#define ATTRIBUTE_PRINTF __attribute__((format(printf, 2, 3))) |
| 252 |
#else |
| 253 |
#define ATTRIBUTE_PRINTF |
| 254 |
#endif |
| 255 |
|
| 256 |
bool merror(const char *category, const char *format, ...) ATTRIBUTE_PRINTF; |
| 257 |
bool mwarning(const char *category, const char *format, ...) ATTRIBUTE_PRINTF; |
| 258 |
bool minfo(const char *category, const char *format, ...) ATTRIBUTE_PRINTF; |
| 259 |
bool mdebug(const char *category, const char *format, ...) ATTRIBUTE_PRINTF; |
| 260 |
bool mtrace(const char *category, const char *format, ...) ATTRIBUTE_PRINTF; |
| 261 |
|
| 262 |
#ifdef __cplusplus |
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
#endif |
| 267 |
|
| 268 |
#endif |