Some formatting

This commit is contained in:
Alexey Rybalchenko 2019-08-21 12:14:44 +02:00
parent f91643b9fa
commit a42c88183e
3 changed files with 48 additions and 64 deletions

View File

@ -22,7 +22,7 @@ class ColoredSeverityWriter
public: public:
ColoredSeverityWriter(Severity severity) : fSeverity(severity) {} ColoredSeverityWriter(Severity severity) : fSeverity(severity) {}
friend std::ostream& operator<<(std::ostream& os, const ColoredSeverityWriter& w) friend ostream& operator<<(ostream& os, const ColoredSeverityWriter& w)
{ {
switch (w.fSeverity) { switch (w.fSeverity) {
case Severity::nolog: case Severity::nolog:
@ -203,100 +203,81 @@ Logger::Logger(Severity severity, Verbosity verbosity, const string& file, const
chrono::time_point<chrono::system_clock> now = chrono::system_clock::now(); chrono::time_point<chrono::system_clock> now = chrono::system_clock::now();
size_t pos = file.rfind("/"); size_t pos = file.rfind("/");
fMetaData.timestamp = chrono::system_clock::to_time_t(now); fInfos.timestamp = chrono::system_clock::to_time_t(now);
fMetaData.us = chrono::duration_cast<chrono::microseconds>(now.time_since_epoch()) % 1000000; fInfos.us = chrono::duration_cast<chrono::microseconds>(now.time_since_epoch()) % 1000000;
fMetaData.process_name = fProcessName; fInfos.process_name = fProcessName;
fMetaData.file = file.substr(pos + 1); fInfos.file = file.substr(pos + 1);
fMetaData.line = line; fInfos.line = line;
fMetaData.func = func; fInfos.func = func;
fMetaData.severity_name = fSeverityNames.at(static_cast<size_t>(severity)); fInfos.severity_name = fSeverityNames.at(static_cast<size_t>(severity));
fMetaData.severity = severity; fInfos.severity = severity;
auto spec = fVerbosities[verbosity]; auto spec = fVerbosities[verbosity];
if ((!fColored && LoggingToConsole()) || LoggingToFile()) { if ((!fColored && LoggingToConsole()) || LoggingToFile()) {
bool appendSpace = false; for (const auto info : spec.fInfos) {
for (const auto info : spec.fOrder) {
switch (info) { switch (info) {
case VerbositySpec::Info::process_name: case VerbositySpec::Info::process_name:
fBWOut << "[" << fMetaData.process_name << "]"; fBWOut << fmt::format("[{}]", fInfos.process_name);
appendSpace = true;
break; break;
case VerbositySpec::Info::timestamp_us: case VerbositySpec::Info::timestamp_us:
fBWOut << fmt::format("[{:%H:%M:%S}.{:06}]", fmt::localtime(fMetaData.timestamp), fMetaData.us.count()); fBWOut << fmt::format("[{:%H:%M:%S}.{:06}]", fmt::localtime(fInfos.timestamp), fInfos.us.count());
appendSpace = true;
break; break;
case VerbositySpec::Info::timestamp_s: case VerbositySpec::Info::timestamp_s:
fBWOut << fmt::format("[{:%H:%M:%S}]", fmt::localtime(fMetaData.timestamp)); fBWOut << fmt::format("[{:%H:%M:%S}]", fmt::localtime(fInfos.timestamp));
appendSpace = true;
break; break;
case VerbositySpec::Info::severity: case VerbositySpec::Info::severity:
fBWOut << "[" << fMetaData.severity_name << "]"; fBWOut << fmt::format("[{}]", fInfos.severity_name);
appendSpace = true;
break; break;
case VerbositySpec::Info::file_line_function: case VerbositySpec::Info::file_line_function:
fBWOut << "[" << fMetaData.file << ":" << fMetaData.line << ":" << fMetaData.func << "]"; fBWOut << fmt::format("[{}:{}:{}]", fInfos.file, fInfos.line, fInfos.func);
appendSpace = true;
break; break;
case VerbositySpec::Info::file_line: case VerbositySpec::Info::file_line:
fBWOut << "[" << fMetaData.file << ":" << fMetaData.line << "]"; fBWOut << fmt::format("[{}:{}]", fInfos.file, fInfos.line);
appendSpace = true;
break; break;
case VerbositySpec::Info::file: case VerbositySpec::Info::file:
fBWOut << "[" << fMetaData.file << "]"; fBWOut << fmt::format("[{}]", fInfos.file);
appendSpace = true;
break; break;
default: default:
break; break;
} }
} }
if (appendSpace) { if (spec.fSize > 0) {
fBWOut << " "; fBWOut << " ";
} }
} }
if (fColored && LoggingToConsole()) { if (fColored && LoggingToConsole()) {
bool appendSpace = false; for (const auto info : spec.fInfos) {
for (const auto info : spec.fOrder) {
switch (info) { switch (info) {
case VerbositySpec::Info::process_name: case VerbositySpec::Info::process_name:
fColorOut << "[" << ColorOut(Color::fgBlue, fMetaData.process_name) << "]"; fColorOut << fmt::format("[{}]", ColorOut(Color::fgBlue, fInfos.process_name));
appendSpace = true;
break; break;
case VerbositySpec::Info::timestamp_us: case VerbositySpec::Info::timestamp_us:
fColorOut << fmt::format("[{}{:%H:%M:%S}.{:06}{}]", startColor(Color::fgCyan), fmt::localtime(fMetaData.timestamp), fMetaData.us.count(), endColor()); fColorOut << fmt::format("[{}{:%H:%M:%S}.{:06}{}]", startColor(Color::fgCyan), fmt::localtime(fInfos.timestamp), fInfos.us.count(), endColor());
appendSpace = true;
break; break;
case VerbositySpec::Info::timestamp_s: case VerbositySpec::Info::timestamp_s:
fColorOut << fmt::format("[{}{:%H:%M:%S}{}]", startColor(Color::fgCyan), fmt::localtime(fMetaData.timestamp), endColor()); fColorOut << fmt::format("[{}{:%H:%M:%S}{}]", startColor(Color::fgCyan), fmt::localtime(fInfos.timestamp), endColor());
appendSpace = true;
break; break;
case VerbositySpec::Info::severity: case VerbositySpec::Info::severity:
fColorOut << "[" << ColoredSeverityWriter(fMetaData.severity) << "]"; fColorOut << fmt::format("[{}]", ColoredSeverityWriter(fInfos.severity));
appendSpace = true;
break; break;
case VerbositySpec::Info::file_line_function: case VerbositySpec::Info::file_line_function:
fColorOut << "[" << ColorOut(Color::fgBlue, fMetaData.file) << ":" fColorOut << fmt::format("[{}:{}:{}]", ColorOut(Color::fgBlue, fInfos.file), ColorOut(Color::fgYellow, fInfos.line), ColorOut(Color::fgBlue, fInfos.func));
<< ColorOut(Color::fgYellow, fMetaData.line) << ":"
<< ColorOut(Color::fgBlue, fMetaData.func) << "]";
appendSpace = true;
break; break;
case VerbositySpec::Info::file_line: case VerbositySpec::Info::file_line:
fColorOut << "[" << ColorOut(Color::fgBlue, fMetaData.file) << ":" fColorOut << fmt::format("[{}:{}]", ColorOut(Color::fgBlue, fInfos.file), ColorOut(Color::fgYellow, fInfos.line));
<< ColorOut(Color::fgYellow, fMetaData.line) << "]";
appendSpace = true;
break; break;
case VerbositySpec::Info::file: case VerbositySpec::Info::file:
fColorOut << "[" << ColorOut(Color::fgBlue, fMetaData.file) << "]"; fColorOut << fmt::format("[{}]", ColorOut(Color::fgBlue, fInfos.file));
appendSpace = true;
break; break;
default: default:
break; break;
} }
} }
if (appendSpace) { if (spec.fSize > 0) {
fColorOut << " "; fColorOut << " ";
} }
} }
@ -314,7 +295,7 @@ Logger::~Logger() noexcept(false)
for (auto& it : fCustomSinks) { for (auto& it : fCustomSinks) {
if (LoggingCustom(it.second.first)) { if (LoggingCustom(it.second.first)) {
lock_guard<mutex> lock(fMtx); lock_guard<mutex> lock(fMtx);
it.second.second(fContent.str(), fMetaData); it.second.second(fContent.str(), fInfos);
} }
} }
@ -338,7 +319,7 @@ Logger::~Logger() noexcept(false)
} }
} }
if (fMetaData.severity == Severity::fatal) { if (fInfos.severity == Severity::fatal) {
if (fFatalCallback) { if (fFatalCallback) {
fFatalCallback(); fFatalCallback();
} }
@ -605,23 +586,23 @@ void Logger::RemoveFileSink()
bool Logger::LoggingToConsole() const bool Logger::LoggingToConsole() const
{ {
return (fMetaData.severity <= fConsoleSeverity && return (fInfos.severity <= fConsoleSeverity &&
fMetaData.severity > Severity::nolog) || fInfos.severity > Severity::nolog) ||
fMetaData.severity == Severity::fatal; fInfos.severity == Severity::fatal;
} }
bool Logger::LoggingToFile() const bool Logger::LoggingToFile() const
{ {
return (fMetaData.severity <= fFileSeverity && return (fInfos.severity <= fFileSeverity &&
fMetaData.severity > Severity::nolog) || fInfos.severity > Severity::nolog) ||
fMetaData.severity == Severity::fatal; fInfos.severity == Severity::fatal;
} }
bool Logger::LoggingCustom(const Severity severity) const bool Logger::LoggingCustom(const Severity severity) const
{ {
return (fMetaData.severity <= severity && return (fInfos.severity <= severity &&
fMetaData.severity > Severity::nolog) || fInfos.severity > Severity::nolog) ||
fMetaData.severity == Severity::fatal; fInfos.severity == Severity::fatal;
} }
void Logger::OnFatal(function<void()> func) void Logger::OnFatal(function<void()> func)

View File

@ -116,9 +116,10 @@ struct VerbositySpec
__max__ // needs to be last in enum __max__ // needs to be last in enum
}; };
std::array<Info, static_cast<int>(Info::__max__)> fOrder; std::array<Info, static_cast<int>(Info::__max__)> fInfos;
int fSize;
VerbositySpec() : fOrder({Info::__empty__}) {} VerbositySpec() : fInfos({Info::__empty__}), fSize(0) {}
template<typename ... Ts> template<typename ... Ts>
static VerbositySpec Make(Ts ... options) static VerbositySpec Make(Ts ... options)
@ -136,11 +137,13 @@ struct VerbositySpec
assert(option > Info::__empty__); assert(option > Info::__empty__);
assert(option < Info::__max__); assert(option < Info::__max__);
if (std::find(spec.fOrder.begin(), spec.fOrder.end(), option) == spec.fOrder.end()) { if (std::find(spec.fInfos.begin(), spec.fInfos.end(), option) == spec.fInfos.end()) {
spec.fOrder[i] = option; spec.fInfos[i] = option;
++i; ++i;
} }
spec.fSize = i;
return Make(spec, i, options ...); return Make(spec, i, options ...);
} }
@ -337,7 +340,7 @@ class Logger
static struct DestructionHelper { ~DestructionHelper() { Logger::fIsDestructed = true; }} fDestructionHelper; static struct DestructionHelper { ~DestructionHelper() { Logger::fIsDestructed = true; }} fDestructionHelper;
private: private:
LogMetaData fMetaData; LogMetaData fInfos;
std::ostringstream fContent; std::ostringstream fContent;
std::ostringstream fColorOut; std::ostringstream fColorOut;

View File

@ -134,8 +134,8 @@ int main()
cout << "cout: is logging fatal: " << fair::Logger::Logging(Severity::fatal) << endl; cout << "cout: is logging fatal: " << fair::Logger::Logging(Severity::fatal) << endl;
cout << "cout: is logging nolog: " << fair::Logger::Logging(Severity::nolog) << endl; cout << "cout: is logging nolog: " << fair::Logger::Logging(Severity::nolog) << endl;
for (int i = 0; i < 1000000; ++i) { for (int i = 0; i < 100000; ++i) {
silentlyPrintAllVerbositiesWithSeverity(Severity::nolog); silentlyPrintAllVerbositiesWithSeverity(Severity::trace);
} }
cout << endl; cout << endl;
cout << "cout: setting severity to 'trace' and verbosity to 'veryhigh'" << endl; cout << "cout: setting severity to 'trace' and verbosity to 'veryhigh'" << endl;