Skip to content

Commit

Permalink
enh(log_v2): new logging engine in centengine
Browse files Browse the repository at this point in the history
enh(engine/log_v2): new possibility to enable/disabled old/new logs
enh(log_v2): add broker sink log and unit test for new loggers
enh(log_v2): change some logs info on debug
enh(log_v2): duplicate hold log with new loggers
enh(log_v2): add new test robot
REFS: MON-11583
REFS: MON-11866
REFS: MON-11867
  • Loading branch information
rem31 committed Jan 18, 2022
1 parent 654bad4 commit 85b9728
Show file tree
Hide file tree
Showing 98 changed files with 6,770 additions and 2,116 deletions.
26 changes: 13 additions & 13 deletions centreon-clib/test/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,10 @@ TEST(ClibLogging, TempLoggerCtor) {
auto id = e.add(obj.get(), 3, 0);

temp_logger(0, 0) << "Centreon Clib test";
ASSERT_EQ(obj->get_nb_call(), 0);
ASSERT_EQ(obj->get_nb_call(), 0u);

temp_logger(1, 0) << "Centreon Clib test";
ASSERT_EQ(obj->get_nb_call(), 1);
ASSERT_EQ(obj->get_nb_call(), 1u);
e.remove(id);
}

Expand Down Expand Up @@ -524,7 +524,7 @@ TEST(ClibLogging, TempLoggerLogChar) {
auto id = e.add(obj.get(), 3, 0);

temp_logger(1, 0) << 'c';
ASSERT_EQ(obj->get_nb_call(), 1);
ASSERT_EQ(obj->get_nb_call(), 1u);
e.remove(id);
}

Expand All @@ -534,7 +534,7 @@ TEST(ClibLogging, TempLoggerLogDouble) {
auto id = e.add(obj.get(), 3, 0);

temp_logger(1, 0) << double(42.42);
ASSERT_EQ(obj->get_nb_call(), 1);
ASSERT_EQ(obj->get_nb_call(), 1u);
e.remove(id);
}

Expand All @@ -544,7 +544,7 @@ TEST(ClibLogging, TempLoggerLogInt) {
auto id = e.add(obj.get(), 3, 0);

temp_logger(1, 0) << int(42);
ASSERT_EQ(obj->get_nb_call(), 1);
ASSERT_EQ(obj->get_nb_call(), 1u);
e.remove(id);
}

Expand All @@ -554,7 +554,7 @@ TEST(ClibLogging, TempLoggerLogLong) {
auto id = e.add(obj.get(), 3, 0);

temp_logger(1, 0) << 42L;
ASSERT_EQ(obj->get_nb_call(), 1);
ASSERT_EQ(obj->get_nb_call(), 1u);
e.remove(id);
}

Expand All @@ -564,7 +564,7 @@ TEST(ClibLogging, TempLoggerLogLongLong) {
auto id = e.add(obj.get(), 3, 0);

temp_logger(1, 0) << 42LL;
ASSERT_EQ(obj->get_nb_call(), 1);
ASSERT_EQ(obj->get_nb_call(), 1u);
e.remove(id);
}

Expand All @@ -574,7 +574,7 @@ TEST(ClibLogging, TempLoggerLogPVoid) {
auto id = e.add(obj.get(), 3, 0);

temp_logger(1, 0) << obj.get();
ASSERT_EQ(obj->get_nb_call(), 1);
ASSERT_EQ(obj->get_nb_call(), 1u);
e.remove(id);
}

Expand All @@ -584,7 +584,7 @@ TEST(ClibLogging, TempLoggerLogStdString) {
auto id = e.add(obj.get(), 3, 0);

temp_logger(1, 0) << std::string("Centreon Clib test");
ASSERT_EQ(obj->get_nb_call(), 1);
ASSERT_EQ(obj->get_nb_call(), 1u);
e.remove(id);
}

Expand All @@ -594,7 +594,7 @@ TEST(ClibLogging, TempLoggerLogString) {
auto id = e.add(obj.get(), 3, 0);

temp_logger(1, 0) << "Centreon Clib test";
ASSERT_EQ(obj->get_nb_call(), 1);
ASSERT_EQ(obj->get_nb_call(), 1u);
e.remove(id);
}

Expand All @@ -605,7 +605,7 @@ TEST(ClibLogging, TempLoggerLogUint) {

unsigned int ui(42);
temp_logger(1, 0) << ui;
ASSERT_EQ(obj->get_nb_call(), 1);
ASSERT_EQ(obj->get_nb_call(), 1u);
e.remove(id);
}

Expand All @@ -616,7 +616,7 @@ TEST(ClibLogging, TempLoggerLogULong) {

unsigned long ul(42);
temp_logger(1, 0) << ul;
ASSERT_EQ(obj->get_nb_call(), 1);
ASSERT_EQ(obj->get_nb_call(), 1u);
e.remove(id);
}

Expand All @@ -627,6 +627,6 @@ TEST(ClibLogging, TempLoggerLogULongLong) {

unsigned long long ull(42);
temp_logger(1, 0) << ull;
ASSERT_EQ(obj->get_nb_call(), 1);
ASSERT_EQ(obj->get_nb_call(), 1u);
e.remove(id);
}
3 changes: 3 additions & 0 deletions centreon-engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ set(
"${SRC_DIR}/hostdependency.cc"
"${SRC_DIR}/hostescalation.cc"
"${SRC_DIR}/hostgroup.cc"
"${SRC_DIR}/log_v2.cc"
"${SRC_DIR}/macros.cc"
"${SRC_DIR}/nebmods.cc"
"${SRC_DIR}/notification.cc"
Expand Down Expand Up @@ -485,6 +486,7 @@ set(
"${INC_DIR}/com/centreon/engine/hostdependency.hh"
"${INC_DIR}/com/centreon/engine/hostescalation.hh"
"${INC_DIR}/com/centreon/engine/hostgroup.hh"
"${INC_DIR}/com/centreon/engine/log_v2.hh"
"${INC_DIR}/com/centreon/engine/logging.hh"
"${INC_DIR}/com/centreon/engine/macros.hh"
"${INC_DIR}/com/centreon/engine/nebcallbacks.hh"
Expand Down Expand Up @@ -544,6 +546,7 @@ target_link_libraries(cce_core ${nlohmann_json_LIBS}
${SOCKET_LIBRARIES}
centreon_clib
${fmt_LIBRARIES}
${spdlog_LIBS}
)

# centengine target.
Expand Down
52 changes: 50 additions & 2 deletions centreon-engine/inc/com/centreon/engine/configuration/state.hh
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,38 @@ class state {
void use_setpgid(bool value);
bool use_syslog() const noexcept;
void use_syslog(bool value);
bool log_v2_enabled() const noexcept;
void log_v2_enabled(bool value);
bool log_legacy_enabled() const noexcept;
void log_legacy_enabled(bool value);
std::string const& log_v2_logger() const noexcept;
void log_v2_logger(std::string const& value);
std::string const& log_level_functions() const noexcept;
void log_level_functions(std::string const& value);
std::string const& log_level_config() const noexcept;
void log_level_config(std::string const& value);
std::string const& log_level_events() const noexcept;
void log_level_events(std::string const& value);
std::string const& log_level_checks() const noexcept;
void log_level_checks(std::string const& value);
std::string const& log_level_notifications() const noexcept;
void log_level_notifications(std::string const& value);
std::string const& log_level_eventbroker() const noexcept;
void log_level_eventbroker(std::string const& value);
std::string const& log_level_external_command() const noexcept;
void log_level_external_command(std::string const& value);
std::string const& log_level_commands() const noexcept;
void log_level_commands(std::string const& value);
std::string const& log_level_downtimes() const noexcept;
void log_level_downtimes(std::string const& value);
std::string const& log_level_comments() const noexcept;
void log_level_comments(std::string const& value);
std::string const& log_level_macros() const noexcept;
void log_level_macros(std::string const& value);
std::string const& log_level_process() const noexcept;
void log_level_process(std::string const& value);
std::string const& log_level_runtime() const noexcept;
void log_level_runtime(std::string const& value);
std::string const& use_timezone() const noexcept;
void use_timezone(std::string const& value);
bool use_true_regexp_matching() const noexcept;
Expand Down Expand Up @@ -449,7 +481,7 @@ class state {
return (false);
(obj.*ptr)(val);
} catch (std::exception const& e) {
logger(logging::log_config_error, logging::basic) << e.what();
engine_logger(logging::log_config_error, logging::basic) << e.what();
return (false);
}
return (true);
Expand All @@ -462,7 +494,7 @@ class state {
try {
(obj.*ptr)(value);
} catch (std::exception const& e) {
logger(logging::log_config_error, logging::basic) << e.what();
engine_logger(logging::log_config_error, logging::basic) << e.what();
return (false);
}
return (true);
Expand Down Expand Up @@ -604,6 +636,22 @@ class state {
bool _use_retained_scheduling_info;
bool _use_setpgid;
bool _use_syslog;
bool _log_v2_enabled;
bool _log_legacy_enabled;
std::string _log_v2_logger;
std::string _log_level_functions;
std::string _log_level_config;
std::string _log_level_events;
std::string _log_level_checks;
std::string _log_level_notifications;
std::string _log_level_eventbroker;
std::string _log_level_external_command;
std::string _log_level_commands;
std::string _log_level_downtimes;
std::string _log_level_comments;
std::string _log_level_macros;
std::string _log_level_process;
std::string _log_level_runtime;
std::string _use_timezone;
bool _use_true_regexp_matching;
};
Expand Down
6 changes: 0 additions & 6 deletions centreon-engine/inc/com/centreon/engine/contact.hh
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,6 @@ std::shared_ptr<com::centreon::engine::contact> add_contact(
int retain_status_information,
int retain_nonstatus_information);

// bool operator==(
// com::centreon::engine::contact const& obj1,
// com::centreon::engine::contact const& obj2) throw ();
// bool operator!=(
// com::centreon::engine::contact const& obj1,
// com::centreon::engine::contact const& obj2) throw ();
std::ostream& operator<<(std::ostream& os,
com::centreon::engine::contact const& obj);
std::ostream& operator<<(std::ostream& os, contact_map_unsafe const& obj);
Expand Down
71 changes: 71 additions & 0 deletions centreon-engine/inc/com/centreon/engine/log_v2.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
** Copyright 2021 Centreon
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
**
** For more information : contact@centreon.com
*/
#ifndef CCE_LOG_V2_HH
#define CCE_LOG_V2_HH

#include <spdlog/common.h>
#include <spdlog/spdlog.h>

#include <array>
#include <map>
#include <memory>

#include "com/centreon/engine/configuration/state.hh"
#include "com/centreon/engine/namespace.hh"

CCE_BEGIN()
class log_v2 {
std::string _log_name;
std::shared_ptr<spdlog::logger> _config_log;
std::shared_ptr<spdlog::logger> _functions_log;
std::shared_ptr<spdlog::logger> _events_log;
std::shared_ptr<spdlog::logger> _checks_log;
std::shared_ptr<spdlog::logger> _notifications_log;
std::shared_ptr<spdlog::logger> _eventbroker_log;
std::shared_ptr<spdlog::logger> _external_command_log;
std::shared_ptr<spdlog::logger> _commands_log;
std::shared_ptr<spdlog::logger> _downtimes_log;
std::shared_ptr<spdlog::logger> _comments_log;
std::shared_ptr<spdlog::logger> _macros_log;
std::shared_ptr<spdlog::logger> _process_log;
std::shared_ptr<spdlog::logger> _runtime_log;

log_v2();
~log_v2() noexcept = default;

public:
void apply(const configuration::state& config);
static bool contains_level(const std::string& level_name);
static log_v2& instance();
static std::shared_ptr<spdlog::logger> functions();
static std::shared_ptr<spdlog::logger> config();
static std::shared_ptr<spdlog::logger> events();
static std::shared_ptr<spdlog::logger> checks();
static std::shared_ptr<spdlog::logger> notifications();
static std::shared_ptr<spdlog::logger> eventbroker();
static std::shared_ptr<spdlog::logger> external_command();
static std::shared_ptr<spdlog::logger> commands();
static std::shared_ptr<spdlog::logger> downtimes();
static std::shared_ptr<spdlog::logger> comments();
static std::shared_ptr<spdlog::logger> macros();
static std::shared_ptr<spdlog::logger> process();
static std::shared_ptr<spdlog::logger> runtime();
};
CCE_END()

#endif /* !CCE_LOG_V2_HH */
67 changes: 67 additions & 0 deletions centreon-engine/inc/com/centreon/engine/logging/broker_sink.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
** Copyright 2011-2021 Centreon
**
** This file is part of Centreon Engine.
**
** Centreon Engine is free software: you can redistribute it and/or
** modify it under the terms of the GNU General Public License version 2
** as published by the Free Software Foundation.
**
** Centreon Engine is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Centreon Engine. If not, see
** <http://www.gnu.org/licenses/>.
*/
#ifndef CCE_LOGGING_BROKER_SINK_HH
#define CCE_LOGGING_BROKER_SINK_HH
#include <spdlog/details/fmt_helper.h>
#include <spdlog/sinks/base_sink.h>
#include <mutex>
#include "com/centreon/engine/broker.hh"
#include "com/centreon/engine/logging/broker.hh"
#include "com/centreon/engine/logging/logger.hh"
#include "com/centreon/engine/namespace.hh"
#include "com/centreon/engine/nebstructs.hh"
#include "com/centreon/unique_array_ptr.hh"
#include "spdlog/details/null_mutex.h"
CCE_BEGIN()

namespace logging {
template <typename Mutex>
class broker_sink : public spdlog::sinks::base_sink<Mutex> {
protected:
void sink_it_(const spdlog::details::log_msg& msg) override {
// log_msg is a struct containing the log entry info like level, timestamp,
// thread id etc. msg.raw contains pre formatted log

// If needed (very likely but not mandatory), the sink formats the message
// before sending it to its final destination:
if (this->should_log(msg.level)) {
std::string message{fmt::to_string(msg.payload)};
nebstruct_log_data ds{.type = NEBTYPE_LOG_DATA,
.flags = NEBFLAG_NONE,
.attr = NEBATTR_NONE,
.timestamp = get_broker_timestamp(nullptr),
.entry_time = time(nullptr),
.data_type = logging::log_all,
.data = message.c_str()};

// Make callbacks.
neb_make_callbacks(NEBCALLBACK_LOG_DATA, &ds);
}
}

void flush_() override {}
};

using broker_sink_mt = broker_sink<std::mutex>;
using broker_sink_st = broker_sink<spdlog::details::null_mutex>;

} // namespace logging
CCE_END()

#endif // !CCE_LOGGING_BROKER_SINK_HH
2 changes: 1 addition & 1 deletion centreon-engine/inc/com/centreon/engine/logging/logger.hh
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ enum verbosity_level { basic = 0u, more = 1u, most = 2u };

CCE_END()

#define logger(type, verbose) \
#define engine_logger(type, verbose) \
for (unsigned int __com_centreon_engine_logging_define_ui(0); \
!__com_centreon_engine_logging_define_ui && \
com::centreon::logging::engine::instance().is_log(type, verbose); \
Expand Down
2 changes: 1 addition & 1 deletion centreon-engine/inc/com/centreon/engine/nebstructs.hh
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ typedef struct nebstruct_log_struct {

time_t entry_time;
int data_type;
char* data;
const char* data;
} nebstruct_log_data;

/* Module data structure. */
Expand Down
Loading

0 comments on commit 85b9728

Please sign in to comment.