| 129 | } |
| 130 | |
| 131 | void SetupTestLogging(const std::string& logFolder, |
| 132 | const std::string& logLevel, |
| 133 | const std::optional<std::string>& logVerboseModules) noexcept |
| 134 | { |
| 135 | try { |
| 136 | util::log::IdentificationTable::AddThisProcess("ms-test"); |
| 137 | util::log::IdentificationTable::AddThisThread("exec"); |
| 138 | |
| 139 | const auto verboseModules = |
| 140 | logVerboseModules ? ParseVerboseModules_(*logVerboseModules) : std::vector<util::log::V>{}; |
| 141 | auto pChannel = util::log::GetDefaultChannel(); |
| 142 | if (!pChannel) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | const auto level = ParseLogLevel_(logLevel); |
| 147 | auto& policy = util::log::GlobalPolicy::Get(); |
| 148 | policy.SetLogLevel(level); |
| 149 | policy.SetTraceLevel(util::log::Level::Error); |
| 150 | policy.SetExceptionTrace(false); |
| 151 | for (auto mod : verboseModules) { |
| 152 | policy.ActivateVerboseModule(mod); |
| 153 | } |
| 154 | |
| 155 | if (!logFolder.empty()) { |
| 156 | std::filesystem::path folderPath{ logFolder }; |
| 157 | std::error_code ec; |
| 158 | std::filesystem::create_directories(folderPath, ec); |
| 159 | const auto filePath = folderPath / BuildLogFileName_(folderPath); |
| 160 | auto pFormatter = std::make_shared<util::log::TextFormatter>(); |
| 161 | pChannel->AttachComponent(std::make_shared<util::log::BasicFileDriver>( |
| 162 | pFormatter, |
| 163 | std::make_shared<util::log::SimpleFileStrategy>(std::move(filePath))), |
| 164 | "drv:file"); |
| 165 | } |
| 166 | |
| 167 | auto& linkState = GetLogLinkState_(); |
| 168 | LoggingSingletons gettersCopy{}; |
| 169 | { |
| 170 | std::lock_guard lock{ linkState.mtx }; |
| 171 | if (!linkState.linked) { |
| 172 | linkState.getters = pmLinkLogging_(pChannel, []() -> util::log::IdentificationTable& { |
| 173 | return util::log::IdentificationTable::Get_(); |
| 174 | }); |
| 175 | linkState.linked = true; |
| 176 | } |
| 177 | gettersCopy = linkState.getters; |
| 178 | } |
| 179 | |
| 180 | if (gettersCopy) { |
| 181 | auto& dllPolicy = gettersCopy.getGlobalPolicy(); |
| 182 | dllPolicy.SetLogLevel(level); |
| 183 | dllPolicy.SetTraceLevel(util::log::Level::Error); |
| 184 | dllPolicy.SetExceptionTrace(false); |
| 185 | for (auto mod : verboseModules) { |
| 186 | dllPolicy.ActivateVerboseModule(mod); |
| 187 | } |
| 188 | } |