TraceLogger.cpp (1721B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "Public/winget/TraceLogger.h" 5 #include "Public/AppInstallerTelemetry.h" 6 #include "Public/winget/ThreadGlobals.h" 7 8 namespace AppInstaller::Logging 9 { 10 void TraceLogger::Write(Channel channel, Level, std::string_view message) noexcept try 11 { 12 // Send to a string first to create a single block to log to a trace. 13 std::stringstream strstr; 14 strstr << std::chrono::system_clock::now() << " [" << std::setw(GetMaxChannelNameLength()) << std::left << std::setfill(' ') << GetChannelName(channel) << "] " << message << std::endl; 15 16 TraceLoggingWriteActivity(g_hTraceProvider, 17 "Diagnostics", 18 Telemetry().GetActivityId(), 19 Telemetry().GetParentActivityId(), 20 TraceLoggingString(strstr.str().c_str(), "LogMessage")); 21 } 22 catch (...) 23 { 24 // Just eat any exceptions here; better to lose logs than functionality 25 } 26 27 void TraceLogger::WriteDirect(Channel, Level, std::string_view message) noexcept try 28 { 29 TraceLoggingWriteActivity(g_hTraceProvider, 30 "Diagnostics", 31 Telemetry().GetActivityId(), 32 Telemetry().GetParentActivityId(), 33 TraceLoggingCountedUtf8String(message.data(), static_cast<ULONG>(message.size()), "LogMessage")); 34 } 35 catch (...) 36 { 37 // Just eat any exceptions here; better to lose logs than functionality 38 } 39 40 std::string TraceLogger::GetName() const 41 { 42 return "Trace"; 43 } 44 45 void TraceLogger::Add() 46 { 47 Log().AddLogger(std::make_unique<TraceLogger>()); 48 } 49 }