OutputDebugStringLogger.cpp (1572B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #include "pch.h" 4 #include "winget/OutputDebugStringLogger.h" 5 6 namespace AppInstaller::Logging 7 { 8 namespace 9 { 10 static constexpr std::string_view s_OutputDebugStringLoggerName = "OutputDebugStringLogger"; 11 } 12 13 std::string OutputDebugStringLogger::GetName() const 14 { 15 return std::string{ s_OutputDebugStringLoggerName }; 16 } 17 18 void OutputDebugStringLogger::Write(Channel channel, Level, std::string_view message) noexcept try 19 { 20 std::stringstream strstr; 21 strstr << "[" << std::setw(GetMaxChannelNameLength()) << std::left << std::setfill(' ') << GetChannelName(channel) << "] " << message << std::endl; 22 std::string formattedMessage = std::move(strstr).str(); 23 24 OutputDebugStringA(formattedMessage.c_str()); 25 } 26 catch (...) 27 { 28 // Just eat any exceptions here; better than losing logs 29 } 30 31 void OutputDebugStringLogger::WriteDirect(Channel, Level, std::string_view message) noexcept try 32 { 33 std::string nullTerminatedMessage{ message }; 34 OutputDebugStringA(nullTerminatedMessage.c_str()); 35 } 36 catch (...) 37 { 38 // Just eat any exceptions here; better than losing logs 39 } 40 41 void OutputDebugStringLogger::Add() 42 { 43 Log().AddLogger(std::make_unique<OutputDebugStringLogger>()); 44 } 45 46 void OutputDebugStringLogger::Remove() 47 { 48 Log().RemoveLogger(std::string{ s_OutputDebugStringLoggerName }); 49 } 50 }