Skip to content

Commit ca2da22

Browse files
authored
structured logs: add targeted trace messages to logs (#260)
This adds a `To` field to the `kv` field of structured log messages. Useful for structured log analysis involving the network simulator.
1 parent 5efc049 commit ca2da22

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

SilKit/source/core/participant/Participant_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1633,7 +1633,7 @@ template <typename SilKitMessageT>
16331633
void Participant<SilKitConnectionT>::SendMsgImpl(const IServiceEndpoint* from, const std::string& targetParticipantName,
16341634
SilKitMessageT&& msg)
16351635
{
1636-
TraceTx(GetLoggerInternal(), from, msg);
1636+
TraceTx(GetLoggerInternal(), from, targetParticipantName, msg);
16371637
_connection.SendMsg(from, targetParticipantName, std::forward<SilKitMessageT>(msg));
16381638
}
16391639

SilKit/source/services/logging/MessageTracing.hpp

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,45 @@ void TraceTx(Logging::ILoggerInternal* logger, const Core::IServiceEndpoint* add
7171
}
7272
}
7373

74+
// targeted messages
75+
template <class SilKitMessageT>
76+
void TraceTx(Logging::ILoggerInternal* logger, const Core::IServiceEndpoint* addr, const std::string_view target, const SilKitMessageT& msg)
77+
{
78+
if (logger->GetLogLevel() == Logging::Level::Trace)
79+
{
80+
Logging::LoggerMessage lm{logger, Logging::Level::Trace};
81+
lm.SetMessage("Send message");
82+
lm.SetKeyValue(addr->GetServiceDescriptor());
83+
lm.FormatKeyValue(Logging::Keys::msg, "{}", msg);
84+
lm.FormatKeyValue(Logging::Keys::to, "{}", target);
85+
86+
auto virtualTimeStamp = GetTimestamp(msg);
87+
if (virtualTimeStamp != std::chrono::nanoseconds::duration::min())
88+
{
89+
lm.FormatKeyValue(Logging::Keys::virtualTimeNS, "{}", virtualTimeStamp.count());
90+
}
91+
lm.Dispatch();
92+
}
93+
}
7494
// Don't trace LogMessages - this could cause cycles!
75-
inline void TraceRx(Logging::ILoggerInternal* /*logger*/, Core::IServiceEndpoint* /*addr*/, const Logging::LogMsg& /*msg*/) {}
76-
inline void TraceTx(Logging::ILoggerInternal* /*logger*/, Core::IServiceEndpoint* /*addr*/, const Logging::LogMsg& /*msg*/) {}
95+
inline void TraceRx(Logging::ILoggerInternal* /*logger*/, Core::IServiceEndpoint* /*addr*/,
96+
const Logging::LogMsg& /*msg*/)
97+
{
98+
}
99+
inline void TraceTx(Logging::ILoggerInternal* /*logger*/, const Core::IServiceEndpoint* /*addr*/,
100+
const std::string_view /*target*/, const Logging::LogMsg& /*msg*/)
101+
{
102+
}
103+
inline void TraceTx(Logging::ILoggerInternal* /*logger*/, Core::IServiceEndpoint* /*addr*/,
104+
const Logging::LogMsg& /*msg*/)
105+
{
106+
}
77107

78-
inline void TraceRx(Logging::ILoggerInternal* /*logger*/, Core::IServiceEndpoint* /*addr*/, Logging::LogMsg&& /*msg*/) {}
79-
inline void TraceTx(Logging::ILoggerInternal* /*logger*/, Core::IServiceEndpoint* /*addr*/, Logging::LogMsg&& /*msg*/) {}
108+
inline void TraceRx(Logging::ILoggerInternal* /*logger*/, Core::IServiceEndpoint* /*addr*/, Logging::LogMsg&& /*msg*/)
109+
{
110+
}
111+
inline void TraceTx(Logging::ILoggerInternal* /*logger*/, Core::IServiceEndpoint* /*addr*/, Logging::LogMsg&& /*msg*/)
112+
{
113+
}
80114
} // namespace Services
81115
} // namespace SilKit

SilKit/source/services/logging/StructuredLoggingKeys.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <atomic>
88
#include <string>
9+
#include <string_view>
910

1011
namespace SilKit {
1112
namespace Services {
@@ -15,6 +16,7 @@ namespace Keys {
1516
const std::string virtualTimeNS{"VirtualTimeNS"};
1617
const std::string msg{"Msg"};
1718
const std::string from{"From"};
19+
constexpr std::string_view to{"To"};
1820

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

0 commit comments

Comments
 (0)