Merge pull request #5909

8933955 epee: misc_log_ex.h can now be used in C code (moneromooo-monero)
This commit is contained in:
luigi1111 2019-09-24 10:57:55 -05:00
commit cb6f96b9d1
No known key found for this signature in database
GPG key ID: F4ACA0183641E010
2 changed files with 51 additions and 11 deletions

View file

@ -28,6 +28,8 @@
#ifndef _MISC_LOG_EX_H_ #ifndef _MISC_LOG_EX_H_
#define _MISC_LOG_EX_H_ #define _MISC_LOG_EX_H_
#ifdef __cplusplus
#include <string> #include <string>
#include "easylogging++.h" #include "easylogging++.h"
@ -220,4 +222,28 @@ void set_console_color(int color, bool bright);
void reset_console_color(); void reset_console_color();
} }
extern "C"
{
#endif
#ifdef __GNUC__
#define ATTRIBUTE_PRINTF __attribute__((format(printf, 2, 3)))
#else
#define ATTRIBUTE_PRINTF
#endif
bool merror(const char *category, const char *format, ...) ATTRIBUTE_PRINTF;
bool mwarning(const char *category, const char *format, ...) ATTRIBUTE_PRINTF;
bool minfo(const char *category, const char *format, ...) ATTRIBUTE_PRINTF;
bool mdebug(const char *category, const char *format, ...) ATTRIBUTE_PRINTF;
bool mtrace(const char *category, const char *format, ...) ATTRIBUTE_PRINTF;
#ifdef __cplusplus
}
#endif
#endif //_MISC_LOG_EX_H_ #endif //_MISC_LOG_EX_H_

View file

@ -472,40 +472,54 @@ void reset_console_color() {
} }
static void mlog(el::Level level, const char *category, const char *format, va_list ap) static bool mlog(el::Level level, const char *category, const char *format, va_list ap) noexcept
{ {
int size = 0; int size = 0;
char *p = NULL; char *p = NULL;
va_list apc; va_list apc;
bool ret = true;
/* Determine required size */ /* Determine required size */
va_copy(apc, ap); va_copy(apc, ap);
size = vsnprintf(p, size, format, apc); size = vsnprintf(p, size, format, apc);
va_end(apc); va_end(apc);
if (size < 0) if (size < 0)
return; return false;
size++; /* For '\0' */ size++; /* For '\0' */
p = (char*)malloc(size); p = (char*)malloc(size);
if (p == NULL) if (p == NULL)
return; return false;
size = vsnprintf(p, size, format, ap); size = vsnprintf(p, size, format, ap);
if (size < 0) if (size < 0)
{ {
free(p); free(p);
return; return false;
} }
try
{
MCLOG(level, category, el::Color::Default, p); MCLOG(level, category, el::Color::Default, p);
}
catch(...)
{
ret = false;
}
free(p); free(p);
return ret;
} }
void mfatal(const char *category, const char *fmt, ...) { va_list ap; va_start(ap, fmt); mlog(el::Level::Fatal, category, fmt, ap); va_end(ap); } #define DEFLOG(fun,lev) \
void merror(const char *category, const char *fmt, ...) { va_list ap; va_start(ap, fmt); mlog(el::Level::Error, category, fmt, ap); va_end(ap); } bool m##fun(const char *category, const char *fmt, ...) { va_list ap; va_start(ap, fmt); bool ret = mlog(el::Level::lev, category, fmt, ap); va_end(ap); return ret; }
void mwarning(const char *category, const char *fmt, ...) { va_list ap; va_start(ap, fmt); mlog(el::Level::Warning, category, fmt, ap); va_end(ap); }
void minfo(const char *category, const char *fmt, ...) { va_list ap; va_start(ap, fmt); mlog(el::Level::Info, category, fmt, ap); va_end(ap); } DEFLOG(error, Error)
void mdebug(const char *category, const char *fmt, ...) { va_list ap; va_start(ap, fmt); mlog(el::Level::Debug, category, fmt, ap); va_end(ap); } DEFLOG(warning, Warning)
void mtrace(const char *category, const char *fmt, ...) { va_list ap; va_start(ap, fmt); mlog(el::Level::Trace, category, fmt, ap); va_end(ap); } DEFLOG(info, Info)
DEFLOG(debug, Debug)
DEFLOG(trace, Trace)
#undef DEFLOG
#endif //_MLOG_H_ #endif //_MLOG_H_