Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions examples/mnist-learn/parse_arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include "parse_arguments.h"

#include <knp/framework/logging.h>

#include <iostream>
#include <string>

Expand All @@ -39,10 +41,12 @@ std::optional<ModelDescription> parse_arguments(int argc, char** argv)
"images", po::value<std::string>()->default_value("MNIST.bin"), "path to raw images file")(
"labels", po::value<std::string>()->default_value("MNIST.target"), "path to images labels file")(
"backend,b", po::value<std::string>()->default_value("knp-cpu-single-threaded-backend"), "path to backend")(
"log_path", po::value<std::string>()->default_value(""),
"path for putting logs. if no path is specified, no logs will be produced.")(
"extensive_logs_path", po::value<std::string>()->default_value(""),
"path for putting extensive logs. if no path is specified, no extensive logs will be produced.")(
"model_path", po::value<std::string>()->default_value(""),
"path for saving trained model. if no path is specified, model wont be saved.");
"path for saving trained model. if no path is specified, model wont be saved.")(
"logging_level,l", po::value<std::string>()->default_value("info"),
"logging level. allowed options are: trace, debug, info, warn, error, critical, none");

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
Expand Down Expand Up @@ -154,5 +158,20 @@ std::optional<ModelDescription> parse_arguments(int argc, char** argv)
model_desc.model_saving_path_ = "";
}

if (vm.count("logging_level"))
{
knp::framework::logging::Level logging_level =
knp::framework::logging::str_to_level(vm["logging_level"].as<std::string>());
knp::framework::logging::set_level(logging_level);
std::cout << "Set logging level to \"" << knp::framework::logging::level_to_str(logging_level) << "\""
<< std::endl;
}
else
{
std::cout << "Logging level not specified." << std::endl;
std::cout << desc << std::endl;
return std::nullopt;
}

return model_desc;
}
1 change: 1 addition & 0 deletions knp/base-framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ knp_add_library("${PROJECT_NAME}-core"
impl/inference_evaluation/perfomance_metrics.cpp
impl/inference_evaluation/classification/processor.cpp
impl/observer.cpp
impl/logging.cpp
${${PROJECT_NAME}_headers}
ALIAS KNP::BaseFramework::Core
LINK_PRIVATE
Expand Down
90 changes: 90 additions & 0 deletions knp/base-framework/impl/logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* @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
{

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;
}


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;
}


void set_level(Level level)
{
spdlog::set_level(convert_level_to_spdlog_level(level));
}


Level get_level()
{
return convert_spdlog_level_to_level(spdlog::get_level());
}


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();
}


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(str.begin());
if (level == spdlog::level::off)
Comment thread
artiomn marked this conversation as resolved.
{
SPDLOG_ERROR("Could not convert string to level.");
return none;
}
return convert_spdlog_level_to_level(level);
}

} //namespace knp::framework::logging
80 changes: 80 additions & 0 deletions knp/base-framework/include/knp/framework/logging.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/**
* @file logging.h
* @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.
*/

#pragma once

#include <string>


/**
* @brief Logging namespace.
*/
namespace knp::framework::logging
{

/**
* @brief Levels of logging. Each level includes all levels below it including itself. So for example "warn" will
* enable "warn","error","critical", "none".
* @note This enum have direct mapping to spdlog's logging levels.
*
*/
enum Level : int
{
trace,
debug,
info,
warn,
error,
critical,
none
};


/**
* @brief Set level of logging.
* @param level Logging level.
*/
void set_level(Level level);


/**
* @brief Get level of logging.
* @return Logging level.
*/
Level get_level();


/**
* @brief Convert level to string.
* @param level Logging level.
* @return Converted string.
*/
std::string level_to_str(Level level);


/**
* @brief Convert string to level.
* @param str Level string.
* @return Converted logging level.
*/
Level str_to_level(std::string_view str);

} //namespace knp::framework::logging
3 changes: 0 additions & 3 deletions knp/core-library/impl/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ namespace knp::core
Backend::Backend()
: message_bus_{knp::core::MessageBus::construct_bus()}, message_endpoint_(message_bus_->create_endpoint())
{
#if (!defined(NDEBUG))
spdlog::set_level(spdlog::level::trace);
#endif
}


Expand Down
Loading