Move some repetitive code into a separate method

This commit is contained in:
Tim Wojtulewicz 2022-05-16 11:02:28 -07:00
parent ec50b66ff3
commit f4461d5e95
3 changed files with 25 additions and 80 deletions

View file

@ -59,7 +59,7 @@ protected:
bool DoUpdate() override; bool DoUpdate() override;
bool DoHeartbeat(double network_time, double current_time) 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: private:
bool ReadHeader(bool useCached); bool ReadHeader(bool useCached);

View file

@ -89,11 +89,11 @@ public:
INTERNAL_ERROR INTERNAL_ERROR
}; };
ReporterMessage(Type arg_type, MsgThread* thread, const char* arg_msg) ReporterMessage(Type arg_type, MsgThread* thread, std::string_view arg_msg)
: OutputMessage<MsgThread>("ReporterMessage", thread) : OutputMessage<MsgThread>("ReporterMessage", thread)
{ {
type = arg_type; type = arg_type;
msg = util::copy_string(arg_msg); msg = util::copy_string(arg_msg.data());
} }
~ReporterMessage() override { delete[] msg; } ~ReporterMessage() override { delete[] msg; }
@ -125,11 +125,11 @@ public:
class DebugMessage final : public OutputMessage<MsgThread> class DebugMessage final : public OutputMessage<MsgThread>
{ {
public: public:
DebugMessage(DebugStream arg_stream, MsgThread* thread, const char* arg_msg) DebugMessage(DebugStream arg_stream, MsgThread* thread, std::string_view arg_msg)
: OutputMessage<MsgThread>("DebugMessage", thread) : OutputMessage<MsgThread>("DebugMessage", thread)
{ {
stream = arg_stream; stream = arg_stream;
msg = util::copy_string(arg_msg); msg = util::copy_string(arg_msg.data());
} }
~DebugMessage() override { delete[] msg; } ~DebugMessage() override { delete[] msg; }
@ -341,7 +341,7 @@ void MsgThread::Finished()
SendOut(new detail::FinishedMessage(this)); SendOut(new detail::FinishedMessage(this));
} }
void MsgThread::Info(const char* msg) std::string MsgThread::BuildMsgWithLocation(const char* msg)
{ {
ODesc desc; ODesc desc;
@ -352,96 +352,48 @@ void MsgThread::Info(const char* msg)
} }
desc.Add(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) void MsgThread::Warning(const char* msg)
{ {
ODesc desc; SendOut(new detail::ReporterMessage(detail::ReporterMessage::WARNING, this,
BuildMsgWithLocation(msg)));
if ( auto* location = GetLocationInfo() )
{
location->Describe(&desc);
desc.Add(": ");
}
desc.Add(msg);
SendOut(
new detail::ReporterMessage(detail::ReporterMessage::WARNING, this, desc.Description()));
} }
void MsgThread::Error(const char* msg) void MsgThread::Error(const char* msg)
{ {
ODesc desc; SendOut(new detail::ReporterMessage(detail::ReporterMessage::ERROR, this,
BuildMsgWithLocation(msg)));
if ( auto* location = GetLocationInfo() )
{
location->Describe(&desc);
desc.Add(": ");
}
desc.Add(msg);
SendOut(new detail::ReporterMessage(detail::ReporterMessage::ERROR, this, desc.Description()));
} }
void MsgThread::FatalError(const char* 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, SendOut(new detail::ReporterMessage(detail::ReporterMessage::FATAL_ERROR, this,
desc.Description())); BuildMsgWithLocation(msg)));
} }
void MsgThread::FatalErrorWithCore(const char* 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, SendOut(new detail::ReporterMessage(detail::ReporterMessage::FATAL_ERROR_WITH_CORE, this,
desc.Description())); BuildMsgWithLocation(msg)));
} }
void MsgThread::InternalWarning(const char* 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, SendOut(new detail::ReporterMessage(detail::ReporterMessage::INTERNAL_WARNING, this,
desc.Description())); BuildMsgWithLocation(msg)));
} }
void MsgThread::InternalError(const char* msg) void MsgThread::InternalError(const char* msg)
{ {
// This one aborts immediately. fprintf(stderr, "internal error in thread: %s\n", BuildMsgWithLocation(msg).c_str());
ODesc desc;
if ( auto* location = GetLocationInfo() )
{
location->Describe(&desc);
desc.Add(": ");
}
desc.Add(msg);
fprintf(stderr, "internal error in thread: %s\n", desc.Description());
abort(); abort();
} }
@ -449,16 +401,7 @@ void MsgThread::InternalError(const char* msg)
void MsgThread::Debug(DebugStream stream, const char* msg) void MsgThread::Debug(DebugStream stream, const char* msg)
{ {
ODesc desc; SendOut(new detail::DebugMessage(stream, this, BuildMsgWithLocation(msg)));
if ( auto* location = GetLocationInfo() )
{
location->Describe(&desc);
desc.Add(": ");
}
desc.Add(msg);
SendOut(new detail::DebugMessage(stream, this, desc.Description()));
} }
#endif #endif

View file

@ -289,7 +289,7 @@ protected:
* @return A Location pointer containing the file location information, * @return A Location pointer containing the file location information,
* or nullptr if nothing is available. * or nullptr if nothing is available.
*/ */
virtual zeek::detail::Location* GetLocationInfo() const { return nullptr; } virtual const zeek::detail::Location* GetLocationInfo() const { return nullptr; }
private: private:
/** /**
@ -353,6 +353,8 @@ private:
*/ */
void Finished(); void Finished();
std::string BuildMsgWithLocation(const char* msg);
Queue<BasicInputMessage*> queue_in; Queue<BasicInputMessage*> queue_in;
Queue<BasicOutputMessage*> queue_out; Queue<BasicOutputMessage*> queue_out;