diff --git a/src/input/readers/ascii/Ascii.h b/src/input/readers/ascii/Ascii.h index f30fe58c3d..5f3370971d 100644 --- a/src/input/readers/ascii/Ascii.h +++ b/src/input/readers/ascii/Ascii.h @@ -59,7 +59,7 @@ protected: bool DoUpdate() override; bool DoHeartbeat(double network_time, double current_time) override; - zeek::detail::Location* GetLocationInfo() const override { return read_location.get(); } + const zeek::detail::Location* GetLocationInfo() const override { return read_location.get(); } private: bool ReadHeader(bool useCached); diff --git a/src/threading/MsgThread.cc b/src/threading/MsgThread.cc index 110e58f831..f8fceae59c 100644 --- a/src/threading/MsgThread.cc +++ b/src/threading/MsgThread.cc @@ -89,11 +89,11 @@ public: INTERNAL_ERROR }; - ReporterMessage(Type arg_type, MsgThread* thread, const char* arg_msg) + ReporterMessage(Type arg_type, MsgThread* thread, std::string_view arg_msg) : OutputMessage("ReporterMessage", thread) { type = arg_type; - msg = util::copy_string(arg_msg); + msg = util::copy_string(arg_msg.data()); } ~ReporterMessage() override { delete[] msg; } @@ -125,11 +125,11 @@ public: class DebugMessage final : public OutputMessage { public: - DebugMessage(DebugStream arg_stream, MsgThread* thread, const char* arg_msg) + DebugMessage(DebugStream arg_stream, MsgThread* thread, std::string_view arg_msg) : OutputMessage("DebugMessage", thread) { stream = arg_stream; - msg = util::copy_string(arg_msg); + msg = util::copy_string(arg_msg.data()); } ~DebugMessage() override { delete[] msg; } @@ -341,7 +341,7 @@ void MsgThread::Finished() SendOut(new detail::FinishedMessage(this)); } -void MsgThread::Info(const char* msg) +std::string MsgThread::BuildMsgWithLocation(const char* msg) { ODesc desc; @@ -352,96 +352,48 @@ void MsgThread::Info(const char* msg) } desc.Add(msg); - SendOut(new detail::ReporterMessage(detail::ReporterMessage::INFO, this, desc.Description())); + return std::string(desc.Description()); + } + +void MsgThread::Info(const char* msg) + { + SendOut(new detail::ReporterMessage(detail::ReporterMessage::INFO, this, + BuildMsgWithLocation(msg))); } void MsgThread::Warning(const char* msg) { - ODesc desc; - - if ( auto* location = GetLocationInfo() ) - { - location->Describe(&desc); - desc.Add(": "); - } - - desc.Add(msg); - SendOut( - new detail::ReporterMessage(detail::ReporterMessage::WARNING, this, desc.Description())); + SendOut(new detail::ReporterMessage(detail::ReporterMessage::WARNING, this, + BuildMsgWithLocation(msg))); } void MsgThread::Error(const char* msg) { - ODesc desc; - - if ( auto* location = GetLocationInfo() ) - { - location->Describe(&desc); - desc.Add(": "); - } - - desc.Add(msg); - SendOut(new detail::ReporterMessage(detail::ReporterMessage::ERROR, this, desc.Description())); + SendOut(new detail::ReporterMessage(detail::ReporterMessage::ERROR, this, + BuildMsgWithLocation(msg))); } void MsgThread::FatalError(const char* msg) { - ODesc desc; - - if ( auto* location = GetLocationInfo() ) - { - location->Describe(&desc); - desc.Add(": "); - } - - desc.Add(msg); SendOut(new detail::ReporterMessage(detail::ReporterMessage::FATAL_ERROR, this, - desc.Description())); + BuildMsgWithLocation(msg))); } void MsgThread::FatalErrorWithCore(const char* msg) { - ODesc desc; - - if ( auto* location = GetLocationInfo() ) - { - location->Describe(&desc); - desc.Add(": "); - } - - desc.Add(msg); SendOut(new detail::ReporterMessage(detail::ReporterMessage::FATAL_ERROR_WITH_CORE, this, - desc.Description())); + BuildMsgWithLocation(msg))); } void MsgThread::InternalWarning(const char* msg) { - ODesc desc; - - if ( auto* location = GetLocationInfo() ) - { - location->Describe(&desc); - desc.Add(": "); - } - - desc.Add(msg); SendOut(new detail::ReporterMessage(detail::ReporterMessage::INTERNAL_WARNING, this, - desc.Description())); + BuildMsgWithLocation(msg))); } void MsgThread::InternalError(const char* msg) { - // This one aborts immediately. - ODesc desc; - - if ( auto* location = GetLocationInfo() ) - { - location->Describe(&desc); - desc.Add(": "); - } - - desc.Add(msg); - fprintf(stderr, "internal error in thread: %s\n", desc.Description()); + fprintf(stderr, "internal error in thread: %s\n", BuildMsgWithLocation(msg).c_str()); abort(); } @@ -449,16 +401,7 @@ void MsgThread::InternalError(const char* msg) void MsgThread::Debug(DebugStream stream, const char* msg) { - ODesc desc; - - if ( auto* location = GetLocationInfo() ) - { - location->Describe(&desc); - desc.Add(": "); - } - - desc.Add(msg); - SendOut(new detail::DebugMessage(stream, this, desc.Description())); + SendOut(new detail::DebugMessage(stream, this, BuildMsgWithLocation(msg))); } #endif diff --git a/src/threading/MsgThread.h b/src/threading/MsgThread.h index a14ca7c102..a846c20c68 100644 --- a/src/threading/MsgThread.h +++ b/src/threading/MsgThread.h @@ -289,7 +289,7 @@ protected: * @return A Location pointer containing the file location information, * or nullptr if nothing is available. */ - virtual zeek::detail::Location* GetLocationInfo() const { return nullptr; } + virtual const zeek::detail::Location* GetLocationInfo() const { return nullptr; } private: /** @@ -353,6 +353,8 @@ private: */ void Finished(); + std::string BuildMsgWithLocation(const char* msg); + Queue queue_in; Queue queue_out;