@@ -19,6 +19,10 @@ namespace pmon::util::log
1919{
2020 namespace
2121 {
22+ // cache reference to diagnostics system here for C-Api interaction and attachment to new channels
23+ std::shared_ptr<DiagnosticDriver> pDiagnostics_;
24+ std::mutex diagnosticsMtx_;
25+
2226 // creates an independent logging channel or a copy channel, resets log level to default
2327 std::shared_ptr<IChannel> MakeChannel_ (std::shared_ptr<IChannel> pCopyTargetChannel = {})
2428 {
@@ -46,6 +50,13 @@ namespace pmon::util::log
4650 pChannel->AttachComponent (std::make_shared<MsvcDebugDriver>(pFormatter), " drv:dbg" );
4751 pChannel->AttachComponent (std::make_shared<BasicFileDriver>(pFormatter, pFileStrategy), " drv:file" );
4852 }
53+ // connect diagnostic driver if present to new channel
54+ {
55+ std::lock_guard lk{ diagnosticsMtx_ };
56+ if (pDiagnostics_) {
57+ pChannel->AttachComponent (pDiagnostics_, " drv:diag" );
58+ }
59+ }
4960 return pChannel;
5061 }
5162 // creates a channel for debug diagnostic purposes
@@ -54,14 +65,6 @@ namespace pmon::util::log
5465 GlobalPolicy::Get ().SetLogLevelDefault ();
5566 // channel
5667 auto pChannel = std::make_shared<Channel>();
57- // error resolver
58- auto pErrorResolver = std::make_shared<ErrorCodeResolver>();
59- pErrorResolver->AddProvider (std::make_unique<win::HrErrorCodeProvider>());
60- pErrorResolver->AddProvider (std::make_unique<pmapi::PmErrorCodeProvider>());
61- // error resolving policy
62- auto pErrPolicy = std::make_shared<ErrorCodeResolvePolicy>();
63- pErrPolicy->SetResolver (std::move (pErrorResolver));
64- pChannel->AttachComponent (std::move (pErrPolicy));
6568 // make and add the line-tracking policy
6669 pChannel->AttachComponent (std::make_shared<LinePolicy>());
6770 // configure drivers
@@ -74,8 +77,6 @@ namespace pmon::util::log
7477 GlobalPolicy::Get ().SetLogLevel (Level::None);
7578 return {};
7679 }
77- // cache reference to diagnostics system here for C-Api interaction
78- std::shared_ptr<DiagnosticDriver> pDiagnostics_;
7980 }
8081
8182 std::shared_ptr<IChannel> GetDefaultChannel () noexcept
@@ -95,19 +96,23 @@ namespace pmon::util::log
9596
9697 void SetupDiagnosticLayer (const PM_DIAGNOSTIC_CONFIGURATION* pConfig)
9798 {
98- if (!pDiagnostics_) {
99+ // create / replace diagnostic driver
100+ {
101+ std::lock_guard lk{ diagnosticsMtx_ };
99102 pDiagnostics_ = std::make_shared<log::DiagnosticDriver>(pConfig);
100103 }
104+ // attach to existing channel if present
101105 if (auto pChan = GetDefaultChannel ()) {
102106 pChan->AttachComponent (pDiagnostics_, " drv:diag" );
103107 }
104- else {
108+ else { // otherwise make a minimal channel for diagnostic handling
105109 InjectDefaultChannel (MakeDiagnosticChannel_ (pDiagnostics_));
106110 }
107111 }
108112
109113 std::shared_ptr<class DiagnosticDriver > GetDiagnostics ()
110114 {
115+ std::lock_guard lk{ diagnosticsMtx_ };
111116 return pDiagnostics_;
112117 }
113118}
0 commit comments