winget-cli

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

AppInstallerLogging.cpp (5767B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "Public/AppInstallerLogging.h"
      5 #include "Public/AppInstallerStrings.h"
      6 #include "Public/AppInstallerDateTime.h"
      7 #include "Public/winget/SharedThreadGlobals.h"
      8 
      9 namespace AppInstaller::Logging
     10 {
     11     std::string_view GetChannelName(Channel channel)
     12     {
     13         switch(channel)
     14         {
     15         case Channel::Fail:   return "FAIL";
     16         case Channel::CLI:    return "CLI";
     17         case Channel::SQL:    return "SQL";
     18         case Channel::Repo:   return "REPO";
     19         case Channel::YAML:   return "YAML";
     20         case Channel::Core:   return "CORE";
     21         case Channel::Test:   return "TEST";
     22         case Channel::Config: return "CONF";
     23         case Channel::Workflow: return "WORK";
     24         default:              return "NONE";
     25         }
     26     }
     27 
     28     Channel GetChannelFromName(std::string_view channel)
     29     {
     30         std::string lowerChannel = Utility::ToLower(channel);
     31 
     32         if (lowerChannel == "fail")
     33         {
     34             return Channel::Fail;
     35         }
     36         else if (lowerChannel == "cli")
     37         {
     38             return Channel::CLI;
     39         }
     40         else if (lowerChannel == "sql")
     41         {
     42             return Channel::SQL;
     43         }
     44         else if (lowerChannel == "repo")
     45         {
     46             return Channel::Repo;
     47         }
     48         else if (lowerChannel == "yaml")
     49         {
     50             return Channel::YAML;
     51         }
     52         else if (lowerChannel == "core")
     53         {
     54             return Channel::Core;
     55         }
     56         else if (lowerChannel == "test")
     57         {
     58             return Channel::Test;
     59         }
     60         else if (lowerChannel == "conf" || lowerChannel == "config")
     61         {
     62             return Channel::Config;
     63         }
     64         else if (lowerChannel == "workflow")
     65         {
     66             return Channel::Workflow;
     67         }
     68         else if (lowerChannel == "default" || lowerChannel == "defaults")
     69         {
     70             return Channel::Defaults;
     71         }
     72         else if (lowerChannel == "all")
     73         {
     74             return Channel::All;
     75         }
     76 
     77         return Channel::None;
     78     }
     79 
     80     size_t GetMaxChannelNameLength() { return 4; }
     81 
     82     void DiagnosticLogger::AddLogger(std::unique_ptr<ILogger>&& logger)
     83     {
     84         m_loggers.emplace_back(std::move(logger));
     85     }
     86 
     87     bool DiagnosticLogger::ContainsLogger(const std::string& name)
     88     {
     89         for (auto i = m_loggers.begin(); i != m_loggers.end(); ++i)
     90         {
     91             if ((*i)->GetName() == name)
     92             {
     93                 return true;
     94             }
     95         }
     96 
     97         return false;
     98     }
     99 
    100     std::unique_ptr<ILogger> DiagnosticLogger::RemoveLogger(const std::string& name)
    101     {
    102         std::unique_ptr<ILogger> result;
    103 
    104         for (auto i = m_loggers.begin(); i != m_loggers.end(); ++i)
    105         {
    106             if ((*i)->GetName() == name)
    107             {
    108                 result = std::move(*i);
    109                 m_loggers.erase(i);
    110                 break;
    111             }
    112         }
    113 
    114         return result;
    115     }
    116 
    117     void DiagnosticLogger::RemoveAllLoggers()
    118     {
    119         m_loggers.clear();
    120     }
    121 
    122     void DiagnosticLogger::EnableChannel(Channel channel)
    123     {
    124         WI_SetAllFlags(m_enabledChannels, channel);
    125     }
    126 
    127     void DiagnosticLogger::SetEnabledChannels(Channel channel)
    128     {
    129         m_enabledChannels = channel;
    130     }
    131 
    132     void DiagnosticLogger::DisableChannel(Channel channel)
    133     {
    134         WI_ClearAllFlags(m_enabledChannels, channel);
    135     }
    136 
    137     void DiagnosticLogger::SetLevel(Level level)
    138     {
    139         m_enabledLevel = level;
    140     }
    141 
    142     Level DiagnosticLogger::GetLevel() const
    143     {
    144         return m_enabledLevel;
    145     }
    146 
    147     bool DiagnosticLogger::IsEnabled(Channel channel, Level level) const
    148     {
    149         return (!m_loggers.empty() &&
    150                 WI_IsAnyFlagSet(m_enabledChannels, channel) &&
    151                 (ToIntegral(level) >= ToIntegral(m_enabledLevel)));
    152     }
    153 
    154     void DiagnosticLogger::Write(Channel channel, Level level, std::string_view message)
    155     {
    156         THROW_HR_IF_MSG(E_INVALIDARG, channel == Channel::All, "Cannot write to all channels");
    157 
    158         if (IsEnabled(channel, level))
    159         {
    160             for (auto& logger : m_loggers)
    161             {
    162                 logger->Write(channel, level, message);
    163             }
    164         }
    165     }
    166 
    167     void DiagnosticLogger::WriteDirect(Channel channel, Level level, std::string_view message)
    168     {
    169         THROW_HR_IF_MSG(E_INVALIDARG, channel == Channel::All, "Cannot write to all channels");
    170 
    171         if (IsEnabled(channel, level))
    172         {
    173             for (auto& logger : m_loggers)
    174             {
    175                 logger->WriteDirect(channel, level, message);
    176             }
    177         }
    178     }
    179 
    180     DiagnosticLogger& Log()
    181     {
    182         ThreadLocalStorage::ThreadGlobals* pThreadGlobals = ThreadLocalStorage::ThreadGlobals::GetForCurrentThread();
    183         if (pThreadGlobals)
    184         {
    185             return pThreadGlobals->GetDiagnosticLogger();
    186         }
    187         else
    188         {
    189             static DiagnosticLogger processGlobalLogger;
    190             return processGlobalLogger;
    191         }
    192     }
    193 
    194     std::ostream& SetHRFormat(std::ostream& out)
    195     {
    196         return out << std::hex << std::setw(8) << std::setfill('0');
    197     }
    198 }
    199 
    200 namespace std
    201 {
    202     std::ostream& operator<<(std::ostream& out, const std::chrono::system_clock::time_point& time)
    203     {
    204         AppInstaller::Utility::OutputTimePoint(out, time);
    205         return out;
    206     }
    207 
    208     std::ostream& operator<<(std::ostream& out, const GUID& guid)
    209     {
    210         wchar_t buffer[256];
    211 
    212         if (StringFromGUID2(guid, buffer, ARRAYSIZE(buffer)))
    213         {
    214             out << AppInstaller::Utility::ConvertToUTF8(buffer);
    215         }
    216         else
    217         {
    218             out << "error";
    219         }
    220 
    221         return out;
    222     }
    223 }