| 66 | using namespace ::pmon::util::log; |
| 67 | |
| 68 | void ConfigureLogging(bool asApp) noexcept |
| 69 | { |
| 70 | try { |
| 71 | // shortcuts for command line and registry |
| 72 | const auto& opt = clio::Options::Get(); |
| 73 | const auto& reg = Reg::Get(); |
| 74 | auto& pol = GlobalPolicy::Get(); |
| 75 | // get the channel |
| 76 | auto pChannel = GetDefaultChannel(); |
| 77 | // enable colorized stdio only for child/console mode |
| 78 | if (asApp) { |
| 79 | const bool enableColorizedStdioLog = !opt.disableColorizedStdioLog; |
| 80 | pChannel->AttachComponent(std::make_shared<StdioDriver>(std::make_shared<TextFormatter>(), true, enableColorizedStdioLog), "drv:std"); |
| 81 | } |
| 82 | // configure logging based on command line |
| 83 | if (opt.logLevel || reg.logLevel.Exists()) { |
| 84 | pol.SetLogLevel(opt.logLevel ? *opt.logLevel : reg.logLevel); |
| 85 | } |
| 86 | if (opt.logVerboseModules) { |
| 87 | for (auto mod : *opt.logVerboseModules) { |
| 88 | pol.ActivateVerboseModule(mod); |
| 89 | } |
| 90 | } |
| 91 | else if (reg.logVerboseModules.Exists()) { |
| 92 | pol.StoreVerboseModules(reg.logVerboseModules); |
| 93 | } |
| 94 | if (!opt.enableStdioLog) { |
| 95 | pChannel->AttachComponent({}, "drv:std"); |
| 96 | } |
| 97 | if (!opt.enableDebuggerLog) { |
| 98 | pChannel->AttachComponent({}, "drv:dbg"); |
| 99 | } |
| 100 | if (opt.logDir || reg.logDir.Exists()) { |
| 101 | const auto dir = opt.logDir ? *opt.logDir : reg.logDir; |
| 102 | std::string tag; |
| 103 | if (opt.logNamePid) { |
| 104 | tag = std::to_string(GetCurrentProcessId()); |
| 105 | } |
| 106 | else { |
| 107 | const std::chrono::zoned_time now{ std::chrono::current_zone(), |
| 108 | std::chrono::system_clock::now() }; |
| 109 | tag = std::format("{0:%y}{0:%m}{0:%d}-{0:%H}{0:%M}{0:%OS}", now); |
| 110 | } |
| 111 | auto fullPath = std::format("{}\\pmsvc-log-{}.txt", dir, tag); |
| 112 | pChannel->AttachComponent(std::make_shared<BasicFileDriver>( std::make_shared<TextFormatter>(), |
| 113 | std::make_shared<SimpleFileStrategy>(fullPath)), "drv:file"); |
| 114 | } |
| 115 | // setup ipc logging connection for clients |
| 116 | if (!opt.disableIpcLog) { |
| 117 | try { |
| 118 | auto pSender = std::make_shared<NamedPipeMarshallSender>(*opt.logPipeName, |
| 119 | asApp ? pipe::SecurityMode::Child : pipe::SecurityMode::Service); |
| 120 | log::IdentificationTable::RegisterSink(pSender); |
| 121 | auto pDriver = std::make_shared<log::MarshallDriver>(pSender); |
| 122 | pChannel->AttachComponent(std::move(pDriver)); |
| 123 | } |
| 124 | catch (...) { |
| 125 | pmlog_panic_(ReportException()); |
no test coverage detected