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