Skip to content

Commit b4516f7

Browse files
authored
dev: Usage of InternalLogger and key values (#135)
* SILKit-1607: Usage of InternalLogger and key values
1 parent 9bd7945 commit b4516f7

30 files changed

Lines changed: 502 additions & 198 deletions

SilKit/source/core/internal/LoggingDatatypesInternal.hpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
2525
#include <string>
2626
#include <sstream>
2727
#include <ostream>
28-
#include <unordered_map>
28+
#include <vector>
2929

3030
#include "silkit/services/logging/LoggingDatatypes.hpp"
3131
#include "silkit/services/logging/string_utils.hpp"
@@ -54,7 +54,7 @@ struct LogMsg
5454
log_clock::time_point time;
5555
SourceLoc source;
5656
std::string payload;
57-
std::unordered_map<std::string, std::string> keyValues;
57+
std::vector<std::pair<std::string, std::string>> keyValues;
5858
};
5959

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

6969

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

7373
// ================================================================================
7474
// Inline Implementations
@@ -98,15 +98,16 @@ std::ostream& operator<<(std::ostream& out, const SourceLoc& sourceLoc)
9898
<< "line=" << sourceLoc.line << ", funcname={\"" << sourceLoc.funcname << "\"}";
9999
}
100100

101-
inline std::string to_string(const std::unordered_map<std::string, std::string>& kv)
101+
102+
inline std::string to_string(const std::vector<std::pair<std::string, std::string>>& kv)
102103
{
103104
std::stringstream outStream;
104105
outStream << kv;
105106
return outStream.str();
106107
}
107108

108109

109-
inline std::ostream& operator<<(std::ostream& out, const std::unordered_map<std::string, std::string>& kv)
110+
inline std::ostream& operator<<(std::ostream& out, const std::vector<std::pair<std::string, std::string>>& kv)
110111
{
111112
std::string result;
112113
result.reserve(kv.size() * 2);
@@ -115,7 +116,7 @@ inline std::ostream& operator<<(std::ostream& out, const std::unordered_map<std:
115116
{
116117
result.append(", kv: ");
117118

118-
std::unordered_map<std::string, std::string>::const_iterator it = kv.begin();
119+
std::vector<std::pair<std::string, std::string>>::const_iterator it = kv.begin();
119120
result.append("{");
120121
while (it != kv.end())
121122
{

SilKit/source/core/internal/MessageBuffer.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ class MessageBuffer
229229
template <typename ValueT, size_t SIZE>
230230
inline MessageBuffer& operator>>(std::array<ValueT, SIZE>& array);
231231
// --------------------------------------------------------------------------------
232+
// std::pair<T, U>
233+
template <typename T, typename U>
234+
inline MessageBuffer& operator<<(const std::pair<T, U>& pair);
235+
template <typename T, typename U>
236+
inline MessageBuffer& operator>>(std::pair<T, U>& pair);
237+
// --------------------------------------------------------------------------------
232238
// std::chrono::duration<Rep, Period> and system_clock::time_point
233239
template <class Rep, class Period>
234240
inline MessageBuffer& operator<<(std::chrono::duration<Rep, Period> duration);
@@ -632,6 +638,21 @@ inline MessageBuffer& MessageBuffer::operator>>(std::unordered_map<std::string,
632638
return *this;
633639
}
634640

641+
template <typename T, typename U>
642+
inline MessageBuffer& MessageBuffer::operator<<(const std::pair<T, U>& pair)
643+
{
644+
*this << pair.first << pair.second;
645+
return *this;
646+
}
647+
648+
template <typename T, typename U>
649+
inline MessageBuffer& MessageBuffer::operator>>(std::pair<T, U>& pair)
650+
{
651+
*this >> pair.first >> pair.second;
652+
return *this;
653+
}
654+
655+
635656
// --------------------------------------------------------------------------------
636657
// Uuid
637658

SilKit/source/core/internal/ServiceDescriptor.hpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
2424
#include <sstream>
2525
#include <map>
2626

27+
#include "StructuredLoggingKeys.hpp"
2728
#include "ServiceConfigKeys.hpp"
2829
#include "Configuration.hpp"
2930
#include "EndpointAddress.hpp"
@@ -70,6 +71,8 @@ class ServiceDescriptor
7071
inline bool operator==(const ServiceDescriptor& rhs) const;
7172
inline bool operator!=(const ServiceDescriptor& rhs) const;
7273
inline std::string to_string() const;
74+
inline std::vector<std::pair<std::string, std::string>> to_keyValues() const;
75+
7376
inline Core::EndpointAddress to_endpointAddress() const;
7477

7578
public:
@@ -304,6 +307,47 @@ std::string ServiceDescriptor::to_string() const
304307
return ss.str();
305308
}
306309

310+
311+
std::vector<std::pair<std::string, std::string>> ServiceDescriptor::to_keyValues() const
312+
{
313+
namespace Keys = SilKit::Services::Logging::Keys;
314+
315+
std::vector<std::pair<std::string, std::string>> kv;
316+
std::string controllerTypeName;
317+
std::stringstream ss;
318+
319+
kv.push_back({Keys::participantName, GetParticipantName()});
320+
321+
switch (GetServiceType())
322+
{
323+
case ServiceType::Link:
324+
kv.push_back({Keys::networkType, Config::to_string(GetNetworkType())});
325+
kv.push_back({Keys::networkName, GetNetworkName()});
326+
break;
327+
case ServiceType::Controller:
328+
case ServiceType::SimulatedController:
329+
if (!GetSupplementalDataItem(SilKit::Core::Discovery::controllerType, controllerTypeName))
330+
{
331+
throw LogicError(
332+
"ServiceDescriptor::to_keyValues() failed: No controller type defined in supplemental data.");
333+
}
334+
kv.push_back({Keys::controllerTypeName, controllerTypeName});
335+
kv.push_back({Keys::networkName, GetNetworkName()});
336+
kv.push_back({Keys::serviceName, GetServiceName()});
337+
break;
338+
case ServiceType::InternalController:
339+
kv.push_back({Keys::serviceName, GetServiceName()});
340+
break;
341+
case ServiceType::Undefined:
342+
kv.push_back({Keys::networkName, GetNetworkName()});
343+
kv.push_back({Keys::serviceName, GetServiceName()});
344+
break;
345+
}
346+
347+
kv.push_back({Keys::serviceType, SilKit::Core::to_string(GetServiceType())});
348+
return kv;
349+
}
350+
307351
EndpointAddress ServiceDescriptor::to_endpointAddress() const
308352
{
309353
return {GetParticipantId(), GetServiceId()};

SilKit/source/core/mock/nullconnection/NullConnectionParticipant.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ struct NullConnection
4040
}
4141

4242
void SetLogger(Services::Logging::ILogger* /*logger*/) {}
43+
void SetLoggerInternal(Services::Logging::ILoggerInternal* /*logger*/) {}
4344
void SetTimeSyncService(Orchestration::TimeSyncService* /*timeSyncService*/) {}
4445
void JoinSimulation(std::string /*registryUri*/) {}
4546

0 commit comments

Comments
 (0)