Remove SLOG from logging

This commit is contained in:
badaix 2020-04-16 09:01:09 +02:00
parent b27aee80e6
commit ad8332345f
9 changed files with 55 additions and 99 deletions

View file

@ -85,7 +85,7 @@ void ClientConnection::start()
// setsockopt(socket->native_handle(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
// setsockopt(socket->native_handle(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
socket_.connect(*iterator);
SLOG(NOTICE) << "Connected to " << socket_.remote_endpoint().address().to_string() << endl;
LOG(NOTICE) << "Connected to " << socket_.remote_endpoint().address().to_string() << endl;
active_ = true;
sumTimeout_ = chronos::msec(0);
readerThread_ = make_unique<thread>(&ClientConnection::reader, this);

View file

@ -261,7 +261,7 @@ void Controller::worker()
catch (const std::exception& e)
{
async_exception_ = nullptr;
SLOG(ERROR) << "Exception in Controller::worker(): " << e.what() << endl;
LOG(ERROR) << "Exception in Controller::worker(): " << e.what() << endl;
clientConnection_->stop();
player_.reset();
stream_.reset();

View file

@ -168,7 +168,7 @@ FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder* /*decod
{
if (buffer[channel] == nullptr)
{
SLOG(ERROR) << "ERROR: buffer[" << channel << "] is NULL\n";
LOG(ERROR) << "ERROR: buffer[" << channel << "] is NULL\n";
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
@ -211,7 +211,7 @@ void metadata_callback(const FLAC__StreamDecoder* /*decoder*/, const FLAC__Strea
void error_callback(const FLAC__StreamDecoder* /*decoder*/, FLAC__StreamDecoderErrorStatus status, void* client_data)
{
SLOG(ERROR) << "Got error callback: " << FLAC__StreamDecoderErrorStatusString[status] << "\n";
LOG(ERROR) << "Got error callback: " << FLAC__StreamDecoderErrorStatusString[status] << "\n";
static_cast<FlacDecoder*>(client_data)->lastError_ = std::make_unique<FLAC__StreamDecoderErrorStatus>(status);
}
} // namespace callback

View file

@ -175,17 +175,17 @@ int main(int argc, char** argv)
// XXX: Only one metadata option must be set
AixLog::Log::init<AixLog::SinkNative>("snapclient", AixLog::Severity::trace, AixLog::Type::special);
// TODO: AixLog::Log::init<AixLog::SinkNative>("snapclient", AixLog::Severity::trace, AixLog::Type::special);
if (debugOption->is_set())
{
AixLog::Log::instance().add_logsink<AixLog::SinkCout>(AixLog::Severity::trace, AixLog::Type::all, "%Y-%m-%d %H-%M-%S.#ms [#severity] (#tag_func)");
AixLog::Log::instance().add_logsink<AixLog::SinkCout>(AixLog::Severity::trace, "%Y-%m-%d %H-%M-%S.#ms [#severity] (#tag_func)");
if (!debugOption->value().empty())
AixLog::Log::instance().add_logsink<AixLog::SinkFile>(AixLog::Severity::trace, AixLog::Type::all, debugOption->value(),
AixLog::Log::instance().add_logsink<AixLog::SinkFile>(AixLog::Severity::trace, debugOption->value(),
"%Y-%m-%d %H-%M-%S.#ms [#severity] (#tag_func)");
}
else
{
AixLog::Log::instance().add_logsink<AixLog::SinkCout>(AixLog::Severity::info, AixLog::Type::all, "%Y-%m-%d %H-%M-%S [#severity] (#tag_func)");
AixLog::Log::instance().add_logsink<AixLog::SinkCout>(AixLog::Severity::info, "%Y-%m-%d %H-%M-%S [#severity] (#tag_func)");
}
#ifdef HAS_DAEMON
@ -209,7 +209,7 @@ int main(int argc, char** argv)
group = user_group[1];
}
daemon = std::make_unique<Daemon>(user, group, pidFile);
SLOG(NOTICE) << "daemonizing" << std::endl;
LOG(NOTICE) << "daemonizing" << std::endl;
daemon->daemonize();
if (processPriority < -20)
processPriority = -20;
@ -217,7 +217,7 @@ int main(int argc, char** argv)
processPriority = 19;
if (processPriority != 0)
setpriority(PRIO_PROCESS, 0, processPriority);
SLOG(NOTICE) << "daemon started" << std::endl;
LOG(NOTICE) << "daemon started" << std::endl;
}
#endif
@ -259,7 +259,7 @@ int main(int argc, char** argv)
#endif
SIGTERM, SIGINT},
[&active, &controller](int signal, const std::string& strsignal) {
SLOG(INFO) << "Received signal " << signal << ": " << strsignal << "\n";
LOG(INFO) << "Received signal " << signal << ": " << strsignal << "\n";
active = false;
if (controller)
{
@ -291,7 +291,7 @@ int main(int argc, char** argv)
}
catch (const std::exception& e)
{
SLOG(ERROR) << "Exception: " << e.what() << std::endl;
LOG(ERROR) << "Exception: " << e.what() << std::endl;
}
}
#endif
@ -311,10 +311,10 @@ int main(int argc, char** argv)
}
catch (const std::exception& e)
{
SLOG(ERROR) << "Exception: " << e.what() << std::endl;
LOG(ERROR) << "Exception: " << e.what() << std::endl;
exitcode = EXIT_FAILURE;
}
SLOG(NOTICE) << "daemon terminated." << endl;
LOG(NOTICE) << "daemon terminated." << endl;
exit(exitcode);
}

View file

@ -98,7 +98,6 @@
#ifndef WIN32
#define LOG(...) AIXLOG_INTERNAL__LOG_MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__) << TIMESTAMP << FUNC
#endif
#define SLOG(...) AIXLOG_INTERNAL__LOG_MACRO_CHOOSER(__VA_ARGS__)(__VA_ARGS__) << TIMESTAMP << SPECIAL << FUNC
// usage: COLOR(TEXT_COLOR, BACKGROUND_COLOR) or COLOR(TEXT_COLOR)
// e.g.: COLOR(yellow, blue) or COLOR(red)
@ -107,7 +106,6 @@
#define FUNC AixLog::Function(AIXLOG_INTERNAL__FUNC, __FILE__, __LINE__)
#define TAG AixLog::Tag
#define COND AixLog::Conditional
#define SPECIAL AixLog::Type::special
#define TIMESTAMP AixLog::Timestamp(std::chrono::system_clock::now())
@ -176,19 +174,6 @@ enum class Severity : std::int8_t
fatal = SEVERITY::FATAL
};
/**
* @brief
* Type of the log message or Sink
*
* "normal" messages will be logged by "normal" or "all" Sinks
* "special" ones by "special" or "all" Sinks
*/
enum class Type
{
normal,
special,
all
};
/**
* @brief
@ -409,13 +394,12 @@ private:
*/
struct Metadata
{
Metadata() : severity(Severity::trace), tag(nullptr), type(Type::normal), function(nullptr), timestamp(nullptr)
Metadata() : severity(Severity::trace), tag(nullptr), function(nullptr), timestamp(nullptr)
{
}
Severity severity;
Tag tag;
Type type;
Function function;
Timestamp timestamp;
};
@ -428,33 +412,19 @@ struct Metadata
*/
struct Sink
{
Sink(Severity severity, Type type) : severity(severity), sink_type_(type)
Sink(Severity severity) : severity(severity)
{
}
virtual ~Sink() = default;
virtual void log(const Metadata& metadata, const std::string& message) = 0;
virtual Type get_type() const
{
return sink_type_;
}
virtual Sink& set_type(Type sink_type)
{
sink_type_ = sink_type;
return *this;
}
Severity severity;
protected:
Type sink_type_;
};
/// ostream operators << for the meta data structs
static std::ostream& operator<<(std::ostream& os, const Severity& log_severity);
static std::ostream& operator<<(std::ostream& os, const Type& log_type);
static std::ostream& operator<<(std::ostream& os, const Timestamp& timestamp);
static std::ostream& operator<<(std::ostream& os, const Tag& tag);
static std::ostream& operator<<(std::ostream& os, const Function& function);
@ -549,7 +519,7 @@ protected:
Log() noexcept : last_buffer_(nullptr)
{
std::clog.rdbuf(this);
std::clog << Severity() << Type::normal << Tag() << Function() << Conditional() << AixLog::Color::NONE << std::flush;
std::clog << Severity() << Tag() << Function() << Conditional() << AixLog::Color::NONE << std::flush;
}
virtual ~Log()
@ -566,9 +536,8 @@ protected:
{
for (const auto& sink : log_sinks_)
{
if ((metadata_.type == Type::all) || (sink->get_type() == Type::all) || (metadata_.type == sink->get_type()))
if (metadata_.severity >= sink->severity)
sink->log(metadata_, get_stream().str());
if (metadata_.severity >= sink->severity)
sink->log(metadata_, get_stream().str());
}
}
get_stream().str("");
@ -597,7 +566,6 @@ protected:
private:
friend std::ostream& operator<<(std::ostream& os, const Severity& log_severity);
friend std::ostream& operator<<(std::ostream& os, const Type& log_type);
friend std::ostream& operator<<(std::ostream& os, const Timestamp& timestamp);
friend std::ostream& operator<<(std::ostream& os, const Tag& tag);
friend std::ostream& operator<<(std::ostream& os, const Function& function);
@ -639,7 +607,7 @@ private:
*/
struct SinkFormat : public Sink
{
SinkFormat(Severity severity, Type type, const std::string& format) : Sink(severity, type), format_(format)
SinkFormat(Severity severity, const std::string& format) : Sink(severity), format_(format)
{
}
@ -705,7 +673,7 @@ protected:
*/
struct SinkCout : public SinkFormat
{
SinkCout(Severity severity, Type type, const std::string& format = "%Y-%m-%d %H-%M-%S.#ms [#severity] (#tag_func)") : SinkFormat(severity, type, format)
SinkCout(Severity severity, const std::string& format = "%Y-%m-%d %H-%M-%S.#ms [#severity] (#tag_func)") : SinkFormat(severity, format)
{
}
@ -721,7 +689,7 @@ struct SinkCout : public SinkFormat
*/
struct SinkCerr : public SinkFormat
{
SinkCerr(Severity severity, Type type, const std::string& format = "%Y-%m-%d %H-%M-%S.#ms [#severity] (#tag_func)") : SinkFormat(severity, type, format)
SinkCerr(Severity severity, const std::string& format = "%Y-%m-%d %H-%M-%S.#ms [#severity] (#tag_func)") : SinkFormat(severity, format)
{
}
@ -737,8 +705,8 @@ struct SinkCerr : public SinkFormat
*/
struct SinkFile : public SinkFormat
{
SinkFile(Severity severity, Type type, const std::string& filename, const std::string& format = "%Y-%m-%d %H-%M-%S.#ms [#severity] (#tag_func)")
: SinkFormat(severity, type, format)
SinkFile(Severity severity, const std::string& filename, const std::string& format = "%Y-%m-%d %H-%M-%S.#ms [#severity] (#tag_func)")
: SinkFormat(severity, format)
{
ofs.open(filename.c_str(), std::ofstream::out | std::ofstream::trunc);
}
@ -766,7 +734,7 @@ protected:
*/
struct SinkOutputDebugString : public Sink
{
SinkOutputDebugString(Severity severity, Type type = Type::all) : Sink(severity, type)
SinkOutputDebugString(Severity severity) : Sink(severity)
{
}
@ -785,7 +753,7 @@ struct SinkOutputDebugString : public Sink
*/
struct SinkUnifiedLogging : public Sink
{
SinkUnifiedLogging(Severity severity, Type type = Type::all) : Sink(severity, type)
SinkUnifiedLogging(Severity severity) : Sink(severity)
{
}
@ -825,7 +793,7 @@ struct SinkUnifiedLogging : public Sink
*/
struct SinkSyslog : public Sink
{
SinkSyslog(const char* ident, Severity severity, Type type) : Sink(severity, type)
SinkSyslog(const char* ident, Severity severity) : Sink(severity)
{
openlog(ident, LOG_PID, LOG_USER);
}
@ -874,7 +842,7 @@ struct SinkSyslog : public Sink
*/
struct SinkAndroid : public Sink
{
SinkAndroid(const std::string& ident, Severity severity, Type type = Type::all) : Sink(severity, type), ident_(ident)
SinkAndroid(const std::string& ident, Severity severity) : Sink(severity), ident_(ident)
{
}
@ -931,7 +899,7 @@ protected:
*/
struct SinkEventLog : public Sink
{
SinkEventLog(const std::string& ident, Severity severity, Type type = Type::all) : Sink(severity, type)
SinkEventLog(const std::string& ident, Severity severity) : Sink(severity)
{
std::wstring wide = std::wstring(ident.begin(), ident.end()); // stijnvdb: RegisterEventSource expands to RegisterEventSourceW which takes wchar_t
event_log = RegisterEventSource(NULL, wide.c_str());
@ -983,16 +951,16 @@ protected:
*/
struct SinkNative : public Sink
{
SinkNative(const std::string& ident, Severity severity, Type type = Type::all) : Sink(severity, type), log_sink_(nullptr), ident_(ident)
SinkNative(const std::string& ident, Severity severity) : Sink(severity), log_sink_(nullptr), ident_(ident)
{
#ifdef __ANDROID__
log_sink_ = std::make_shared<SinkAndroid>(ident_, severity, type);
log_sink_ = std::make_shared<SinkAndroid>(ident_, severity);
#elif HAS_APPLE_UNIFIED_LOG_
log_sink_ = std::make_shared<SinkUnifiedLogging>(severity, type);
log_sink_ = std::make_shared<SinkUnifiedLogging>(severity);
#elif _WIN32
log_sink_ = std::make_shared<SinkEventLog>(ident, severity, type);
log_sink_ = std::make_shared<SinkEventLog>(ident, severity);
#elif HAS_SYSLOG_
log_sink_ = std::make_shared<SinkSyslog>(ident_.c_str(), severity, type);
log_sink_ = std::make_shared<SinkSyslog>(ident_.c_str(), severity);
#else
/// will not throw or something. Use "get_logger()" to check for success
log_sink_ = nullptr;
@ -1027,7 +995,7 @@ struct SinkCallback : public Sink
{
using callback_fun = std::function<void(const Metadata& metadata, const std::string& message)>;
SinkCallback(Severity severity, Type type, callback_fun callback) : Sink(severity, type), callback_(callback)
SinkCallback(Severity severity, callback_fun callback) : Sink(severity), callback_(callback)
{
}
@ -1057,7 +1025,6 @@ static std::ostream& operator<<(std::ostream& os, const Severity& log_severity)
{
log->sync();
log->metadata_.severity = log_severity;
log->metadata_.type = Type::normal;
log->metadata_.timestamp = nullptr;
log->metadata_.tag = nullptr;
log->metadata_.function = nullptr;
@ -1071,17 +1038,6 @@ static std::ostream& operator<<(std::ostream& os, const Severity& log_severity)
return os;
}
static std::ostream& operator<<(std::ostream& os, const Type& log_type)
{
Log* log = dynamic_cast<Log*>(os.rdbuf());
if (log != nullptr)
{
std::lock_guard<std::recursive_mutex> lock(log->mutex_);
log->metadata_.type = log_type;
}
return os;
}
static std::ostream& operator<<(std::ostream& os, const Timestamp& timestamp)
{
Log* log = dynamic_cast<Log*>(os.rdbuf());

View file

@ -62,7 +62,7 @@ void Config::init(const std::string& root_directory, const std::string& user, co
throw SnapException("failed to create settings directory: \"" + dir + "\": " + cpt::to_string(errno));
filename_ = dir + "server.json";
SLOG(NOTICE) << "Settings file: \"" << filename_ << "\"\n";
LOG(NOTICE) << "Settings file: \"" << filename_ << "\"\n";
int fd;
if ((fd = open(filename_.c_str(), O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
@ -83,7 +83,7 @@ void Config::init(const std::string& root_directory, const std::string& user, co
}
catch (const std::exception& e)
{
SLOG(ERROR) << "Exception in chown: " << e.what() << "\n";
LOG(ERROR) << "Exception in chown: " << e.what() << "\n";
}
}

View file

@ -51,7 +51,7 @@ void ControlServer::cleanup()
auto count = distance(new_end, sessions_.end());
if (count > 0)
{
SLOG(ERROR) << "Removing " << count << " inactive session(s), active sessions: " << sessions_.size() - count << "\n";
LOG(ERROR) << "Removing " << count << " inactive session(s), active sessions: " << sessions_.size() - count << "\n";
sessions_.erase(new_end, sessions_.end());
}
}
@ -116,7 +116,7 @@ void ControlServer::handleAccept(tcp::socket socket, Args&&... args)
setsockopt(socket.native_handle(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
setsockopt(socket.native_handle(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
// socket->set_option(boost::asio::ip::tcp::no_delay(false));
SLOG(NOTICE) << "ControlServer::NewConnection: " << socket.remote_endpoint().address().to_string() << endl;
LOG(NOTICE) << "ControlServer::NewConnection: " << socket.remote_endpoint().address().to_string() << endl;
shared_ptr<SessionType> session = make_shared<SessionType>(this, io_context_, std::move(socket), std::forward<Args>(args)...);
{
std::lock_guard<std::recursive_mutex> mlock(session_mutex_);
@ -127,7 +127,7 @@ void ControlServer::handleAccept(tcp::socket socket, Args&&... args)
}
catch (const std::exception& e)
{
SLOG(ERROR) << "Exception in ControlServer::handleAccept: " << e.what() << endl;
LOG(ERROR) << "Exception in ControlServer::handleAccept: " << e.what() << endl;
}
startAccept();
}

View file

@ -138,7 +138,7 @@ int main(int argc, char* argv[])
}
catch (const std::invalid_argument& e)
{
SLOG(ERROR) << "Exception: " << e.what() << std::endl;
LOG(ERROR) << "Exception: " << e.what() << std::endl;
cout << "\n" << op << "\n";
exit(EXIT_FAILURE);
}
@ -182,17 +182,17 @@ int main(int argc, char* argv[])
exit(EXIT_SUCCESS);
}
AixLog::Log::init<AixLog::SinkNative>("snapserver", AixLog::Severity::trace, AixLog::Type::special);
// TODO: AixLog::Log::init<AixLog::SinkNative>("snapserver", AixLog::Severity::trace, AixLog::Type::special);
if (settings.logging.debug)
{
AixLog::Log::instance().add_logsink<AixLog::SinkCout>(AixLog::Severity::trace, AixLog::Type::all, "%Y-%m-%d %H-%M-%S.#ms [#severity] (#tag_func)");
AixLog::Log::instance().add_logsink<AixLog::SinkCout>(AixLog::Severity::trace, "%Y-%m-%d %H-%M-%S.#ms [#severity] (#tag_func)");
if (!settings.logging.debug_logfile.empty())
AixLog::Log::instance().add_logsink<AixLog::SinkFile>(AixLog::Severity::trace, AixLog::Type::all, settings.logging.debug_logfile,
AixLog::Log::instance().add_logsink<AixLog::SinkFile>(AixLog::Severity::trace, settings.logging.debug_logfile,
"%Y-%m-%d %H-%M-%S.#ms [#severity] (#tag_func)");
}
else
{
AixLog::Log::instance().add_logsink<AixLog::SinkCout>(AixLog::Severity::info, AixLog::Type::all, "%Y-%m-%d %H-%M-%S [#severity] (#tag_func)");
AixLog::Log::instance().add_logsink<AixLog::SinkCout>(AixLog::Severity::info, "%Y-%m-%d %H-%M-%S [#severity] (#tag_func)");
}
for (const auto& opt : conf.unknown_options())
@ -228,7 +228,7 @@ int main(int argc, char* argv[])
data_dir = "/var/lib/snapserver";
Config::instance().init(data_dir, user, group);
daemon.reset(new Daemon(user, group, pid_file));
SLOG(NOTICE) << "daemonizing" << std::endl;
LOG(NOTICE) << "daemonizing" << std::endl;
daemon->daemonize();
if (processPriority < -20)
processPriority = -20;
@ -236,7 +236,7 @@ int main(int argc, char* argv[])
processPriority = 19;
if (processPriority != 0)
setpriority(PRIO_PROCESS, 0, processPriority);
SLOG(NOTICE) << "daemon started" << std::endl;
LOG(NOTICE) << "daemon started" << std::endl;
}
else
Config::instance().init(data_dir);
@ -284,9 +284,9 @@ int main(int argc, char* argv[])
boost::asio::signal_set signals(io_context, SIGHUP, SIGINT, SIGTERM);
signals.async_wait([&io_context](const boost::system::error_code& ec, int signal) {
if (!ec)
SLOG(INFO) << "Received signal " << signal << ": " << strsignal(signal) << "\n";
LOG(INFO) << "Received signal " << signal << ": " << strsignal(signal) << "\n";
else
SLOG(INFO) << "Failed to wait for signal: " << ec << "\n";
LOG(INFO) << "Failed to wait for signal: " << ec << "\n";
io_context.stop();
});
@ -305,10 +305,10 @@ int main(int argc, char* argv[])
}
catch (const std::exception& e)
{
SLOG(ERROR) << "Exception: " << e.what() << std::endl;
LOG(ERROR) << "Exception: " << e.what() << std::endl;
exitcode = EXIT_FAILURE;
}
Config::instance().save();
SLOG(NOTICE) << "daemon terminated." << endl;
LOG(NOTICE) << "daemon terminated." << endl;
exit(exitcode);
}

View file

@ -45,7 +45,7 @@ void StreamServer::cleanup()
auto count = distance(new_end, sessions_.end());
if (count > 0)
{
SLOG(ERROR) << "Removing " << count << " inactive session(s), active sessions: " << sessions_.size() - count << "\n";
LOG(ERROR) << "Removing " << count << " inactive session(s), active sessions: " << sessions_.size() - count << "\n";
sessions_.erase(new_end, sessions_.end());
}
}
@ -785,7 +785,7 @@ void StreamServer::handleAccept(tcp::socket socket)
/// experimental: turn on tcp::no_delay
socket.set_option(tcp::no_delay(true));
SLOG(NOTICE) << "StreamServer::NewConnection: " << socket.remote_endpoint().address().to_string() << endl;
LOG(NOTICE) << "StreamServer::NewConnection: " << socket.remote_endpoint().address().to_string() << endl;
shared_ptr<StreamSession> session = make_shared<StreamSession>(io_context_, this, std::move(socket));
session->setBufferMs(settings_.stream.bufferMs);
@ -797,7 +797,7 @@ void StreamServer::handleAccept(tcp::socket socket)
}
catch (const std::exception& e)
{
SLOG(ERROR) << "Exception in StreamServer::handleAccept: " << e.what() << endl;
LOG(ERROR) << "Exception in StreamServer::handleAccept: " << e.what() << endl;
}
startAccept();
}
@ -839,7 +839,7 @@ void StreamServer::start()
}
catch (const std::exception& e)
{
SLOG(NOTICE) << "StreamServer::start: " << e.what() << endl;
LOG(NOTICE) << "StreamServer::start: " << e.what() << endl;
stop();
throw;
}