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