mirror of
https://github.com/mhx/dwarfs.git
synced 2025-09-15 07:16:13 -04:00
Support multi-line logging
This commit is contained in:
parent
ae7be72003
commit
7615b2e827
@ -24,6 +24,8 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include <folly/Conv.h>
|
#include <folly/Conv.h>
|
||||||
|
#include <folly/String.h>
|
||||||
|
#include <folly/small_vector.h>
|
||||||
|
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
|
|
||||||
@ -126,6 +128,7 @@ void console_writer::write(level_type level, const std::string& output,
|
|||||||
auto t = get_current_time_string();
|
auto t = get_current_time_string();
|
||||||
const char* prefix = "";
|
const char* prefix = "";
|
||||||
const char* suffix = "";
|
const char* suffix = "";
|
||||||
|
const char* newline = pg_mode_ != NONE ? "\x1b[K\n" : "\n";
|
||||||
|
|
||||||
if (color_) {
|
if (color_) {
|
||||||
switch (level) {
|
switch (level) {
|
||||||
@ -146,9 +149,11 @@ void console_writer::write(level_type level, const std::string& output,
|
|||||||
|
|
||||||
char lchar = logger::level_char(level);
|
char lchar = logger::level_char(level);
|
||||||
std::string context;
|
std::string context;
|
||||||
|
size_t context_len = 0;
|
||||||
|
|
||||||
if (with_context_ && file) {
|
if (with_context_ && file) {
|
||||||
context = get_logger_context(file, line);
|
context = get_logger_context(file, line);
|
||||||
|
context_len = context.size();
|
||||||
if (color_) {
|
if (color_) {
|
||||||
context = folly::to<std::string>(
|
context = folly::to<std::string>(
|
||||||
suffix, terminal_color(termcolor::MAGENTA), context,
|
suffix, terminal_color(termcolor::MAGENTA), context,
|
||||||
@ -156,20 +161,26 @@ void console_writer::write(level_type level, const std::string& output,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
folly::small_vector<std::string_view, 2> lines;
|
||||||
|
folly::split('\n', output, lines);
|
||||||
|
|
||||||
|
if (lines.back().empty()) {
|
||||||
|
lines.pop_back();
|
||||||
|
}
|
||||||
|
|
||||||
std::lock_guard lock(mx_);
|
std::lock_guard lock(mx_);
|
||||||
|
|
||||||
switch (pg_mode_) {
|
rewind();
|
||||||
case UNICODE:
|
|
||||||
case ASCII:
|
|
||||||
rewind();
|
|
||||||
os_ << prefix << lchar << ' ' << t << ' ' << context << output << suffix
|
|
||||||
<< "\x1b[K\n";
|
|
||||||
os_ << statebuf_;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
for (auto l : lines) {
|
||||||
os_ << lchar << ' ' << t << ' ' << context << output << "\n";
|
os_ << prefix << lchar << ' ' << t << ' ' << context << l << suffix
|
||||||
break;
|
<< newline;
|
||||||
|
std::fill(t.begin(), t.end(), '.');
|
||||||
|
context.assign(context_len, ' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pg_mode_ == UNICODE || pg_mode_ == ASCII) {
|
||||||
|
os_ << statebuf_;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,8 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
#include <folly/Conv.h>
|
#include <folly/Conv.h>
|
||||||
|
#include <folly/String.h>
|
||||||
|
#include <folly/small_vector.h>
|
||||||
|
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
#include <folly/experimental/symbolizer/Symbolizer.h>
|
#include <folly/experimental/symbolizer/Symbolizer.h>
|
||||||
@ -108,9 +110,11 @@ void stream_logger::write(level_type level, const std::string& output,
|
|||||||
|
|
||||||
char lchar = logger::level_char(level);
|
char lchar = logger::level_char(level);
|
||||||
std::string context;
|
std::string context;
|
||||||
|
size_t context_len = 0;
|
||||||
|
|
||||||
if (with_context_ && file) {
|
if (with_context_ && file) {
|
||||||
context = get_logger_context(file, line);
|
context = get_logger_context(file, line);
|
||||||
|
context_len = context.size();
|
||||||
if (color_) {
|
if (color_) {
|
||||||
context = folly::to<std::string>(
|
context = folly::to<std::string>(
|
||||||
suffix, terminal_color(termcolor::MAGENTA), context,
|
suffix, terminal_color(termcolor::MAGENTA), context,
|
||||||
@ -118,9 +122,20 @@ void stream_logger::write(level_type level, const std::string& output,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
folly::small_vector<std::string_view, 2> lines;
|
||||||
|
folly::split('\n', output, lines);
|
||||||
|
|
||||||
|
if (lines.back().empty()) {
|
||||||
|
lines.pop_back();
|
||||||
|
}
|
||||||
|
|
||||||
std::lock_guard lock(mx_);
|
std::lock_guard lock(mx_);
|
||||||
os_ << prefix << lchar << ' ' << t << ' ' << context << output << suffix
|
for (auto l : lines) {
|
||||||
<< "\n";
|
os_ << prefix << lchar << ' ' << t << ' ' << context << l << suffix
|
||||||
|
<< "\n";
|
||||||
|
std::fill(t.begin(), t.end(), '.');
|
||||||
|
context.assign(context_len, ' ');
|
||||||
|
}
|
||||||
|
|
||||||
#if DWARFS_SYMBOLIZE
|
#if DWARFS_SYMBOLIZE
|
||||||
if (threshold_ == TRACE) {
|
if (threshold_ == TRACE) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user