Skip to content

Commit

Permalink
moved TEvAuditLog to the header file
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewstalin committed Dec 4, 2024
1 parent 00a2c2b commit 0f2caad
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 42 deletions.
35 changes: 33 additions & 2 deletions ydb/core/audit/audit_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <util/generic/string.h>
#include <util/generic/vector.h>

#include <ydb/core/base/events.h>
#include <ydb/core/protos/config.pb.h>
#include <ydb/library/actors/core/actor.h>
#include <ydb/library/actors/core/executor_thread.h>
Expand Down Expand Up @@ -40,10 +41,40 @@ namespace NKikimr::NAudit {

extern std::atomic<bool> AUDIT_LOG_ENABLED;

struct TEvAuditLog {
//
// Events declaration
//

enum EEvents {
EvBegin = EventSpaceBegin(TKikimrEvents::ES_YDB_AUDIT_LOG),

// Request actors
EvWriteAuditLog = EvBegin + 0,

EvEnd
};

static_assert(EvEnd <= EventSpaceEnd(TKikimrEvents::ES_YDB_AUDIT_LOG),
"expected EvEnd <= EventSpaceEnd(TKikimrEvents::ES_YDB_AUDIT_LOG)"
);

struct TEvWriteAuditLog : public NActors::TEventLocal<TEvWriteAuditLog, EvWriteAuditLog> {
TInstant Time;
TVector<std::pair<TString, TString>> Parts;

TEvWriteAuditLog(TInstant time, TVector<std::pair<TString, TString>>&& parts)
: Time(time)
, Parts(std::move(parts))
{}
};
};

using TAuditLogItemBuilder = TString(*)(const TEvAuditLog::TEvWriteAuditLog*);

void SendAuditLog(const NActors::TActorSystem* sys, TVector<std::pair<TString, TString>>&& parts);

// Registration of a function for converting audit events to a string in a specified format
template <typename T>
void RegisterAuditLogItemBuilder(NKikimrConfig::TAuditConfig::EFormat format, TString(*)(const T*));
void RegisterAuditLogItemBuilder(NKikimrConfig::TAuditConfig::EFormat format, TAuditLogItemBuilder builder);

} // namespace NKikimr::NAudit
43 changes: 3 additions & 40 deletions ydb/core/audit/audit_log_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include <ydb/library/actors/core/hfunc.h>
#include <ydb/library/services/services.pb.h>

#include <ydb/core/base/events.h>

#include "audit_log_service.h"
#include "audit_log.h"

Expand All @@ -32,38 +30,6 @@

namespace NKikimr::NAudit {

// TAuditLogActor
//

struct TEvAuditLog {
//
// Events declaration
//

enum EEvents {
EvBegin = EventSpaceBegin(TKikimrEvents::ES_YDB_AUDIT_LOG),

// Request actors
EvWriteAuditLog = EvBegin + 0,

EvEnd
};

static_assert(EvEnd <= EventSpaceEnd(TKikimrEvents::ES_YDB_AUDIT_LOG),
"expected EvEnd <= EventSpaceEnd(TKikimrEvents::ES_YDB_AUDIT_LOG)"
);

struct TEvWriteAuditLog : public NActors::TEventLocal<TEvWriteAuditLog, EvWriteAuditLog> {
TInstant Time;
TVector<std::pair<TString, TString>> Parts;

TEvWriteAuditLog(TInstant time, TVector<std::pair<TString, TString>>&& parts)
: Time(time)
, Parts(std::move(parts))
{}
};
};

void WriteLog(const TString& log, const TVector<THolder<TLogBackend>>& logBackends) {
for (auto& logBackend : logBackends) {
try {
Expand Down Expand Up @@ -122,18 +88,15 @@ TString GetTxtLog(const TEvAuditLog::TEvWriteAuditLog* ev) {
return ss.Str();
}


using TAuditLogItemBuilder = TString(*)(const TEvAuditLog::TEvWriteAuditLog*);

// Array of functions for converting TEvAuditLog::TEvWriteAuditLog events to a string.
// Indexing in the array occurs by the value of the NKikimrConfig::TAuditConfig::EFormat enumeration.
// The size of AuditLogItemBuilders must be equal to the maximum value of the NKikimrConfig::TAuditConfig::EFormat enumeration.
static std::vector<TAuditLogItemBuilder> AuditLogItemBuilders = { GetJsonLog, GetTxtLog, GetJsonLogCompatibleLog, nullptr };

// numbering enumeration starts with one
// numbering enumeration starts from one
static constexpr size_t DefaultAuditLogItemBuilder = static_cast<size_t>(NKikimrConfig::TAuditConfig::JSON) - 1;

template<>
void RegisterAuditLogItemBuilder<TEvAuditLog::TEvWriteAuditLog>(NKikimrConfig::TAuditConfig::EFormat format, TAuditLogItemBuilder builder) {
void RegisterAuditLogItemBuilder(NKikimrConfig::TAuditConfig::EFormat format, TAuditLogItemBuilder builder) {
size_t index = static_cast<size_t>(format);
if (index < AuditLogItemBuilders.size()) {
AuditLogItemBuilders[index] = builder;
Expand Down

0 comments on commit 0f2caad

Please sign in to comment.