test: more for logging.

This commit is contained in:
Leo Yuriev 2017-04-21 18:34:43 +03:00
parent 2523170806
commit 101e015d2c
2 changed files with 84 additions and 39 deletions

View File

@ -55,8 +55,12 @@ const char *level2str(const loglevel level) {
switch (level) { switch (level) {
default: default:
return "invalid/unknown"; return "invalid/unknown";
case extra:
return "extra";
case trace: case trace:
return "trace"; return "trace";
case verbose:
return "verbose";
case info: case info:
return "info"; return "info";
case notice: case notice:
@ -70,48 +74,74 @@ const char *level2str(const loglevel level) {
} }
} }
void output(loglevel priority, const char *format, va_list ap) { bool output(loglevel priority, const char *format, ...) {
if (priority >= level) { if (priority < level)
last = (priority >= error) ? stderr : stdout; return false;
fprintf(last, "[ %u %-10s %6s ] %s" /* TODO */, osal_getpid(),
prefix.c_str(), level2str(priority), suffix.c_str());
vfprintf(last, format, ap);
size_t len = strlen(format); va_list ap;
char end = len ? format[len - 1] : '\0'; va_start(ap, format);
switch (end) { output(priority, format, ap);
default: va_end(ap);
putc('\n', last); return true;
case '\n':
if (priority > info)
fflushall();
break;
case ' ':
case '_':
case ':':
case '|':
case ',':
case '\t':
case '\b':
case '\r':
case '\0':
return;
}
}
last = nullptr;
} }
void feed(const char *format, ...) { bool output(loglevel priority, const char *format, va_list ap) {
if (last) { if (last) {
va_list ap; putc('\n', last);
va_start(ap, format); last = nullptr;
vfprintf(last, format, ap);
va_end(ap);
size_t len = strlen(format);
if (len && format[len - 1] == '\n')
last = nullptr;
} }
if (priority < level)
return false;
last = (priority >= error) ? stderr : stdout;
fprintf(last, "[ %u%10s %.4s ] %s" /* TODO */, osal_getpid(), prefix.c_str(),
level2str(priority), suffix.c_str());
vfprintf(last, format, ap);
size_t len = strlen(format);
char end = len ? format[len - 1] : '\0';
switch (end) {
default:
putc('\n', last);
case '\n':
if (priority > info)
fflushall();
last = nullptr;
case ' ':
case '_':
case ':':
case '|':
case ',':
case '\t':
case '\b':
case '\r':
case '\0':
break;
}
return true;
}
bool feed(const char *format, va_list ap) {
if (!last)
return false;
vfprintf(last, format, ap);
size_t len = strlen(format);
if (len && format[len - 1] == '\n')
last = nullptr;
return true;
}
bool feed(const char *format, ...) {
if (!last)
return false;
va_list ap;
va_start(ap, format);
feed(format, ap);
va_end(ap);
return true;
} }
local_suffix::local_suffix(const char *c_str) local_suffix::local_suffix(const char *c_str)
@ -151,6 +181,16 @@ void log_trace(const char *msg, ...) {
logging::last = nullptr; logging::last = nullptr;
} }
void log_verbose(const char *msg, ...) {
if (logging::verbose >= logging::level) {
va_list ap;
va_start(ap, msg);
logging::output(logging::verbose, msg, ap);
va_end(ap);
} else
logging::last = nullptr;
}
void log_info(const char *msg, ...) { void log_info(const char *msg, ...) {
if (logging::info >= logging::level) { if (logging::info >= logging::level) {
va_list ap; va_list ap;

View File

@ -33,7 +33,9 @@ const char *test_strerror(int errnum);
namespace logging { namespace logging {
enum loglevel { enum loglevel {
extra,
trace, trace,
verbose,
info, info,
notice, notice,
warning, warning,
@ -45,8 +47,10 @@ const char *level2str(const loglevel level);
void setup(loglevel level, const std::string &prefix); void setup(loglevel level, const std::string &prefix);
void setup(const std::string &prefix); void setup(const std::string &prefix);
void output(loglevel priority, const char *format, va_list ap); bool output(loglevel priority, const char *format, va_list ap);
void __printf_args(1, 2) feed(const char *format, ...); bool __printf_args(2, 3) output(loglevel priority, const char *format, ...);
bool feed(const char *format, va_list ap);
bool __printf_args(1, 2) feed(const char *format, ...);
class local_suffix { class local_suffix {
protected: protected:
@ -68,6 +72,7 @@ public:
} /* namespace log */ } /* namespace log */
void __printf_args(1, 2) log_trace(const char *msg, ...); void __printf_args(1, 2) log_trace(const char *msg, ...);
void __printf_args(1, 2) log_verbose(const char *msg, ...);
void __printf_args(1, 2) log_info(const char *msg, ...); void __printf_args(1, 2) log_info(const char *msg, ...);
void __printf_args(1, 2) log_notice(const char *msg, ...); void __printf_args(1, 2) log_notice(const char *msg, ...);
void __printf_args(1, 2) log_warning(const char *msg, ...); void __printf_args(1, 2) log_warning(const char *msg, ...);