our_knp/examples/knp/base-framework/impl/logging.cpp
int 8f43927575 Initial import: KNP experiments (examples, viz, cats-dogs, reports)
examples/ is based on KasperskyLab/knp at 55470ed6 with local changes
to the mnist-learn altai/blifat models, cuda-backend CMake and model_loader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 18:17:15 +03:00

90 lines
2.4 KiB
C++

/**
* @file logging.cpp
* @brief Global logging API settings.
* @kaspersky_support Postnikov D.
* @date 17.02.2026
* @license Apache 2.0
* @copyright © 2026 AO Kaspersky Lab
*
* 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 <knp/framework/logging.h>
#include <spdlog/spdlog.h>
namespace knp::framework::logging
{
KNP_DECLSPEC spdlog::level::level_enum convert_level_to_spdlog_level(Level level)
{
auto spdlog_level = static_cast<spdlog::level::level_enum>(level);
if (spdlog_level >= spdlog::level::level_enum::n_levels)
{
SPDLOG_ERROR("Could not convert logging level to spdlog's logging level. Returning level \"none\".");
spdlog_level = spdlog::level::off;
}
return spdlog_level;
}
KNP_DECLSPEC Level convert_spdlog_level_to_level(spdlog::level::level_enum spdlog_level)
{
auto level = static_cast<Level>(spdlog_level);
if (level > none)
{
SPDLOG_ERROR("Could not convert spdlog's logging level to knp's logging level. Returning level \"none\".");
level = none;
}
return level;
}
KNP_DECLSPEC void set_level(Level level)
{
spdlog::set_level(convert_level_to_spdlog_level(level));
}
KNP_DECLSPEC Level get_level()
{
return convert_spdlog_level_to_level(spdlog::get_level());
}
KNP_DECLSPEC std::string level_to_str(Level level)
{
if (level == none) return "none";
return spdlog::level::to_string_view(convert_level_to_spdlog_level(level)).begin();
}
KNP_DECLSPEC Level str_to_level(std::string_view str)
{
if (str.empty())
{
SPDLOG_ERROR("String is empty.");
return none;
}
if (str == "none") return none;
const auto level = spdlog::level::from_str(std::string(str));
if (level == spdlog::level::off)
{
SPDLOG_ERROR("Could not convert string to level.");
return none;
}
return convert_spdlog_level_to_level(level);
}
} //namespace knp::framework::logging