mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
Plugin: Add hooks for log init and writing.
The two hooks being added are: void HookLogInit(const std::string& writer, const std::string& instantiating_filter, bool local, bool remote, const logging::WriterBackend::WriterInfo& info, int num_fields, const threading::Field* const* fields); which is called when a writer is being instantiated and contains information about the fields being logged, as well as bool HookLogWrite(const std::string& writer, const std::string& filter, const logging::WriterBackend::WriterInfo& info, int num_fields, const threading::Field* const* fields, threading::Value** vals); which is called for each log line being written by each writer. It contains all the data being written. The data can be changed in the function call and lines can be prevented from being written. This commit also fixes a few small problems with plugin hooks itself, and extends the tests that were already there, besides introducing tests for the added functionality.
This commit is contained in:
parent
2c2c9c9052
commit
684ea8aa37
16 changed files with 689 additions and 39 deletions
0
testing/btest/plugins/logging-hooks-plugin/.btest-ignore
Normal file
0
testing/btest/plugins/logging-hooks-plugin/.btest-ignore
Normal file
60
testing/btest/plugins/logging-hooks-plugin/src/Plugin.cc
Normal file
60
testing/btest/plugins/logging-hooks-plugin/src/Plugin.cc
Normal file
|
@ -0,0 +1,60 @@
|
|||
|
||||
#include "Plugin.h"
|
||||
|
||||
#include <Func.h>
|
||||
#include <Event.h>
|
||||
#include <Conn.h>
|
||||
#include <threading/Formatter.h>
|
||||
|
||||
namespace plugin { namespace Log_Hooks { Plugin plugin; } }
|
||||
|
||||
using namespace plugin::Log_Hooks;
|
||||
|
||||
plugin::Configuration Plugin::Configure()
|
||||
{
|
||||
round = 0;
|
||||
EnableHook(HOOK_LOG_INIT);
|
||||
EnableHook(HOOK_LOG_WRITE);
|
||||
|
||||
plugin::Configuration config;
|
||||
config.name = "Log::Hooks";
|
||||
config.description = "Exercises Log hooks";
|
||||
config.version.major = 1;
|
||||
config.version.minor = 0;
|
||||
return config;
|
||||
}
|
||||
|
||||
void Plugin::HookLogInit(const std::string& writer, const std::string& instantiating_filter, bool local, bool remote, const logging::WriterBackend::WriterInfo& info, int num_fields, const threading::Field* const* fields)
|
||||
{
|
||||
ODesc d;
|
||||
|
||||
d.Add("{");
|
||||
for ( int i=0; i < num_fields; i++ )
|
||||
{
|
||||
const threading::Field* f = fields[i];
|
||||
|
||||
if ( i > 0 )
|
||||
d.Add(", ");
|
||||
|
||||
d.Add(f->name);
|
||||
d.Add(" (");
|
||||
d.Add(f->TypeName());
|
||||
d.Add(")");
|
||||
}
|
||||
d.Add("}");
|
||||
|
||||
fprintf(stderr, "%.6f %-15s %s %d/%d %s\n", network_time, "| HookLogInit", info.path, local, remote, d.Description());
|
||||
}
|
||||
|
||||
bool Plugin::HookLogWrite(const std::string& writer, const std::string& filter, const logging::WriterBackend::WriterInfo& info, int num_fields, const threading::Field* const* fields, threading::Value** vals)
|
||||
{
|
||||
round++;
|
||||
if ( round == 1 ) // do not output line
|
||||
return false;
|
||||
else if ( round == 2 )
|
||||
vals[0]->val.int_val = 0;
|
||||
else if ( round == 3 )
|
||||
vals[1]->present = false;
|
||||
|
||||
return true;
|
||||
}
|
28
testing/btest/plugins/logging-hooks-plugin/src/Plugin.h
Normal file
28
testing/btest/plugins/logging-hooks-plugin/src/Plugin.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
|
||||
#ifndef BRO_PLUGIN_Log_Hooks
|
||||
#define BRO_PLUGIN_Log_Hooks
|
||||
|
||||
#include <plugin/Plugin.h>
|
||||
|
||||
namespace plugin {
|
||||
namespace Log_Hooks {
|
||||
|
||||
class Plugin : public ::plugin::Plugin
|
||||
{
|
||||
protected:
|
||||
void HookLogInit(const std::string& writer, const std::string& instantiating_filter, bool local, bool remote, const logging::WriterBackend::WriterInfo& info, int num_fields, const threading::Field* const* fields) override;
|
||||
bool HookLogWrite(const std::string& writer, const std::string& filter, const logging::WriterBackend::WriterInfo& info, int num_fields, const threading::Field* const* fields, threading::Value** vals) override;
|
||||
|
||||
// Overridden from plugin::Plugin.
|
||||
plugin::Configuration Configure() override;
|
||||
|
||||
private:
|
||||
int round;
|
||||
};
|
||||
|
||||
extern Plugin plugin;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue