|
9 | 9 | #include "ServiceDescriptor.hpp" |
10 | 10 | #include "traits/SilKitMsgTraits.hpp" |
11 | 11 |
|
| 12 | +#include "YamlParser.hpp" |
| 13 | + |
12 | 14 |
|
13 | 15 | namespace SilKit { |
14 | 16 | namespace Services { |
15 | 17 |
|
16 | | - |
17 | | -template <typename MsgT> |
18 | | -std::chrono::nanoseconds GetTimestamp(MsgT& msg, |
19 | | - std::enable_if_t<Core::HasTimestamp<MsgT>::value, bool> = true) |
| 18 | +namespace Detail { |
| 19 | +template <class SilKitMessageT> |
| 20 | +void TraceMessageCommon(Logging::ILoggerInternal* logger, |
| 21 | + const char* messageString, |
| 22 | + const Core::IServiceEndpoint* addr, |
| 23 | + const SilKitMessageT& msg, |
| 24 | + std::string_view keyString = {}, |
| 25 | + std::string_view valueString = {}) |
20 | 26 | { |
21 | | - return msg.timestamp; |
22 | | -} |
23 | 27 |
|
24 | | -template <typename MsgT> |
25 | | -std::chrono::nanoseconds GetTimestamp(MsgT& /*msg*/, |
26 | | - std::enable_if_t<!Core::HasTimestamp<MsgT>::value, bool> = false) |
27 | | -{ |
28 | | - return std::chrono::nanoseconds::duration::min(); |
| 28 | + if constexpr (std::is_same_v<SilKitMessageT, SilKit::Services::Logging::LogMsg>) |
| 29 | + { |
| 30 | + // Don't trace LogMessages - this could cause cycles! |
| 31 | + return; |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + if (logger->GetLogLevel() == Logging::Level::Trace) |
| 36 | + { |
| 37 | + Logging::LoggerMessage lm{logger, Logging::Level::Trace}; |
| 38 | + lm.SetMessage(messageString); |
| 39 | + lm.SetKeyValue(addr->GetServiceDescriptor()); |
| 40 | + lm.FormatKeyValue(Logging::Keys::msg, "{}", msg); |
| 41 | + |
| 42 | + if (!keyString.empty() && ! valueString.empty()) |
| 43 | + { |
| 44 | + lm.SetKeyValue(keyString, valueString); |
| 45 | + } |
| 46 | + |
| 47 | + if constexpr (Core::HasTimestamp<SilKitMessageT>::value) |
| 48 | + { |
| 49 | + lm.FormatKeyValue(Logging::Keys::virtualTimeNS, "{}", msg.timestamp.count()); |
| 50 | + } |
| 51 | + |
| 52 | + // Turn the Raw-logging into a trait when we have enough types that implement it |
| 53 | + if constexpr (std::is_same_v<SilKitMessageT, SilKit::Services::Flexray::FlexrayControllerConfig>) |
| 54 | + { |
| 55 | + lm.SetKeyValue(Logging::Keys::raw, SilKit::Config::SerializeAsJson(msg)); |
| 56 | + } |
| 57 | + lm.Dispatch(); |
| 58 | + } |
| 59 | + } |
29 | 60 | } |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
| 61 | +} // namespace Detail |
34 | 62 | template <class SilKitMessageT> |
35 | 63 | void TraceRx(Logging::ILoggerInternal* logger, const Core::IServiceEndpoint* addr, const SilKitMessageT& msg, |
36 | 64 | const Core::ServiceDescriptor& from) |
37 | 65 | { |
38 | | - if (logger->GetLogLevel() == Logging::Level::Trace) |
39 | | - { |
40 | | - Logging::LoggerMessage lm{logger, Logging::Level::Trace}; |
41 | | - lm.SetMessage("Recv message"); |
42 | | - lm.SetKeyValue(addr->GetServiceDescriptor()); |
43 | | - lm.SetKeyValue(Logging::Keys::from, from.GetParticipantName()); |
44 | | - lm.FormatKeyValue(Logging::Keys::msg, "{}", msg); |
45 | | - |
46 | | - auto virtualTimeStamp = GetTimestamp(msg); |
47 | | - if (virtualTimeStamp != std::chrono::nanoseconds::duration::min()) |
48 | | - { |
49 | | - lm.FormatKeyValue(Logging::Keys::virtualTimeNS, "{}", virtualTimeStamp.count()); |
50 | | - } |
51 | | - lm.Dispatch(); |
52 | | - } |
| 66 | + Detail::TraceMessageCommon(logger, "Recv message", addr, msg, Logging::Keys::from, from.GetParticipantName()); |
53 | 67 | } |
54 | 68 |
|
55 | 69 | template <class SilKitMessageT> |
56 | 70 | void TraceTx(Logging::ILoggerInternal* logger, const Core::IServiceEndpoint* addr, const SilKitMessageT& msg) |
57 | 71 | { |
58 | | - if (logger->GetLogLevel() == Logging::Level::Trace) |
59 | | - { |
60 | | - Logging::LoggerMessage lm{logger, Logging::Level::Trace}; |
61 | | - lm.SetMessage("Send message"); |
62 | | - lm.SetKeyValue(addr->GetServiceDescriptor()); |
63 | | - lm.FormatKeyValue(Logging::Keys::msg, "{}", msg); |
64 | | - |
65 | | - auto virtualTimeStamp = GetTimestamp(msg); |
66 | | - if (virtualTimeStamp != std::chrono::nanoseconds::duration::min()) |
67 | | - { |
68 | | - lm.FormatKeyValue(Logging::Keys::virtualTimeNS, "{}", virtualTimeStamp.count()); |
69 | | - } |
70 | | - lm.Dispatch(); |
71 | | - } |
| 72 | + Detail::TraceMessageCommon(logger, "Send message", addr, msg); |
72 | 73 | } |
73 | 74 |
|
74 | | -// targeted messages |
75 | 75 | template <class SilKitMessageT> |
76 | 76 | void TraceTx(Logging::ILoggerInternal* logger, const Core::IServiceEndpoint* addr, const std::string_view target, const SilKitMessageT& msg) |
77 | 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 | | -} |
94 | | -// Don't trace LogMessages - this could cause cycles! |
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 | | -} |
107 | | - |
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 | | -{ |
| 78 | + Detail::TraceMessageCommon(logger, "Send targetted message", addr, msg, Logging::Keys::to, target); |
113 | 79 | } |
114 | 80 | } // namespace Services |
115 | 81 | } // namespace SilKit |
0 commit comments