Skip to content

Commit

Permalink
dev: Usage of InternalLogger and key values (#135)
Browse files Browse the repository at this point in the history
* SILKit-1607: Usage of InternalLogger and key values
  • Loading branch information
VLukasBecker authored Nov 8, 2024
1 parent 9bd7945 commit b4516f7
Show file tree
Hide file tree
Showing 30 changed files with 502 additions and 198 deletions.
15 changes: 8 additions & 7 deletions SilKit/source/core/internal/LoggingDatatypesInternal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include <string>
#include <sstream>
#include <ostream>
#include <unordered_map>
#include <vector>

#include "silkit/services/logging/LoggingDatatypes.hpp"
#include "silkit/services/logging/string_utils.hpp"
Expand Down Expand Up @@ -54,7 +54,7 @@ struct LogMsg
log_clock::time_point time;
SourceLoc source;
std::string payload;
std::unordered_map<std::string, std::string> keyValues;
std::vector<std::pair<std::string, std::string>> keyValues;
};

inline bool operator==(const SourceLoc& lhs, const SourceLoc& rhs);
Expand All @@ -67,8 +67,8 @@ inline std::string to_string(const LogMsg& msg);
inline std::ostream& operator<<(std::ostream& out, const LogMsg& msg);


inline std::string to_string(const std::unordered_map<std::string, std::string>& kv);
inline std::ostream& operator<<(std::ostream& out, const std::unordered_map<std::string, std::string>& kv);
inline std::string to_string(const std::vector<std::pair<std::string, std::string>>& kv);
inline std::ostream& operator<<(std::ostream& out, const std::vector<std::pair<std::string, std::string>>& kv);

// ================================================================================
// Inline Implementations
Expand Down Expand Up @@ -98,15 +98,16 @@ std::ostream& operator<<(std::ostream& out, const SourceLoc& sourceLoc)
<< "line=" << sourceLoc.line << ", funcname={\"" << sourceLoc.funcname << "\"}";
}

inline std::string to_string(const std::unordered_map<std::string, std::string>& kv)

inline std::string to_string(const std::vector<std::pair<std::string, std::string>>& kv)
{
std::stringstream outStream;
outStream << kv;
return outStream.str();
}


inline std::ostream& operator<<(std::ostream& out, const std::unordered_map<std::string, std::string>& kv)
inline std::ostream& operator<<(std::ostream& out, const std::vector<std::pair<std::string, std::string>>& kv)
{
std::string result;
result.reserve(kv.size() * 2);
Expand All @@ -115,7 +116,7 @@ inline std::ostream& operator<<(std::ostream& out, const std::unordered_map<std:
{
result.append(", kv: ");

std::unordered_map<std::string, std::string>::const_iterator it = kv.begin();
std::vector<std::pair<std::string, std::string>>::const_iterator it = kv.begin();
result.append("{");
while (it != kv.end())
{
Expand Down
21 changes: 21 additions & 0 deletions SilKit/source/core/internal/MessageBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ class MessageBuffer
template <typename ValueT, size_t SIZE>
inline MessageBuffer& operator>>(std::array<ValueT, SIZE>& array);
// --------------------------------------------------------------------------------
// std::pair<T, U>
template <typename T, typename U>
inline MessageBuffer& operator<<(const std::pair<T, U>& pair);
template <typename T, typename U>
inline MessageBuffer& operator>>(std::pair<T, U>& pair);
// --------------------------------------------------------------------------------
// std::chrono::duration<Rep, Period> and system_clock::time_point
template <class Rep, class Period>
inline MessageBuffer& operator<<(std::chrono::duration<Rep, Period> duration);
Expand Down Expand Up @@ -632,6 +638,21 @@ inline MessageBuffer& MessageBuffer::operator>>(std::unordered_map<std::string,
return *this;
}

template <typename T, typename U>
inline MessageBuffer& MessageBuffer::operator<<(const std::pair<T, U>& pair)
{
*this << pair.first << pair.second;
return *this;
}

template <typename T, typename U>
inline MessageBuffer& MessageBuffer::operator>>(std::pair<T, U>& pair)
{
*this >> pair.first >> pair.second;
return *this;
}


// --------------------------------------------------------------------------------
// Uuid

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

#include "StructuredLoggingKeys.hpp"
#include "ServiceConfigKeys.hpp"
#include "Configuration.hpp"
#include "EndpointAddress.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::vector<std::pair<std::string, std::string>> to_keyValues() const;

inline Core::EndpointAddress to_endpointAddress() const;

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


std::vector<std::pair<std::string, std::string>> ServiceDescriptor::to_keyValues() const
{
namespace Keys = SilKit::Services::Logging::Keys;

std::vector<std::pair<std::string, std::string>> kv;
std::string controllerTypeName;
std::stringstream ss;

kv.push_back({Keys::participantName, GetParticipantName()});

switch (GetServiceType())
{
case ServiceType::Link:
kv.push_back({Keys::networkType, Config::to_string(GetNetworkType())});
kv.push_back({Keys::networkName, GetNetworkName()});
break;
case ServiceType::Controller:
case ServiceType::SimulatedController:
if (!GetSupplementalDataItem(SilKit::Core::Discovery::controllerType, controllerTypeName))
{
throw LogicError(
"ServiceDescriptor::to_keyValues() failed: No controller type defined in supplemental data.");
}
kv.push_back({Keys::controllerTypeName, controllerTypeName});
kv.push_back({Keys::networkName, GetNetworkName()});
kv.push_back({Keys::serviceName, GetServiceName()});
break;
case ServiceType::InternalController:
kv.push_back({Keys::serviceName, GetServiceName()});
break;
case ServiceType::Undefined:
kv.push_back({Keys::networkName, GetNetworkName()});
kv.push_back({Keys::serviceName, GetServiceName()});
break;
}

kv.push_back({Keys::serviceType, SilKit::Core::to_string(GetServiceType())});
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
Loading

0 comments on commit b4516f7

Please sign in to comment.