winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

ThreadGlobals.cpp (2088B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "Public/winget/ThreadGlobals.h"
      5 
      6 namespace AppInstaller::ThreadLocalStorage
      7 {
      8     using namespace AppInstaller::Logging;
      9 
     10     WingetThreadGlobals::WingetThreadGlobals(WingetThreadGlobals& parent, create_sub_thread_globals_t)
     11     {
     12         parent.Initialize();
     13         m_pDiagnosticLogger = parent.m_pDiagnosticLogger;
     14         m_pTelemetryLogger = parent.m_pTelemetryLogger->CreateSubTraceLogger();
     15         // Flip the initialization flag
     16         std::call_once(m_loggerInitOnceFlag, []() {});
     17     }
     18 
     19     DiagnosticLogger& WingetThreadGlobals::GetDiagnosticLogger()
     20     {
     21         return *(m_pDiagnosticLogger);
     22     }
     23 
     24     void* WingetThreadGlobals::GetTelemetryObject()
     25     {
     26         return m_pTelemetryLogger.get();
     27     }
     28 
     29     TelemetryTraceLogger& WingetThreadGlobals::GetTelemetryLogger()
     30     {
     31         return *(m_pTelemetryLogger);
     32     }
     33 
     34     std::unique_ptr<PreviousThreadGlobals> WingetThreadGlobals::SetForCurrentThread()
     35     {
     36         Initialize();
     37         return ThreadGlobals::SetForCurrentThread();
     38     }
     39 
     40     void WingetThreadGlobals::Initialize()
     41     {
     42         try
     43         {
     44             std::call_once(m_loggerInitOnceFlag, [this]()
     45                 {
     46                     m_pDiagnosticLogger = std::make_unique<DiagnosticLogger>();
     47                     m_pTelemetryLogger = std::make_unique<TelemetryTraceLogger>();
     48 
     49                     // The above make_unique for TelemetryTraceLogger will either create an object or will throw which is caught below.
     50                     m_pTelemetryLogger->Initialize();
     51                 });
     52         }
     53         catch (...)
     54         {
     55             // May throw std::system_error if any condition prevents calls to call_once from executing as specified
     56             // May throw std::bad_alloc or any exception thrown by the constructor of TelemetryTraceLogger
     57             // Loggers are best effort and shouldn't block core functionality. So eat up the exceptions here
     58         }
     59     }
     60 }