logging: Avoid repeated writer name lookups for plugin hooks

If a plugin provides a write hook, the invocation for HookLogWrite() would
redo looking up the writer's name from the enum value and instantiating
a new std::string instance for every write. Avoid doing this.
This commit is contained in:
Arne Welzel 2025-01-14 10:41:34 +01:00
parent 927a06b9ab
commit 345c4ca28a

View file

@ -204,6 +204,7 @@ struct Manager::Filter {
string path; string path;
Val* path_val = nullptr; Val* path_val = nullptr;
EnumVal* writer = nullptr; EnumVal* writer = nullptr;
std::string writer_name;
TableVal* config = nullptr; TableVal* config = nullptr;
TableVal* field_name_map = nullptr; TableVal* field_name_map = nullptr;
string scope_sep; string scope_sep;
@ -882,6 +883,7 @@ bool Manager::AddFilter(EnumVal* id, RecordVal* fval) {
filter->policy = policy ? policy->AsFunc() : stream->policy; filter->policy = policy ? policy->AsFunc() : stream->policy;
filter->path_func = path_func ? path_func->AsFunc() : nullptr; filter->path_func = path_func ? path_func->AsFunc() : nullptr;
filter->writer = writer->Ref()->AsEnumVal(); filter->writer = writer->Ref()->AsEnumVal();
filter->writer_name = writer->GetType()->AsEnumType()->Lookup(writer->InternalInt());
filter->local = log_local->AsBool(); filter->local = log_local->AsBool();
filter->remote = log_remote->AsBool(); filter->remote = log_remote->AsBool();
filter->interval = interv->AsInterval(); filter->interval = interv->AsInterval();
@ -1126,10 +1128,9 @@ bool Manager::WriteToFilters(const Manager::Stream* stream, zeek::RecordValPtr c
if ( ! wi->hook_initialized ) { if ( ! wi->hook_initialized ) {
wi->hook_initialized = true; wi->hook_initialized = true;
PLUGIN_HOOK_VOID(HOOK_LOG_INIT, HookLogInit(filter->writer->GetType()->AsEnumType()->Lookup( PLUGIN_HOOK_VOID(HOOK_LOG_INIT,
filter->writer->InternalInt()), HookLogInit(filter->writer_name, wi->instantiating_filter, filter->local,
wi->instantiating_filter, filter->local, filter->remote, filter->remote, *info, filter->num_fields, filter->fields));
*info, filter->num_fields, filter->fields));
} }
} }
@ -1161,10 +1162,8 @@ bool Manager::WriteToFilters(const Manager::Stream* stream, zeek::RecordValPtr c
for ( auto& v : rec ) for ( auto& v : rec )
vals.emplace_back(&v); vals.emplace_back(&v);
bool res = bool res = zeek::plugin_mgr->HookLogWrite(filter->writer_name, filter->name, *info, filter->num_fields,
zeek::plugin_mgr->HookLogWrite(filter->writer->GetType()->AsEnumType()->Lookup( filter->fields, &vals[0]);
filter->writer->InternalInt()),
filter->name, *info, filter->num_fields, filter->fields, &vals[0]);
if ( ! res ) { if ( ! res ) {
DBG_LOG(DBG_LOGGING, "Hook prevented writing to filter '%s' on stream '%s'", filter->name.c_str(), DBG_LOG(DBG_LOGGING, "Hook prevented writing to filter '%s' on stream '%s'", filter->name.c_str(),
stream->name.c_str()); stream->name.c_str());
@ -1684,9 +1683,8 @@ WriterFrontend* Manager::CreateWriter(EnumVal* id, EnumVal* writer, WriterBacken
if ( ! from_remote ) { if ( ! from_remote ) {
winfo->hook_initialized = true; winfo->hook_initialized = true;
PLUGIN_HOOK_VOID(HOOK_LOG_INIT, PLUGIN_HOOK_VOID(HOOK_LOG_INIT, HookLogInit(writer_name, instantiating_filter, local, remote, *winfo->info,
HookLogInit(writer->GetType()->AsEnumType()->Lookup(writer->InternalInt()), num_fields, fields));
instantiating_filter, local, remote, *winfo->info, num_fields, fields));
} }
InstallRotationTimer(winfo); InstallRotationTimer(winfo);