diff --git a/client/decoder/flacDecoder.cpp b/client/decoder/flacDecoder.cpp index b2bca5d9..bd1740a7 100644 --- a/client/decoder/flacDecoder.cpp +++ b/client/decoder/flacDecoder.cpp @@ -162,7 +162,7 @@ FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder { if (buffer[channel] == NULL) { - SLOG(LOG_ERR) << "ERROR: buffer[" << channel << "] is NULL\n"; + SLOG(ERROR) << "ERROR: buffer[" << channel << "] is NULL\n"; return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT; } @@ -210,7 +210,7 @@ void metadata_callback(const FLAC__StreamDecoder *decoder, const FLAC__StreamMet void error_callback(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) { (void)decoder, (void)client_data; - SLOG(LOG_ERR) << "Got error callback: " << FLAC__StreamDecoderErrorStatusString[status] << "\n"; + SLOG(ERROR) << "Got error callback: " << FLAC__StreamDecoderErrorStatusString[status] << "\n"; static_cast(client_data)->lastError_ = std::unique_ptr(new FLAC__StreamDecoderErrorStatus(status)); /// TODO, see issue #120: diff --git a/client/snapClient.cpp b/client/snapClient.cpp index 602275af..fc62f0eb 100644 --- a/client/snapClient.cpp +++ b/client/snapClient.cpp @@ -194,7 +194,7 @@ int main (int argc, char **argv) processPriority = 19; if (processPriority != 0) setpriority(PRIO_PROCESS, 0, processPriority); - SLOG(LOG_NOTICE) << "daemon started" << std::endl; + SLOG(NOTICE) << "daemon started" << std::endl; } #endif @@ -226,7 +226,7 @@ int main (int argc, char **argv) } catch (const std::exception& e) { - SLOG(LOG_ERR) << "Exception: " << e.what() << std::endl; + SLOG(ERROR) << "Exception: " << e.what() << std::endl; } chronos::sleep(500); } @@ -245,11 +245,11 @@ int main (int argc, char **argv) } catch (const std::exception& e) { - SLOG(LOG_ERR) << "Exception: " << e.what() << std::endl; + SLOG(ERROR) << "Exception: " << e.what() << std::endl; exitcode = EXIT_FAILURE; } - SLOG(LOG_NOTICE) << "daemon terminated." << endl; + SLOG(NOTICE) << "daemon terminated." << endl; exit(exitcode); } diff --git a/common/log.h b/common/log.h index 075a3c94..2c0a1d10 100644 --- a/common/log.h +++ b/common/log.h @@ -3,7 +3,7 @@ / _\ ( )( \/ )( ) / \ / __) / \ )( ) ( / (_/\( O )( (_ \ \_/\_/(__)(_/\_)\____/ \__/ \___/ - version 0.13.0 + version 0.16.0 https://github.com/badaix/aixlog This file is part of aixlog @@ -47,20 +47,26 @@ #endif -/// Internal helper defines -#define LOG_WO_TAG(P) std::clog << (AixLog::Severity)P -#define LOG_TAG(P, T) std::clog << (AixLog::Severity)P << TAG(T) +/// Internal helper macros (exposed, but shouldn't be used directly) +#define AIXLOG_INTERNAL__LOG_WO_TAG(SEVERITY_) std::clog << (AixLog::Severity)AixLog::SEVERITY_ +#define AIXLOG_INTERNAL__LOG_TAG(SEVERITY_, TAG_) std::clog << (AixLog::Severity)AixLog::SEVERITY_ << TAG(TAG_) -#define ONE_COLOR(FG) AixLog::Color::FG -#define TWO_COLOR(FG, BG) AixLog::TextColor(AixLog::Color::FG, AixLog::Color::BG) +#define AIXLOG_INTERNAL__ONE_COLOR(FG_) AixLog::Color::FG_ +#define AIXLOG_INTERNAL__TWO_COLOR(FG_, BG_) AixLog::TextColor(AixLog::Color::FG_, AixLog::Color::BG_) -#define VAR_PARM(x,P,T,FUN, ...) FUN +//https://stackoverflow.com/questions/3046889/optional-parameters-with-c-macros +#define AIXLOG_INTERNAL__VAR_PARM(x,PARAM1_,PARAM2_,FUNC_, ...) FUNC_ -/// External logger defines -#define LOG(...) VAR_PARM(,##__VA_ARGS__, LOG_TAG(__VA_ARGS__), LOG_WO_TAG(__VA_ARGS__)) << TIMESTAMP << FUNC -#define SLOG(...) VAR_PARM(,##__VA_ARGS__, LOG_TAG(__VA_ARGS__), LOG_WO_TAG(__VA_ARGS__)) << TIMESTAMP << SPECIAL << FUNC -#define COLOR(...) VAR_PARM(,##__VA_ARGS__, TWO_COLOR(__VA_ARGS__), ONE_COLOR(__VA_ARGS__)) +/// External logger macros +// usage: LOG(SEVERITY) or LOG(SEVERITY, TAG) +// e.g.: LOG(NOTICE) or LOG(NOTICE, "my tag") +#define LOG(...) AIXLOG_INTERNAL__VAR_PARM(,##__VA_ARGS__, AIXLOG_INTERNAL__LOG_TAG(__VA_ARGS__), AIXLOG_INTERNAL__LOG_WO_TAG(__VA_ARGS__)) << TIMESTAMP << FUNC +#define SLOG(...) AIXLOG_INTERNAL__VAR_PARM(,##__VA_ARGS__, AIXLOG_INTERNAL__LOG_TAG(__VA_ARGS__), AIXLOG_INTERNAL__LOG_WO_TAG(__VA_ARGS__)) << TIMESTAMP << SPECIAL << FUNC + +// usage: COLOR(TEXT_COLOR, BACKGROUND_COLOR) or COLOR(TEXT_COLOR) +// e.g.: COLOR(yellow, blue) or COLOR(red) +#define COLOR(...) AIXLOG_INTERNAL__VAR_PARM(,##__VA_ARGS__, AIXLOG_INTERNAL__TWO_COLOR(__VA_ARGS__), AIXLOG_INTERNAL__ONE_COLOR(__VA_ARGS__)) #define FUNC AixLog::Function(__func__, __FILE__, __LINE__) #define TAG AixLog::Tag @@ -69,6 +75,10 @@ #define TIMESTAMP AixLog::Timestamp(std::chrono::system_clock::now()) + +namespace AixLog +{ + enum SEVERITY { // https://chromium.googlesource.com/chromium/mini_chromium/+/master/base/logging.cc @@ -98,9 +108,6 @@ enum SEVERITY -namespace AixLog -{ - enum class Type { normal, @@ -557,7 +564,7 @@ struct SinkCerr : public SinkFormat /// Not tested due to unavailability of Windows -struct SinkOutputDebugString : Sink +struct SinkOutputDebugString : public Sink { SinkOutputDebugString(Severity severity, Type type = Type::all, const std::string& default_tag = "") : Sink(severity, type) { @@ -573,7 +580,7 @@ struct SinkOutputDebugString : Sink -struct SinkUnifiedLogging : Sink +struct SinkUnifiedLogging : public Sink { SinkUnifiedLogging(Severity severity, Type type = Type::all) : Sink(severity, type) { @@ -918,7 +925,7 @@ static std::ostream& operator<< (std::ostream& os, const Color& color) } -} +} /// namespace AixLog #endif /// AIX_LOG_HPP diff --git a/server/config.cpp b/server/config.cpp index aed1129b..e5f12982 100644 --- a/server/config.cpp +++ b/server/config.cpp @@ -48,7 +48,7 @@ Config::Config() throw SnapException("failed to create settings directory: \"" + dir + "\": " + cpt::to_string(errno)); filename_ = dir + "server.json"; - SLOG(LOG_NOTICE) << "Settings file: \"" << filename_ << "\"\n"; + SLOG(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) diff --git a/server/controlServer.cpp b/server/controlServer.cpp index 328c77d2..a1213768 100644 --- a/server/controlServer.cpp +++ b/server/controlServer.cpp @@ -48,7 +48,7 @@ void ControlServer::cleanup() { if (!(*it)->active()) { - SLOG(LOG_ERR) << "Session inactive. Removing\n"; + SLOG(ERROR) << "Session inactive. Removing\n"; // don't block: remove ClientSession in a thread auto func = [](shared_ptr s)->void{s->stop();}; std::thread t(func, *it); @@ -115,7 +115,7 @@ void ControlServer::handleAccept(socket_ptr socket) 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(LOG_NOTICE) << "ControlServer::NewConnection: " << socket->remote_endpoint().address().to_string() << endl; + SLOG(NOTICE) << "ControlServer::NewConnection: " << socket->remote_endpoint().address().to_string() << endl; shared_ptr session = make_shared(this, socket); { std::lock_guard mlock(mutex_); diff --git a/server/controlSession.cpp b/server/controlSession.cpp index a6b3473f..3922e7a2 100644 --- a/server/controlSession.cpp +++ b/server/controlSession.cpp @@ -145,7 +145,7 @@ void ControlSession::reader() } catch (const std::exception& e) { - SLOG(LOG_ERR) << "Exception in ControlSession::reader(): " << e.what() << endl; + SLOG(ERROR) << "Exception in ControlSession::reader(): " << e.what() << endl; } active_ = false; } @@ -166,7 +166,7 @@ void ControlSession::writer() } catch (const std::exception& e) { - SLOG(LOG_ERR) << "Exception in ControlSession::writer(): " << e.what() << endl; + SLOG(ERROR) << "Exception in ControlSession::writer(): " << e.what() << endl; } active_ = false; } diff --git a/server/snapServer.cpp b/server/snapServer.cpp index 172ee27f..a0650a6d 100644 --- a/server/snapServer.cpp +++ b/server/snapServer.cpp @@ -99,7 +99,7 @@ int main(int argc, char* argv[]) } catch (const std::invalid_argument& e) { - SLOG(LOG_ERR) << "Exception: " << e.what() << std::endl; + SLOG(ERROR) << "Exception: " << e.what() << std::endl; cout << "\n" << op << "\n"; exit(EXIT_FAILURE); } @@ -180,7 +180,7 @@ int main(int argc, char* argv[]) processPriority = 19; if (processPriority != 0) setpriority(PRIO_PROCESS, 0, processPriority); - SLOG(LOG_NOTICE) << "daemon started" << std::endl; + SLOG(NOTICE) << "daemon started" << std::endl; } #endif @@ -214,11 +214,11 @@ int main(int argc, char* argv[]) } catch (const std::exception& e) { - SLOG(LOG_ERR) << "Exception: " << e.what() << std::endl; + SLOG(ERROR) << "Exception: " << e.what() << std::endl; exitcode = EXIT_FAILURE; } - SLOG(LOG_NOTICE) << "daemon terminated." << endl; + SLOG(NOTICE) << "daemon terminated." << endl; exit(exitcode); } diff --git a/server/streamServer.cpp b/server/streamServer.cpp index 92cff841..b2cddd93 100644 --- a/server/streamServer.cpp +++ b/server/streamServer.cpp @@ -585,7 +585,7 @@ void StreamServer::handleAccept(socket_ptr socket) /// experimental: turn on tcp::no_delay socket->set_option(tcp::no_delay(true)); - SLOG(LOG_NOTICE) << "StreamServer::NewConnection: " << socket->remote_endpoint().address().to_string() << endl; + SLOG(NOTICE) << "StreamServer::NewConnection: " << socket->remote_endpoint().address().to_string() << endl; shared_ptr session = make_shared(this, socket); session->setBufferMs(settings_.bufferMs); @@ -620,7 +620,7 @@ void StreamServer::start() } catch (const std::exception& e) { - SLOG(LOG_NOTICE) << "StreamServer::start: " << e.what() << endl; + SLOG(NOTICE) << "StreamServer::start: " << e.what() << endl; stop(); throw; } diff --git a/server/streamSession.cpp b/server/streamSession.cpp index 8e9400d6..0d18fb89 100644 --- a/server/streamSession.cpp +++ b/server/streamSession.cpp @@ -183,7 +183,7 @@ void StreamSession::getNextMessage() baseMessage.deserialize(&buffer[0]); if (baseMessage.size > msg::max_size) { - SLOG(LOG_ERR) << "received message of type " << baseMessage.type << " to large: " << baseMessage.size << "\n"; + SLOG(ERROR) << "received message of type " << baseMessage.type << " to large: " << baseMessage.size << "\n"; stop(); return; } @@ -213,7 +213,7 @@ void StreamSession::reader() } catch (const std::exception& e) { - SLOG(LOG_ERR) << "Exception in StreamSession::reader(): " << e.what() << endl; + SLOG(ERROR) << "Exception in StreamSession::reader(): " << e.what() << endl; } if (active_ && (messageReceiver_ != NULL)) @@ -252,7 +252,7 @@ void StreamSession::writer() } catch (const std::exception& e) { - SLOG(LOG_ERR) << "Exception in StreamSession::writer(): " << e.what() << endl; + SLOG(ERROR) << "Exception in StreamSession::writer(): " << e.what() << endl; } if (active_ && (messageReceiver_ != NULL))