Skip to content

Commit 4ce116c

Browse files
committed
Added logging api for controlling spdlog in knp: KasperskyLab#157
1 parent 7defc72 commit 4ce116c

7 files changed

Lines changed: 178 additions & 25 deletions

File tree

examples/mnist-learn/main.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
* limitations under the License.
2020
*/
2121

22-
#include <spdlog/spdlog.h>
23-
2422
#include <iostream>
2523

2624
#include "dataset.h"
@@ -69,8 +67,6 @@ int main(int argc, char** argv)
6967
if (!model_desc_opt.has_value()) return EXIT_FAILURE;
7068
const ModelDescription& model_desc = model_desc_opt.value();
7169

72-
spdlog::set_level(static_cast<spdlog::level::level_enum>(model_desc.spdlog_level_));
73-
7470
std::cout << "Starting model:\n" << model_desc << std::endl;
7571

7672
// Starting model according to selected type.

examples/mnist-learn/model_desc.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,5 @@ std::ostream& operator<<(std::ostream& stream, ModelDescription const& desc)
5757
else
5858
stream << "Model saving path: " << desc.model_saving_path_ << "\n";
5959

60-
stream << "Spdlog logging level: "
61-
<< spdlog::level::to_string_view(static_cast<spdlog::level::level_enum>(desc.spdlog_level_)).begin() << "\n";
62-
63-
6460
return stream;
6561
}

examples/mnist-learn/model_desc.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ struct ModelDescription
6161

6262
/// Path to folder for saving trained model in sonata format.
6363
std::filesystem::path model_saving_path_;
64-
65-
/// Spdlog logging level.
66-
// cppcheck-suppress unusedStructMember
67-
int spdlog_level_;
6864
};
6965

7066

examples/mnist-learn/parse_arguments.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include "parse_arguments.h"
2323

24-
#include <spdlog/spdlog.h>
24+
#include <knp/framework/logging.h>
2525

2626
#include <iostream>
2727
#include <string>
@@ -41,11 +41,12 @@ std::optional<ModelDescription> parse_arguments(int argc, char** argv)
4141
"images", po::value<std::string>()->default_value("MNIST.bin"), "path to raw images file")(
4242
"labels", po::value<std::string>()->default_value("MNIST.target"), "path to images labels file")(
4343
"backend,b", po::value<std::string>()->default_value("knp-cpu-single-threaded-backend"), "path to backend")(
44-
"log_path", po::value<std::string>()->default_value(""),
45-
"path for putting logs. if no path is specified, no logs will be produced.")(
44+
"extensive_logs_path", po::value<std::string>()->default_value(""),
45+
"path for putting extensive logs. if no path is specified, no extensive logs will be produced.")(
4646
"model_path", po::value<std::string>()->default_value(""),
4747
"path for saving trained model. if no path is specified, model wont be saved.")(
48-
"spdlog_level", po::value<std::string>()->default_value("info"), "spdlog logging level.");
48+
"logging_level,l", po::value<std::string>()->default_value("info"),
49+
"logging level. allowed options are: trace, debug, info, warn, error, critical, none");
4950

5051
po::variables_map vm;
5152
po::store(po::parse_command_line(argc, argv, desc), vm);
@@ -157,19 +158,17 @@ std::optional<ModelDescription> parse_arguments(int argc, char** argv)
157158
model_desc.model_saving_path_ = "";
158159
}
159160

160-
if (vm.count("spdlog_level"))
161+
if (vm.count("logging_level"))
161162
{
162-
model_desc.spdlog_level_ = spdlog::level::from_str(vm["spdlog_level"].as<std::string>());
163-
if (model_desc.spdlog_level_ == spdlog::level::off)
164-
{
165-
std::cout << "Spdlog logging level is incorrect." << std::endl;
166-
std::cout << desc << std::endl;
167-
return std::nullopt;
168-
}
163+
knp::framework::logging::Level logging_level =
164+
knp::framework::logging::str_to_level(vm["logging_level"].as<std::string>());
165+
knp::framework::logging::set_level(logging_level);
166+
std::cout << "Set logging level to \"" << knp::framework::logging::level_to_str(logging_level) << "\""
167+
<< std::endl;
169168
}
170169
else
171170
{
172-
std::cout << "Spdlog logging level not specified." << std::endl;
171+
std::cout << "Logging level not specified." << std::endl;
173172
std::cout << desc << std::endl;
174173
return std::nullopt;
175174
}

knp/base-framework/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ knp_add_library("${PROJECT_NAME}-core"
7575
impl/inference_evaluation/perfomance_metrics.cpp
7676
impl/inference_evaluation/classification/processor.cpp
7777
impl/observer.cpp
78+
impl/logging.cpp
7879
${${PROJECT_NAME}_headers}
7980
ALIAS KNP::BaseFramework::Core
8081
LINK_PRIVATE
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* @file logging.cpp
3+
* @brief Global logging settings.
4+
* @kaspersky_support Postnikov D.
5+
* @date 17.02.2026
6+
* @license Apache 2.0
7+
* @copyright © 2026 AO Kaspersky Lab
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*/
21+
22+
#include <knp/framework/logging.h>
23+
24+
#include <spdlog/spdlog.h>
25+
26+
27+
namespace knp::framework::logging
28+
{
29+
30+
spdlog::level::level_enum convert_level_to_spdlog_level(Level level)
31+
{
32+
auto spdlog_level = static_cast<spdlog::level::level_enum>(level);
33+
if (spdlog_level >= spdlog::level::level_enum::n_levels)
34+
{
35+
SPDLOG_ERROR("Could not convert logging level to spdlog's logging level. Returning level \"none\".");
36+
spdlog_level = spdlog::level::off;
37+
}
38+
return spdlog_level;
39+
}
40+
41+
42+
Level convert_spdlog_level_to_level(spdlog::level::level_enum spdlog_level)
43+
{
44+
auto level = static_cast<Level>(spdlog_level);
45+
if (level > none)
46+
{
47+
SPDLOG_ERROR("Could not convert spdlog's logging level to knp's logging level. Returning level \"none\".");
48+
level = none;
49+
}
50+
return level;
51+
}
52+
53+
54+
void set_level(Level level)
55+
{
56+
spdlog::set_level(convert_level_to_spdlog_level(level));
57+
}
58+
59+
60+
Level get_level()
61+
{
62+
return convert_spdlog_level_to_level(spdlog::get_level());
63+
}
64+
65+
66+
std::string level_to_str(Level level)
67+
{
68+
if (level == none) return "none";
69+
return spdlog::level::to_string_view(convert_level_to_spdlog_level(level)).begin();
70+
}
71+
72+
73+
Level str_to_level(std::string_view str)
74+
{
75+
if (str == "none") return none;
76+
auto level = spdlog::level::from_str(str.begin());
77+
if (level == spdlog::level::off)
78+
{
79+
SPDLOG_ERROR("Could not convert string to level.");
80+
return none;
81+
}
82+
return convert_spdlog_level_to_level(level);
83+
}
84+
85+
} //namespace knp::framework::logging
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* @file logging.h
3+
* @brief Global logging settings.
4+
* @kaspersky_support Postnikov D.
5+
* @date 17.02.2026
6+
* @license Apache 2.0
7+
* @copyright © 2026 AO Kaspersky Lab
8+
*
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*/
21+
22+
#pragma once
23+
24+
#include <string>
25+
26+
27+
/**
28+
* @brief Logging namespace.
29+
*/
30+
namespace knp::framework::logging
31+
{
32+
33+
/**
34+
* @brief Levels of logging. Each level includes all levels below it including itself. So for example "warn" will
35+
* enable "warn","error","critical", "none".
36+
* @note This enum have direct mapping to spdlog's logging levels.
37+
*
38+
*/
39+
enum Level : int
40+
{
41+
trace,
42+
debug,
43+
info,
44+
warn,
45+
error,
46+
critical,
47+
none
48+
};
49+
50+
51+
/**
52+
* @brief Set level of logging.
53+
* @param level Logging level.
54+
*/
55+
void set_level(Level level);
56+
57+
58+
/**
59+
* @brief Get level of logging.
60+
* @return Logging level.
61+
*/
62+
Level get_level();
63+
64+
65+
/**
66+
* @brief Convert level to string.
67+
* @param level Logging level.
68+
* @return Converted string.
69+
*/
70+
std::string level_to_str(Level level);
71+
72+
73+
/**
74+
* @brief Convert string to level.
75+
* @param str Level string.
76+
* @return Converted logging level.
77+
*/
78+
Level str_to_level(std::string_view str);
79+
80+
} //namespace knp::framework::logging

0 commit comments

Comments
 (0)