Skip to content

Commit

Permalink
Remove SLOG from logging
Browse files Browse the repository at this point in the history
  • Loading branch information
badaix committed Apr 16, 2020
1 parent b27aee8 commit ad83323
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 99 deletions.
2 changes: 1 addition & 1 deletion client/client_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion client/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions client/decoder/flac_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions client/snapclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -209,15 +209,15 @@ 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;
else if (processPriority > 19)
processPriority = 19;
if (processPriority != 0)
setpriority(PRIO_PROCESS, 0, processPriority);
SLOG(NOTICE) << "daemon started" << std::endl;
LOG(NOTICE) << "daemon started" << std::endl;
}
#endif

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
86 changes: 21 additions & 65 deletions common/aixlog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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())


Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -409,13 +394,12 @@ struct Function
*/
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;
};
Expand All @@ -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);
Expand Down Expand Up @@ -549,7 +519,7 @@ class Log : public std::basic_streambuf<char, std::char_traits<char>>
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()
Expand All @@ -566,9 +536,8 @@ class Log : public std::basic_streambuf<char, std::char_traits<char>>
{
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("");
Expand Down Expand Up @@ -597,7 +566,6 @@ class Log : public std::basic_streambuf<char, std::char_traits<char>>

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);
Expand Down Expand Up @@ -639,7 +607,7 @@ class Log : public std::basic_streambuf<char, std::char_traits<char>>
*/
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)
{
}

Expand Down Expand Up @@ -705,7 +673,7 @@ struct SinkFormat : public Sink
*/
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)
{
}

Expand All @@ -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)
{
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -766,7 +734,7 @@ struct SinkFile : public SinkFormat
*/
struct SinkOutputDebugString : public Sink
{
SinkOutputDebugString(Severity severity, Type type = Type::all) : Sink(severity, type)
SinkOutputDebugString(Severity severity) : Sink(severity)
{
}

Expand All @@ -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)
{
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
{
}

Expand Down Expand Up @@ -931,7 +899,7 @@ struct SinkAndroid : public Sink
*/
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());
Expand Down Expand Up @@ -983,16 +951,16 @@ struct SinkEventLog : public Sink
*/
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;
Expand Down Expand Up @@ -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)
{
}

Expand Down Expand Up @@ -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;
Expand All @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions server/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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";
}
}

Expand Down
Loading

0 comments on commit ad83323

Please sign in to comment.