Refactor logging, improve error handling workflow, clang-format. (#3385)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#include "util/coordinate_calculation.hpp"
|
||||
|
||||
#ifndef NDEBUG
|
||||
#include "util/simple_logger.hpp"
|
||||
#include "util/log.hpp"
|
||||
#endif
|
||||
#include "osrm/coordinate.hpp"
|
||||
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
#include "util/log.hpp"
|
||||
#include "util/isatty.hpp"
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
namespace osrm
|
||||
{
|
||||
namespace util
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
static const char COL_RESET[]{"\x1b[0m"};
|
||||
static const char RED[]{"\x1b[31m"};
|
||||
static const char YELLOW[]{"\x1b[33m"};
|
||||
#ifndef NDEBUG
|
||||
static const char MAGENTA[]{"\x1b[35m"};
|
||||
#endif
|
||||
// static const char GREEN[] { "\x1b[32m"};
|
||||
// static const char BLUE[] { "\x1b[34m"};
|
||||
// static const char CYAN[] { "\x1b[36m"};
|
||||
}
|
||||
|
||||
void LogPolicy::Unmute() { m_is_mute = false; }
|
||||
|
||||
void LogPolicy::Mute() { m_is_mute = true; }
|
||||
|
||||
bool LogPolicy::IsMute() const { return m_is_mute; }
|
||||
|
||||
LogPolicy &LogPolicy::GetInstance()
|
||||
{
|
||||
static LogPolicy runningInstance;
|
||||
return runningInstance;
|
||||
}
|
||||
|
||||
Log::Log(LogLevel level_, std::ostream &ostream) : level(level_), stream(ostream)
|
||||
{
|
||||
const bool is_terminal = IsStdoutATTY();
|
||||
std::lock_guard<std::mutex> lock(get_mutex());
|
||||
switch (level)
|
||||
{
|
||||
case logWARNING:
|
||||
stream << (is_terminal ? YELLOW : "") << "[warn] ";
|
||||
break;
|
||||
case logERROR:
|
||||
stream << (is_terminal ? RED : "") << "[error] ";
|
||||
break;
|
||||
case logDEBUG:
|
||||
#ifndef NDEBUG
|
||||
stream << (is_terminal ? MAGENTA : "") << "[debug] ";
|
||||
#endif
|
||||
break;
|
||||
default: // logINFO:
|
||||
stream << "[info] ";
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Log::Log(LogLevel level_) : Log(level_, buffer) {}
|
||||
|
||||
std::mutex &Log::get_mutex()
|
||||
{
|
||||
static std::mutex mtx;
|
||||
return mtx;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close down this logging instance.
|
||||
* This destructor is responsible for flushing any buffered data,
|
||||
* and printing a newline character (each logger object is responsible for only one line)
|
||||
* Because sub-classes can replace the `stream` object - we need to verify whether
|
||||
* we're writing to std::cerr/cout, or whether we should write to the stream
|
||||
*/
|
||||
Log::~Log()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(get_mutex());
|
||||
const bool usestd = (&stream == &buffer);
|
||||
if (!LogPolicy::GetInstance().IsMute())
|
||||
{
|
||||
const bool is_terminal = IsStdoutATTY();
|
||||
if (usestd)
|
||||
{
|
||||
switch (level)
|
||||
{
|
||||
case logWARNING:
|
||||
case logERROR:
|
||||
std::cerr << buffer.str();
|
||||
std::cerr << (is_terminal ? COL_RESET : "");
|
||||
std::cerr << std::endl;
|
||||
break;
|
||||
case logDEBUG:
|
||||
#ifdef NDEBUG
|
||||
break;
|
||||
#endif
|
||||
case logINFO:
|
||||
default:
|
||||
std::cout << buffer.str();
|
||||
std::cout << (is_terminal ? COL_RESET : "");
|
||||
std::cout << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
stream << (is_terminal ? COL_RESET : "");
|
||||
stream << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
UnbufferedLog::UnbufferedLog(LogLevel level_)
|
||||
: Log(level_, (level_ == logWARNING || level_ == logERROR) ? std::cerr : std::cout)
|
||||
{
|
||||
stream.flags(std::ios_base::unitbuf);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "util/name_table.hpp"
|
||||
#include "util/exception.hpp"
|
||||
#include "util/simple_logger.hpp"
|
||||
#include "util/log.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
@@ -32,9 +32,8 @@ NameTable::NameTable(const std::string &filename)
|
||||
}
|
||||
else
|
||||
{
|
||||
util::SimpleLogger().Write(logINFO)
|
||||
<< "list of street names is empty in construction of name table from: \"" << filename
|
||||
<< "\"";
|
||||
util::Log() << "list of street names is empty in construction of name table from: \""
|
||||
<< filename << "\"";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
#include "util/simple_logger.hpp"
|
||||
#include "util/isatty.hpp"
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
|
||||
namespace osrm
|
||||
{
|
||||
namespace util
|
||||
{
|
||||
|
||||
namespace
|
||||
{
|
||||
static const char COL_RESET[]{"\x1b[0m"};
|
||||
static const char RED[]{"\x1b[31m"};
|
||||
#ifndef NDEBUG
|
||||
static const char YELLOW[]{"\x1b[33m"};
|
||||
#endif
|
||||
// static const char GREEN[] { "\x1b[32m"};
|
||||
// static const char BLUE[] { "\x1b[34m"};
|
||||
// static const char MAGENTA[] { "\x1b[35m"};
|
||||
// static const char CYAN[] { "\x1b[36m"};
|
||||
}
|
||||
|
||||
void LogPolicy::Unmute() { m_is_mute = false; }
|
||||
|
||||
void LogPolicy::Mute() { m_is_mute = true; }
|
||||
|
||||
bool LogPolicy::IsMute() const { return m_is_mute; }
|
||||
|
||||
LogPolicy &LogPolicy::GetInstance()
|
||||
{
|
||||
static LogPolicy runningInstance;
|
||||
return runningInstance;
|
||||
}
|
||||
|
||||
SimpleLogger::SimpleLogger() : level(logINFO) {}
|
||||
|
||||
std::mutex &SimpleLogger::get_mutex()
|
||||
{
|
||||
static std::mutex mtx;
|
||||
return mtx;
|
||||
}
|
||||
|
||||
std::ostringstream &SimpleLogger::Write(LogLevel lvl) noexcept
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(get_mutex());
|
||||
level = lvl;
|
||||
os << "[";
|
||||
switch (level)
|
||||
{
|
||||
case logWARNING:
|
||||
os << "warn";
|
||||
break;
|
||||
case logDEBUG:
|
||||
#ifndef NDEBUG
|
||||
os << "debug";
|
||||
#endif
|
||||
break;
|
||||
default: // logINFO:
|
||||
os << "info";
|
||||
break;
|
||||
}
|
||||
os << "] ";
|
||||
return os;
|
||||
}
|
||||
|
||||
SimpleLogger::~SimpleLogger()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(get_mutex());
|
||||
if (!LogPolicy::GetInstance().IsMute())
|
||||
{
|
||||
const bool is_terminal = IsStdoutATTY();
|
||||
switch (level)
|
||||
{
|
||||
case logWARNING:
|
||||
std::cerr << (is_terminal ? RED : "") << os.str() << (is_terminal ? COL_RESET : "")
|
||||
<< std::endl;
|
||||
break;
|
||||
case logDEBUG:
|
||||
#ifndef NDEBUG
|
||||
std::cout << (is_terminal ? YELLOW : "") << os.str() << (is_terminal ? COL_RESET : "")
|
||||
<< std::endl;
|
||||
#endif
|
||||
break;
|
||||
case logINFO:
|
||||
default:
|
||||
std::cout << os.str() << (is_terminal ? COL_RESET : "") << std::endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user