Skip to content

Commit

Permalink
SILKit-1607: Usage of InternalLogger and key values
Browse files Browse the repository at this point in the history
  • Loading branch information
VLukasBecker committed Oct 14, 2024
1 parent 0ed4a9f commit 47df8bd
Show file tree
Hide file tree
Showing 31 changed files with 270 additions and 88 deletions.
7 changes: 4 additions & 3 deletions Demos/Can/CanDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ int main(int argc, char** argv)
std::cout << "now=" << now << ", duration=" << duration << std::endl;
SendFrame(canController, logger);
std::this_thread::sleep_for(sleepTimePerTick);
},
},
5ms);
}
else
Expand All @@ -207,7 +207,8 @@ int main(int argc, char** argv)
[sleepTimePerTick](std::chrono::nanoseconds now, std::chrono::nanoseconds duration) {
std::cout << "now=" << now << ", duration=" << duration << std::endl;
std::this_thread::sleep_for(sleepTimePerTick);
}, 5ms);
},
5ms);
}

auto finalStateFuture = lifecycleService->StartLifecycle();
Expand Down Expand Up @@ -284,4 +285,4 @@ int main(int argc, char** argv)
}

return 0;
}
}
7 changes: 4 additions & 3 deletions Demos/PubSub/PubSubDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ int main(int argc, char** argv)
PublishData(gpsPublisher, temperaturePublisher);
}
std::this_thread::sleep_for(1s);
},
},
1s);
}
else
Expand All @@ -242,7 +242,8 @@ int main(int argc, char** argv)
auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(now);
std::cout << "now=" << nowMs.count() << "ms" << std::endl;
std::this_thread::sleep_for(1s);
}, 1s);
},
1s);
}

auto finalStateFuture = lifecycleService->StartLifecycle();
Expand Down Expand Up @@ -315,4 +316,4 @@ int main(int argc, char** argv)
}

return 0;
}
}
43 changes: 43 additions & 0 deletions SilKit/source/core/internal/ServiceDescriptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include <string>
#include <sstream>
#include <map>
#include <unordered_map>

#include "ServiceConfigKeys.hpp"
#include "Configuration.hpp"
Expand Down Expand Up @@ -70,6 +71,8 @@ class ServiceDescriptor
inline bool operator==(const ServiceDescriptor& rhs) const;
inline bool operator!=(const ServiceDescriptor& rhs) const;
inline std::string to_string() const;
inline std::unordered_map<std::string, std::string> to_keyValues() const;

inline Core::EndpointAddress to_endpointAddress() const;

public:
Expand Down Expand Up @@ -304,6 +307,46 @@ std::string ServiceDescriptor::to_string() const
return ss.str();
}


std::unordered_map<std::string, std::string> ServiceDescriptor::to_keyValues() const
{
std::unordered_map<std::string, std::string> kv;
std::string controllerTypeName;
std::stringstream ss;

kv.insert({"ParticipantName", GetParticipantName()});
kv.insert({"ServiceType", SilKit::Core::to_string(GetServiceType())});

switch (GetServiceType())
{
case ServiceType::Link:
kv.insert({"NetworkType", Config::to_string(GetNetworkType())});
kv.insert({"NetworkName", GetNetworkName()});
break;
case ServiceType::Controller:
case ServiceType::SimulatedController:
if (!GetSupplementalDataItem(SilKit::Core::Discovery::controllerType, controllerTypeName))
{
throw LogicError("supplementalData.size() > 0");
}
kv.insert({"ControllerTypeName", controllerTypeName});
kv.insert({"NetworkType", Config::to_string(GetNetworkType())});
kv.insert({"NetworkName", GetNetworkName()});
kv.insert({"ServiceName", GetServiceName()});
break;
case ServiceType::InternalController:
kv.insert({"ServiceName", GetServiceName()});
break;
case ServiceType::Undefined:
kv.insert({"NetworkName", GetNetworkName()});
kv.insert({"ServiceName", GetServiceName()});
break;
}
return kv;
}



EndpointAddress ServiceDescriptor::to_endpointAddress() const
{
return {GetParticipantId(), GetServiceId()};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ struct NullConnection
}

void SetLogger(Services::Logging::ILogger* /*logger*/) {}
void SetLoggerInternal(Services::Logging::ILoggerInternal* /*logger*/) {}
void SetTimeSyncService(Orchestration::TimeSyncService* /*timeSyncService*/) {}
void JoinSimulation(std::string /*registryUri*/) {}

Expand Down
7 changes: 3 additions & 4 deletions SilKit/source/core/participant/Participant_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,9 @@ Participant<SilKitConnectionT>::Participant(Config::ParticipantConfiguration par
// NB: do not create the _logger in the initializer list. If participantName is empty,
// this will cause a fairly unintuitive exception in spdlog.
_logger = std::make_unique<Services::Logging::Logger>(GetParticipantName(), _participantConfig.logging);

dynamic_cast<VSilKit::MetricsProcessor&>(*_metricsProcessor).SetLogger(*_logger);
dynamic_cast<VSilKit::MetricsManager&>(*_metricsManager).SetLogger(*_logger);
_connection.SetLogger(_logger.get());
_connection.SetLoggerInternal(_logger.get());

Logging::Info(_logger.get(), "Creating participant '{}' at '{}', SIL Kit version: {}", GetParticipantName(),
_participantConfig.middleware.registryUri, Version::StringImpl());
Expand Down Expand Up @@ -1317,7 +1316,7 @@ template <class SilKitConnectionT>
template <typename SilKitMessageT>
void Participant<SilKitConnectionT>::SendMsgImpl(const IServiceEndpoint* from, SilKitMessageT&& msg)
{
TraceTx(GetLogger(), from, msg);
TraceTx(GetLoggerInternal(), from, msg);
_connection.SendMsg(from, std::forward<SilKitMessageT>(msg));
}

Expand Down Expand Up @@ -1621,7 +1620,7 @@ template <typename SilKitMessageT>
void Participant<SilKitConnectionT>::SendMsgImpl(const IServiceEndpoint* from, const std::string& targetParticipantName,
SilKitMessageT&& msg)
{
TraceTx(GetLogger(), from, msg);
TraceTx(GetLoggerInternal(), from, msg);
_connection.SendMsg(from, targetParticipantName, std::forward<SilKitMessageT>(msg));
}

Expand Down
2 changes: 1 addition & 1 deletion SilKit/source/core/vasio/ConnectKnownParticipants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ ConnectKnownParticipants::ConnectKnownParticipants(IIoContext& ioContext, IConne
}


void ConnectKnownParticipants::SetLogger(SilKit::Services::Logging::ILogger& logger)
void ConnectKnownParticipants::SetLogger(SilKit::Services::Logging::ILoggerInternal& logger)
{
SILKIT_ASSERT(_logger == nullptr);
_logger = &logger;
Expand Down
4 changes: 2 additions & 2 deletions SilKit/source/core/vasio/ConnectKnownParticipants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class ConnectKnownParticipants
IConnectKnownParticipantsListener* _listener{nullptr};
ConnectKnownParticipantsSettings _settings;

SilKit::Services::Logging::ILogger* _logger{nullptr};
SilKit::Services::Logging::ILoggerInternal* _logger{nullptr};
std::promise<std::vector<VAsioPeerInfo>> _knownParticipants;

std::atomic<ConnectStage> _connectStage{ConnectStage::INVALID};
Expand All @@ -152,7 +152,7 @@ class ConnectKnownParticipants
IConnectKnownParticipantsListener& listener,
const ConnectKnownParticipantsSettings& settings);

void SetLogger(SilKit::Services::Logging::ILogger& logger);
void SetLogger(SilKit::Services::Logging::ILoggerInternal& logger);

void SetKnownParticipants(const std::vector<VAsioPeerInfo>& peerInfos);
void StartConnecting();
Expand Down
8 changes: 5 additions & 3 deletions SilKit/source/core/vasio/SilKitLink.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ class SilKitLink
public:
// ----------------------------------------
// Constructors and Destructor
SilKitLink(std::string name, Services::Logging::ILogger* logger,
SilKitLink(std::string name, Services::Logging::ILoggerInternal* logger,
Services::Orchestration::ITimeProvider* timeProvider);


public:
// ----------------------------------------
// Public methods
Expand Down Expand Up @@ -85,7 +86,7 @@ class SilKitLink
// ----------------------------------------
// private members
std::string _name;
Services::Logging::ILogger* _logger;
Services::Logging::ILoggerInternal* _logger;
Services::Orchestration::ITimeProvider* _timeProvider;

std::vector<ReceiverT*> _localReceivers;
Expand All @@ -96,8 +97,9 @@ class SilKitLink
// Inline Implementations
// ================================================================================
template <class MsgT>
SilKitLink<MsgT>::SilKitLink(std::string name, Services::Logging::ILogger* logger,
SilKitLink<MsgT>::SilKitLink(std::string name, Services::Logging::ILoggerInternal* logger,
Services::Orchestration::ITimeProvider* timeProvider)

: _name{std::move(name)}
, _logger{logger}
, _timeProvider{timeProvider}
Expand Down
2 changes: 1 addition & 1 deletion SilKit/source/core/vasio/Test_VAsioConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ class Test_VAsioConnection : public testing::Test
Test_VAsioConnection()
: _connection(nullptr, &_dummyMetricsManager, {}, "Test_VAsioConnection", 1, &_timeProvider)
{
_connection.SetLogger(&_dummyLogger);
_connection.SetLoggerInternal(&_dummyLogger);
}

Tests::MockLogger _dummyLogger;
Expand Down
16 changes: 15 additions & 1 deletion SilKit/source/core/vasio/VAsioConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,20 @@ VAsioConnection::~VAsioConnection()
}
}

void VAsioConnection::SetLogger(Services::Logging::ILogger* logger)
void VAsioConnection::SetLoggerInternal(Services::Logging::ILoggerInternal* logger)
{
_logger = logger;

_ioContext->SetLogger(*_logger);
_connectKnownParticipants.SetLogger(*_logger);
}

auto VAsioConnection::GetLoggerInternal() -> SilKit::Services::Logging::ILoggerInternal*
{
return _logger;
}
/*
void VAsioConnection::SetLogger(Services::Logging::ILoggerInternal* logger)
{
_logger = logger;
Expand All @@ -312,6 +325,7 @@ auto VAsioConnection::GetLogger() -> SilKit::Services::Logging::ILogger*
{
return _logger;
}
*/

auto VAsioConnection::PrepareAcceptorEndpointUris(const std::string& connectUri) -> std::vector<std::string>
{
Expand Down
11 changes: 7 additions & 4 deletions SilKit/source/core/vasio/VAsioConnection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,11 @@ class VAsioConnection
public:
// ----------------------------------------
// Public methods
void SetLogger(Services::Logging::ILogger* logger);
auto GetLogger() -> SilKit::Services::Logging::ILogger*;
// void SetLoggerSetLogger(Services::Logging::ILogger* logger);
// auto GetLogger() -> SilKit::Services::Logging::ILogger*;

void SetLoggerInternal(Services::Logging::ILoggerInternal* logger);
auto GetLoggerInternal() -> SilKit::Services::Logging::ILoggerInternal*;

void JoinSimulation(std::string registryUri);

Expand Down Expand Up @@ -411,7 +414,7 @@ class VAsioConnection
void SendMsgImpl(const IServiceEndpoint* from, SilKitMessageT&& msg)
{
const auto& key = from->GetServiceDescriptor().GetNetworkName();

// TraceTx(GetLoggerInternal(), from, msg);
auto& linkMap = std::get<SilKitServiceToLinkMap<std::decay_t<SilKitMessageT>>>(_serviceToLinkMap);
if (linkMap.count(key) < 1)
{
Expand Down Expand Up @@ -487,7 +490,7 @@ class VAsioConnection
SilKit::Config::ParticipantConfiguration _config;
std::string _participantName;
ParticipantId _participantId{0};
Services::Logging::ILogger* _logger{nullptr};
Services::Logging::ILoggerInternal* _logger{nullptr};
Services::Orchestration::ITimeProvider* _timeProvider{nullptr};

std::string _simulationName;
Expand Down
8 changes: 5 additions & 3 deletions SilKit/source/core/vasio/VAsioReceiver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include "MessageTracing.hpp"
#include "IServiceEndpoint.hpp"
#include "SerializedMessage.hpp"
#include "IloggerInternal.hpp"

namespace SilKit {
namespace Core {
Expand Down Expand Up @@ -74,7 +75,8 @@ class VAsioReceiver
// ----------------------------------------
// Constructors and Destructor
VAsioReceiver(VAsioMsgSubscriber subscriberInfo, std::shared_ptr<SilKitLink<MsgT>> link,
Services::Logging::ILogger* logger);
Services::Logging::ILoggerInternal* logger);


public:
// ----------------------------------------
Expand All @@ -95,7 +97,7 @@ class VAsioReceiver
// private members
VAsioMsgSubscriber _subscriptionInfo;
std::shared_ptr<SilKitLink<MsgT>> _link;
Services::Logging::ILogger* _logger;
Services::Logging::ILoggerInternal* _logger;
ServiceDescriptor _serviceDescriptor;
};

Expand All @@ -104,7 +106,7 @@ class VAsioReceiver
// ================================================================================
template <class MsgT>
VAsioReceiver<MsgT>::VAsioReceiver(VAsioMsgSubscriber subscriberInfo, std::shared_ptr<SilKitLink<MsgT>> link,
Services::Logging::ILogger* logger)
Services::Logging::ILoggerInternal* logger)
: _subscriptionInfo{std::move(subscriberInfo)}
, _link{link}
, _logger{logger}
Expand Down
4 changes: 2 additions & 2 deletions SilKit/source/core/vasio/VAsioRegistry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ VAsioRegistry::VAsioRegistry(std::shared_ptr<SilKit::Config::IParticipantConfigu

if (_registryEventListener != nullptr)
{
_registryEventListener->OnLoggerCreated(_logger.get());
_registryEventListener->OnLoggerInternalCreated(_logger.get());
}

dynamic_cast<VSilKit::MetricsProcessor&>(*_metricsProcessor).SetLogger(*_logger);
dynamic_cast<VSilKit::MetricsManager&>(*_metricsManager).SetLogger(*_logger);
_connection.SetLogger(_logger.get());
_connection.SetLoggerInternal(_logger.get());

_connection.RegisterMessageReceiver([this](IVAsioPeer* from, const ParticipantAnnouncement& announcement) {
this->OnParticipantAnnouncement(from, announcement);
Expand Down
5 changes: 3 additions & 2 deletions SilKit/source/core/vasio/VAsioRegistry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ struct IRegistryEventListener
{
virtual ~IRegistryEventListener() = default;

virtual void OnLoggerCreated(SilKit::Services::Logging::ILogger* logger) = 0;
// virtual void OnLoggerCreated(SilKit::Services::Logging::ILogger* logger) = 0;
virtual void OnLoggerInternalCreated(SilKit::Services::Logging::ILoggerInternal* logger) = 0;
virtual void OnRegistryUri(const std::string& registryUri) = 0;
virtual void OnParticipantConnected(const std::string& simulationName, const std::string& participantName) = 0;
virtual void OnParticipantDisconnected(const std::string& simulationName, const std::string& participantName) = 0;
Expand Down Expand Up @@ -124,7 +125,7 @@ class VAsioRegistry
private:
// ----------------------------------------
// private members
std::unique_ptr<Services::Logging::ILogger> _logger;
std::unique_ptr<Services::Logging::ILoggerInternal> _logger;
IRegistryEventListener* _registryEventListener{nullptr};
std::unordered_map<std::string, std::unordered_map<std::string, ConnectedParticipantInfo>> _connectedParticipants;
std::function<void()> _onAllParticipantsConnected;
Expand Down
2 changes: 1 addition & 1 deletion SilKit/source/core/vasio/io/IIoContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct IIoContext

virtual auto Resolve(const std::string& name) -> std::vector<std::string> = 0;

virtual void SetLogger(SilKit::Services::Logging::ILogger& logger) = 0;
virtual void SetLogger(SilKit::Services::Logging::ILoggerInternal& logger) = 0;
};


Expand Down
2 changes: 1 addition & 1 deletion SilKit/source/core/vasio/io/impl/AsioIoContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ auto AsioIoContext::Resolve(const std::string& name) -> std::vector<std::string>
}


void AsioIoContext::SetLogger(SilKit::Services::Logging::ILogger& logger)
void AsioIoContext::SetLogger(SilKit::Services::Logging::ILoggerInternal& logger)
{
SILKIT_TRACE_METHOD_(&logger, "({})", static_cast<const void*>(&logger));
_logger = &logger;
Expand Down
4 changes: 2 additions & 2 deletions SilKit/source/core/vasio/io/impl/AsioIoContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class AsioIoContext final : public IIoContext
{
AsioSocketOptions _socketOptions;
std::shared_ptr<asio::io_context> _asioIoContext;
SilKit::Services::Logging::ILogger* _logger{nullptr};
SilKit::Services::Logging::ILoggerInternal* _logger{nullptr};

public:
explicit AsioIoContext(const AsioSocketOptions& socketOptions);
Expand All @@ -44,7 +44,7 @@ class AsioIoContext final : public IIoContext
auto MakeLocalConnector(const std::string& path) -> std::unique_ptr<IConnector> override;
auto MakeTimer() -> std::unique_ptr<ITimer> override;
auto Resolve(const std::string& name) -> std::vector<std::string> override;
void SetLogger(SilKit::Services::Logging::ILogger& logger) override;
void SetLogger(SilKit::Services::Logging::ILoggerInternal& logger) override;
};


Expand Down
Loading

0 comments on commit 47df8bd

Please sign in to comment.