Skip to content

Commit

Permalink
Merge pull request #45 from centreon/msg_inheritance_reader_exception
Browse files Browse the repository at this point in the history
enh(reader_exception) : shutdown exception updated, replaced msg by m…
  • Loading branch information
jbrouze committed Jul 7, 2020
2 parents bba177c + a6d3f7f commit 476d47a
Show file tree
Hide file tree
Showing 23 changed files with 92 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#ifndef CCB_CONFIGURATION_READER_EXCEPTION_HH
#define CCB_CONFIGURATION_READER_EXCEPTION_HH

#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/exceptions/msg_fmt.hh"

CCB_BEGIN()

Expand All @@ -32,11 +32,15 @@ namespace configuration {
*
* Reader_exception.
*/
class reader_exception : public exceptions::msg {
class reader_exception : public com::centreon::exceptions::msg_fmt {
public:
reader_exception();
reader_exception() = delete;

template <typename... Args>
explicit reader_exception(std::string const& str, const Args&... args)
: msg_fmt(str, args...) {}
reader_exception(const reader_exception&);
~reader_exception() throw();
~reader_exception() noexcept {}
};
} // namespace configuration
} // namespace bam
Expand Down
11 changes: 6 additions & 5 deletions include/com/centreon/broker/misc/tokenizer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@
#include <cstring>
#include <sstream>
#include <string>
#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/exceptions/msg_fmt.hh"
#include "com/centreon/broker/logging/logging.hh"
#include "com/centreon/broker/namespace.hh"

CCB_BEGIN()

using namespace com::centreon::exceptions;
namespace misc {
template <typename T>
/**
Expand Down Expand Up @@ -75,15 +76,15 @@ class tokenizer {
arg = std::string(_index, position - _index);

if (arg.empty() && !optional)
throw(exceptions::msg() << "expected non optional argument " << _pos
<< " empty or not found");
throw msg_fmt("expected non optional argument {} empty or not found",
_pos);

std::stringstream ss;
ss << arg;
T ret = from_string_stream<T>(ss);
if (ss.fail())
throw(exceptions::msg() << "can't convert '" << ss.str()
<< "' to expected type for pos " << _pos);
throw msg_fmt(
"can't convert '{}' to expected type for pos {}", ss.str(), _pos);

_index = *position ? position + 1 : position;
++_pos;
Expand Down
2 changes: 1 addition & 1 deletion include/com/centreon/exceptions/corruption.hh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class corruption : public msg_fmt {
public:
template <typename... Args>
explicit corruption(std::string const& str, const Args&... args)
: msg_fmt(fmt::format(str, args...)) {}
: msg_fmt(str, args...) {}

corruption() = delete;
~corruption() noexcept {}
Expand Down
7 changes: 0 additions & 7 deletions include/com/centreon/exceptions/shutdown.hh
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,9 @@ class shutdown : public msg_fmt {
template <typename... Args>
explicit shutdown(std::string const& str, const Args&... args)
: msg_fmt(str, args...) {}

shutdown() = delete;
~shutdown() noexcept {}
shutdown& operator=(const shutdown&) = delete;

/**
* Insert data in message.
*
* @param[in] t Data to insert.
*/
};
} // namespace exceptions

Expand Down
1 change: 0 additions & 1 deletion src/20-bam/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ add_library(20-bam SHARED
${CMAKE_SOURCE_DIR}/src/20-bam/configuration/bool_expression.cc
${CMAKE_SOURCE_DIR}/src/20-bam/configuration/kpi.cc
${CMAKE_SOURCE_DIR}/src/20-bam/configuration/meta_service.cc
${CMAKE_SOURCE_DIR}/src/20-bam/configuration/reader_exception.cc
${CMAKE_SOURCE_DIR}/src/20-bam/configuration/reader_v2.cc
${CMAKE_SOURCE_DIR}/src/20-bam/configuration/state.cc
${CMAKE_SOURCE_DIR}/src/20-bam/connector.cc
Expand Down
70 changes: 36 additions & 34 deletions src/20-bam/configuration/reader_v2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include "com/centreon/broker/multiplexing/publisher.hh"
#include "com/centreon/broker/mysql.hh"
#include "com/centreon/broker/time/timeperiod.hh"
#include "com/centreon/exceptions/msg_fmt.hh"

using namespace com::centreon::exceptions;
using namespace com::centreon::broker;
Expand Down Expand Up @@ -196,8 +195,8 @@ void reader_v2::_load(state::kpis& kpis) {
throw;
}
catch (std::exception const& e) {
throw(reader_exception()
<< "BAM: could not retrieve KPI configuration from DB: " << e.what());
throw reader_exception(
"BAM: could not retrieve KPI configuration from DB: {}", e.what());
}
}

Expand Down Expand Up @@ -258,8 +257,8 @@ void reader_v2::_load(state::bas& bas, bam::ba_svc_mapping& mapping) {
throw;
}
catch (std::exception const& e) {
throw(reader_exception()
<< "BAM: could not retrieve BA configuration from DB: " << e.what());
throw reader_exception(
"BAM: could not retrieve BA configuration from DB: {}", e.what());
}

// Load host_id/service_id of virtual BA services. All the associated
Expand Down Expand Up @@ -312,17 +311,16 @@ void reader_v2::_load(state::bas& bas, bam::ba_svc_mapping& mapping) {
throw;
}
catch (std::exception const& e) {
throw(
reader_exception() << "BAM: could not retrieve BA service IDs from DB: "
<< e.what());
throw reader_exception("BAM: could not retrieve BA service IDs from DB: {}",
e.what());
}

// Test for BA without service ID.
for (state::bas::const_iterator it(bas.begin()), end(bas.end()); it != end;
++it)
if (it->second.get_service_id() == 0)
throw(reader_exception() << "BAM: BA " << it->second.get_id()
<< " has no associated service");
throw reader_exception("BAM: BA {} has no associated service",
it->second.get_id());

return;
}
Expand Down Expand Up @@ -361,8 +359,9 @@ void reader_v2::_load(state::bool_exps& bool_exps) {
throw;
}
catch (std::exception const& e) {
throw(reader_exception() << "BAM: could not retrieve boolean expression "
<< "configuration from DB: " << e.what());
throw reader_exception(
"BAM: could not retrieve boolean expression configuration from DB: {}",
e.what());
}
}

Expand Down Expand Up @@ -399,8 +398,8 @@ void reader_v2::_load(state::meta_services& meta_services) {
throw;
}
catch (std::exception const& e) {
throw(reader_exception() << "BAM: could not retrieve meta-services: "
<< e.what());
throw reader_exception("BAM: could not retrieve meta-services: {}",
e.what());
}

// Load host_id/service_id of virtual meta-service services. All
Expand Down Expand Up @@ -445,9 +444,9 @@ void reader_v2::_load(state::meta_services& meta_services) {
throw;
}
catch (std::exception const& e) {
throw(reader_exception()
<< "BAM: could not retrieve meta-services' service IDs from DB: "
<< e.what());
throw reader_exception(
"BAM: could not retrieve meta-services' service IDs from DB: {}",
e.what());
}

// Check for meta-services without service ID.
Expand All @@ -458,7 +457,7 @@ void reader_v2::_load(state::meta_services& meta_services) {
// std::pair<std::string, std::string>
// svc(mapping.get_service(it->first));
// if (svc.first.empty() || svc.second.empty())
// throw (reader_exception() << "BAM: meta-service "
// throw reader_exception("BAM: meta-service "
// << it->first << " has no associated service");
}

Expand Down Expand Up @@ -486,10 +485,11 @@ void reader_v2::_load(state::meta_services& meta_services) {
storage_mysql.reset(new mysql(_storage_cfg));
}
catch (std::exception const& e) {
throw(reader_exception()
<< "BAM: could not initialize storage database to "
"retrieve metrics associated with some "
"meta-service: " << e.what());
throw reader_exception(
"BAM: could not initialize storage database to "
"retrieve metrics associated with some "
"meta-service: {}",
e.what());
}
std::promise<database::mysql_result> promise;
storage_mysql->run_query_and_get_result(query.str(), &promise);
Expand All @@ -499,9 +499,10 @@ void reader_v2::_load(state::meta_services& meta_services) {
it->second.add_metric(res.value_as_u32(0));
}
catch (std::exception const& e) {
throw(reader_exception()
<< "BAM: could not retrieve members of meta-service '"
<< it->second.get_name() << "': " << e.what());
throw reader_exception(
"BAM: could not retrieve members of meta-service '{}': {}",
it->second.get_name(),
e.what());
}
}
// Service list mode.
Expand All @@ -523,9 +524,10 @@ void reader_v2::_load(state::meta_services& meta_services) {
throw;
}
catch (std::exception const& e) {
throw(reader_exception()
<< "BAM: could not retrieve members of meta-service '"
<< it->second.get_name() << "': " << e.what());
throw reader_exception(
"BAM: could not retrieve members of meta-service '{}': {}",
it->second.get_name(),
e.what());
}
}
}
Expand Down Expand Up @@ -562,8 +564,8 @@ void reader_v2::_load(bam::hst_svc_mapping& mapping) {
throw;
}
catch (std::exception const& e) {
throw(reader_exception() << "BAM: could not retrieve host/service IDs: "
<< e.what());
throw reader_exception("BAM: could not retrieve host/service IDs: {}",
e.what());
}

try {
Expand All @@ -588,8 +590,8 @@ void reader_v2::_load(bam::hst_svc_mapping& mapping) {
}
}
catch (std::exception const& e) {
throw(reader_exception() << "BAM: could not retrieve known metrics: "
<< e.what());
throw reader_exception("BAM: could not retrieve known metrics: {}",
e.what());
}
}

Expand Down Expand Up @@ -869,7 +871,7 @@ void reader_v2::_load_dimensions() {
throw;
}
catch (std::exception const& e) {
throw(reader_exception() << "BAM: could not load some dimension table: "
<< e.what());
throw reader_exception("BAM: could not load some dimension table: {}",
e.what());
}
}
47 changes: 21 additions & 26 deletions src/50-tcp/connector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
#include <memory>
#include <sstream>

#include "com/centreon/broker/exceptions/msg.hh"
#include "com/centreon/exceptions/msg_fmt.hh"
#include "com/centreon/broker/log_v2.hh"
#include "com/centreon/broker/logging/logging.hh"
#include "com/centreon/broker/tcp/stream.hh"
#include "com/centreon/broker/tcp/tcp_async.hh"

using namespace com::centreon::exceptions;
using namespace com::centreon::broker;
using namespace com::centreon::broker::tcp;

Expand Down Expand Up @@ -65,8 +66,8 @@ void connector::connect_to(std::string const& host, unsigned short port) {
std::shared_ptr<io::stream> connector::open() {
// Launch connection process.
log_v2::tcp()->info("TCP: connecting to {0}:{1}", _host, _port);
logging::info(logging::high)
<< "TCP: connecting to " << _host << ":" << _port;
logging::info(logging::high) << "TCP: connecting to " << _host << ":"
<< _port;
std::string connection_name{_host + ":" + std::to_string(_port)};

std::shared_ptr<asio::ip::tcp::socket> sock;
Expand All @@ -91,30 +92,28 @@ std::shared_ptr<io::stream> connector::open() {
}

if (err) {
broker::exceptions::msg e;
log_v2::tcp()->error("TCP: could not connect to {0}:{1}",
_host, _port);
e << "TCP: could not connect to remote server '" << _host << ":" << _port
<< "': " << err.message();
throw e;
log_v2::tcp()->error("TCP: could not connect to {0}:{1}", _host, _port);
throw msg_fmt("TCP: could not connect to remote server '{}:{}': {}",
_host,
_port,
err.message());
}

asio::socket_base::keep_alive option{true};
sock->set_option(option);
} catch (std::system_error const& se) {
broker::exceptions::msg e;
log_v2::tcp()->error("TCP: could not resolve {0}:{1}", _host,
_port);
e << "TCP: could not resolve remote server '" << _host << ":" << _port
<< "': " << se.what();
throw e;
}
catch (std::system_error const& se) {
log_v2::tcp()->error("TCP: could not resolve {0}:{1}", _host, _port);
throw msg_fmt("TCP: could not resolve remote server '{}:{}': {}",
_host,
_port,
se.what());
}
tcp_async::instance().register_socket(*sock);

log_v2::tcp()->info("TCP: successfully connected to {}",
connection_name);
logging::info(logging::high)
<< "TCP: successfully connected to " << connection_name;
log_v2::tcp()->info("TCP: successfully connected to {}", connection_name);
logging::info(logging::high) << "TCP: successfully connected to "
<< connection_name;

// Return stream.
std::shared_ptr<stream> s(std::make_shared<stream>(sock, connection_name));
Expand All @@ -128,15 +127,11 @@ std::shared_ptr<io::stream> connector::open() {
*
* @param[in] secs Timeout in seconds.
*/
void connector::set_read_timeout(int secs) {
_read_timeout = secs;
}
void connector::set_read_timeout(int secs) { _read_timeout = secs; }

/**
* Set write timeout.
*
* @param[in] secs Timeout in seconds.
*/
void connector::set_write_timeout(int secs) {
_write_timeout = secs;
}
void connector::set_write_timeout(int secs) { _write_timeout = secs; }
3 changes: 2 additions & 1 deletion src/70-graphite/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ int stream::flush() {
bool stream::read(std::shared_ptr<io::data>& d, time_t deadline) {
(void)deadline;
d.reset();
throw exceptions::shutdown("cannot read from Graphite database");
throw com::centreon::exceptions::shutdown(
"cannot read from Graphite database");
return (true);
}

Expand Down
3 changes: 2 additions & 1 deletion src/70-influxdb/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ int stream::flush() {
bool stream::read(std::shared_ptr<io::data>& d, time_t deadline) {
(void)deadline;
d.reset();
throw exceptions::shutdown("cannot read from InfluxDB database");
throw com::centreon::exceptions::shutdown(
"cannot read from InfluxDB database");
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ccb_core/compression/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ bool stream::read(std::shared_ptr<io::data>& data, time_t deadline) {
(_rbuffer.data() + sizeof(int32_t))),
size);
}
catch (exceptions::corruption const& e) {
catch (corruption const& e) {
logging::debug(logging::medium) << e.what();
}
}
Expand Down Expand Up @@ -190,7 +190,7 @@ bool stream::read(std::shared_ptr<io::data>& data, time_t deadline) {
(void)e;
return false;
}
catch (exceptions::shutdown const& e) {
catch (shutdown const& e) {
_shutdown = true;
if (!_wbuffer.empty()) {
std::shared_ptr<io::raw> r(new io::raw);
Expand Down
Loading

0 comments on commit 476d47a

Please sign in to comment.