* Copyright (c) 2025-2026 IB-Robot Group & openEuler Embedded SIG & openharmony-robot sig_RoboFrame.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "log_check.h"
#include <set>
#include <utility>
#include "log_control_ext/log_control_types.h"
namespace ibmw::cli::doctor {
namespace {
namespace protocol = ibmw::protocols::log_control_extension;
void CompareLogLevel(LogCheckResult& result, bool hasExpectation)
{
if (!hasExpectation) {
result.message =
"Actual runtime threshold observed; comparison skipped because no expected level was supplied.";
return;
}
const auto baseline = protocol::NormalizeLogLevel(result.expected);
if (!baseline) {
result.status = CheckStatus::kUnknown;
result.message = "The expected log level is invalid; comparison could not be completed.";
return;
}
result.expected = *baseline;
result.status = result.actual == result.expected ? CheckStatus::kOk : CheckStatus::kWarning;
result.message = result.status == CheckStatus::kOk ? "Actual log level matches the explicit expectation."
: "Actual log level differs from the explicit expectation; "
"confirm whether this is intentional debugging.";
}
LogCheckResult CheckLogger(const std::string& name, const std::map<std::string, std::string>& actual,
const std::map<std::string, std::string>& expected, const std::set<std::string>& scope)
{
LogCheckResult result{.module = name};
const auto found = actual.find(name);
const auto wanted = expected.find(name);
if (wanted != expected.end()) {
result.expected = wanted->second;
}
if (found == actual.end()) {
result.status = CheckStatus::kUnknown;
result.message = "Requested or expected logger was not returned by the selected log service.";
return result;
}
result.actual = found->second;
const auto level = protocol::NormalizeLogLevel(result.actual);
if (name.empty() || !level) {
result.status = CheckStatus::kUnknown;
result.message = "The returned logger name or actual log level is invalid.";
return result;
}
if (!scope.empty() && !scope.contains(name)) {
result.status = CheckStatus::kUnknown;
result.message = "The log service returned a logger outside the requested scope.";
return result;
}
result.actual = *level;
CompareLogLevel(result, wanted != expected.end());
return result;
}
}
std::vector<LogCheckResult> CheckLogLevels(const std::map<std::string, std::string>& actual,
const std::map<std::string, std::string>& expected,
const std::vector<std::string>& requested)
{
const std::set<std::string> scope(requested.begin(), requested.end());
auto names = scope;
for (const auto& [name, value] : actual) {
names.insert(name);
}
for (const auto& [name, value] : expected) {
names.insert(name);
}
std::vector<LogCheckResult> results;
for (const auto& name : names) {
results.push_back(CheckLogger(name, actual, expected, scope));
}
if (results.empty()) {
results.push_back({.message = "The log service returned no registered loggers."});
}
return results;
}
}