Skip to content

Commit

Permalink
fixup! SILKit-1607: Fix PR requests
Browse files Browse the repository at this point in the history
  • Loading branch information
VLukasBecker committed Oct 30, 2024
1 parent f00db47 commit c94e0fe
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 85 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
40 changes: 39 additions & 1 deletion SilKit/source/core/internal/MessageBuffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ class MessageBuffer
template <typename ValueT>
inline MessageBuffer& operator>>(std::vector<ValueT>& vector);
// --------------------------------------------------------------------------------
// std::vector<std::pair<std::string, std::string>>
inline MessageBuffer& operator<<(const std::vector<std::pair<std::string, std::string>>& msg);
inline MessageBuffer& operator>>(std::vector<std::pair<std::string, std::string>>& updatedMsg);
// --------------------------------------------------------------------------------
// Util::SharedVector<T>
template <typename ValueT>
inline MessageBuffer& operator<<(const Util::SharedVector<ValueT>& sharedData);
Expand Down Expand Up @@ -626,12 +630,46 @@ inline MessageBuffer& MessageBuffer::operator>>(std::unordered_map<std::string,
}
if (numElements != tmp.size())
{
throw SilKitError("MessageBuffer unable to deserialize std::unordered_map<std::string, std::string>");
throw SilKitError("MessageBuffer unable to deserialize std::vector<std::pair<std::string, std::string>>");
}
updatedMsg = std::move(tmp);
return *this;
}


inline MessageBuffer& MessageBuffer::operator<<(const std::vector<std::pair<std::string, std::string>>& msg)
{
*this << static_cast<uint32_t>(msg.size());
for (auto&& kv : msg)
{
*this << kv.first << kv.second;
}
return *this;
}


inline MessageBuffer& MessageBuffer::operator>>(std::vector<std::pair<std::string, std::string>>& updatedMsg)
{
std::vector<std::pair<std::string, std::string>> tmp; // do not modify updatedMsg until we validated the input
uint32_t numElements{0};
*this >> numElements;

for (auto i = 0u; i < numElements; i++)
{
std::string key;
std::string value;
*this >> key >> value;
tmp.push_back({std::move(key), std::move(value)});
}
if (numElements != tmp.size())
{
throw SilKitError("MessageBuffer unable to deserialize std::vector<std::pair<std::string, std::string>>");
}
updatedMsg = std::move(tmp);
return *this;
}


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

Expand Down
37 changes: 19 additions & 18 deletions SilKit/source/core/internal/ServiceDescriptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include <string>
#include <sstream>
#include <map>
#include <unordered_map>

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

inline Core::EndpointAddress to_endpointAddress() const;

Expand Down Expand Up @@ -308,45 +308,46 @@ std::string ServiceDescriptor::to_string() const
}


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

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

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

switch (GetServiceType())
{
case ServiceType::Link:
kv.insert({"NetworkType", Config::to_string(GetNetworkType())});
kv.insert({"NetworkName", GetNetworkName()});
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("supplementalData.size() > 0");
throw LogicError(
"ServiceDescriptor::to_keyValues() failed: No controller type defined in supplemental data.");
}
kv.insert({"ControllerTypeName", controllerTypeName});
kv.insert({"NetworkType", Config::to_string(GetNetworkType())});
kv.insert({"NetworkName", GetNetworkName()});
kv.insert({"ServiceName", GetServiceName()});
kv.push_back({Keys::controllerTypeName, controllerTypeName});
kv.push_back({Keys::networkName, GetNetworkName()});
kv.push_back({Keys::serviceName, GetServiceName()});
break;
case ServiceType::InternalController:
kv.insert({"ServiceName", GetServiceName()});
kv.push_back({Keys::serviceName, GetServiceName()});
break;
case ServiceType::Undefined:
kv.insert({"NetworkName", GetNetworkName()});
kv.insert({"ServiceName", GetServiceName()});
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
1 change: 1 addition & 0 deletions SilKit/source/services/logging/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ add_library(O_SilKit_Services_Logging OBJECT

LoggingSerdes.hpp
LoggingSerdes.cpp
StructuredLoggingKeys.hpp
)

target_link_libraries(O_SilKit_Services_Logging
Expand Down
8 changes: 3 additions & 5 deletions SilKit/source/services/logging/ILoggerInternal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include "silkit/services/logging/ILogger.hpp"

#include "StructuredLoggingKeys.hpp"

#include "SilKitFmtFormatters.hpp"
#include "fmt/format.h"
#include <unordered_map>
#include <string>


Expand Down Expand Up @@ -95,15 +93,15 @@ class LoggerMessage
template<typename Key, typename Value>
void SetKeyValue(Key&& key, Value&& value)
{
_keyValues[std::forward<Key>(key)] = std::forward<Value>(value);
_keyValues.push_back({std::forward<Key>(key), std::forward<Value>(value)});
}

auto GetLevel() const -> Level
{
return _level;
}

auto GetKeyValues() const -> const std::unordered_map<std::string, std::string>&
auto GetKeyValues() const -> const std::vector<std::pair<std::string, std::string>>&
{
return _keyValues;
}
Expand Down Expand Up @@ -135,7 +133,7 @@ class LoggerMessage
ILoggerInternal* _logger;
Level _level;
std::string _msg;
std::unordered_map<std::string, std::string> _keyValues;
std::vector<std::pair<std::string, std::string>> _keyValues;
};


Expand Down
14 changes: 7 additions & 7 deletions SilKit/source/services/logging/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include <chrono>
#include <iomanip>
#include <sstream>
#include <unordered_map>
#include <string>

#include "Logger.hpp"
Expand Down Expand Up @@ -53,13 +52,13 @@ class LoggerMessage;
struct SimpleLogMessage
{
const std::string& msg;
const std::unordered_map<std::string, std::string>& kv;
const std::vector<std::pair<std::string, std::string>>& kv;
};

struct JsonLogMessage
{
const std::string& msg;
const std::unordered_map<std::string, std::string>& kv;
const std::vector<std::pair<std::string, std::string>>& kv;
};

struct JsonString
Expand Down Expand Up @@ -90,12 +89,12 @@ class epoch_formatter_flag : public spdlog::custom_flag_formatter
};


std::string KeyValuesToSimpleString(const std::unordered_map<std::string, std::string>& input)
std::string KeyValuesToSimpleString(const std::vector<std::pair<std::string, std::string>>& input)
{
std::string result;
result.reserve(input.size() * 2);

std::unordered_map<std::string, std::string>::const_iterator it = input.begin();
std::vector<std::pair<std::string, std::string>>::const_iterator it = input.begin();

while (it != input.end())
{
Expand All @@ -109,12 +108,12 @@ std::string KeyValuesToSimpleString(const std::unordered_map<std::string, std::s
return result;
}

std::string KeyValuesToJsonString(const std::unordered_map<std::string, std::string>& input)
std::string KeyValuesToJsonString(const std::vector<std::pair<std::string, std::string>>& input)
{
std::string result;
result.reserve(input.size() * 2);

std::unordered_map<std::string, std::string>::const_iterator it = input.begin();
std::vector<std::pair<std::string, std::string>>::const_iterator it = input.begin();
result.append("{");
while (it != input.end())
{
Expand All @@ -132,6 +131,7 @@ std::string KeyValuesToJsonString(const std::unordered_map<std::string, std::str
}



template <>
struct fmt::formatter<SilKit::Services::Logging::SimpleLogMessage>
{
Expand Down
11 changes: 6 additions & 5 deletions SilKit/source/services/logging/MessageTracing.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,15 @@ void TraceRx(Logging::ILoggerInternal* logger, const Core::IServiceEndpoint* add
{
lm.SetKeyValue(pair.first, pair.second);
}
lm.SetKeyValue(Logging::Keys::MSG, (fmt::format("{}", msg)));
lm.SetKeyValue(Logging::Keys::from, from.GetParticipantName());
lm.SetKeyValue(Logging::Keys::msg, (fmt::format("{}", msg)));

auto virtualTimeStamp = GetTimestamp(msg);
if (virtualTimeStamp != std::chrono::nanoseconds::duration::min())
{
lm.SetKeyValue(Logging::Keys::VIRTUAL_TIME_NS, (fmt::format("{}", virtualTimeStamp.count())));
lm.SetKeyValue(Logging::Keys::virtualTimeNS, (fmt::format("{}", virtualTimeStamp.count())));
}
lm.SetKeyValue(Logging::Keys::FROM, from.GetParticipantName());

lm.Dispatch();
}

Expand All @@ -82,12 +83,12 @@ void TraceTx(Logging::ILoggerInternal* logger, const Core::IServiceEndpoint* add
lm.SetKeyValue(pair.first, pair.second);
}

lm.SetKeyValue(Logging::Keys::MSG, (fmt::format("{}", msg)));
lm.SetKeyValue(Logging::Keys::msg, (fmt::format("{}", msg)));

auto virtualTimeStamp = GetTimestamp(msg);
if (virtualTimeStamp != std::chrono::nanoseconds::duration::min())
{
lm.SetKeyValue(Logging::Keys::VIRTUAL_TIME_NS, (fmt::format("{}", virtualTimeStamp.count())));
lm.SetKeyValue(Logging::Keys::virtualTimeNS, (fmt::format("{}", virtualTimeStamp.count())));
}
lm.Dispatch();
}
Expand Down
32 changes: 18 additions & 14 deletions SilKit/source/services/logging/StructuredLoggingKeys.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,24 @@ namespace Services {
namespace Logging {
namespace Keys {

const std::string VIRTUAL_TIME_NS{"VirtualTimeNS"};
const std::string MSG{"msg"};
const std::string FROM{"From"};

const std::string WAITING_TIME{"WaitingTime"};
const std::string EXECUTION_TIME{"ExecutionTime"};

const std::string PARTICIPANT_NAME{"ParticipantName"};
const std::string NEW_PARTICIPANT_STATE{"NewParticipantState"};
const std::string OLD_PARTICIPANT_STATE{"OldParticipantState"};
const std::string ENTER_TIME{"EnterTime"};
const std::string ENTER_REASON{"EnterReason"};


const std::string virtualTimeNS{"VirtualTimeNS"};
const std::string msg{"Msg"};
const std::string from{"From"};

const std::string waitingTime{"WaitingTime"};
const std::string executionTime{"ExecutionTime"};

const std::string participantName{"ParticipantName"};
const std::string newParticipantState{"NewParticipantState"};
const std::string oldParticipantState{"OldParticipantState"};
const std::string enterTime{"EnterTime"};
const std::string enterReason{"EnterReason"};

const std::string serviceType{"ServiceType"};
const std::string serviceName{"ServiceName"};
const std::string networkType{"NetworkType"};
const std::string networkName{"NetworkName"};
const std::string controllerTypeName{"ControllerTypeName"};


} // namespace Keys
Expand Down
Loading

0 comments on commit c94e0fe

Please sign in to comment.