-
Notifications
You must be signed in to change notification settings - Fork 21
Added ability to specify spdlog logging level: #157 #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7defc72
Added ability to specify spdlog logging level: #157
DavidIkov 4ce116c
Added logging api for controlling spdlog in knp: #157
DavidIkov a79a684
Changed file description for logging, and added const modifier to int…
DavidIkov 51f5046
Made str_to_level function more safe, now it can detect empty strings…
DavidIkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| { | ||
| SPDLOG_ERROR("Could not convert string to level."); | ||
| return none; | ||
| } | ||
| return convert_spdlog_level_to_level(level); | ||
| } | ||
|
|
||
| } //namespace knp::framework::logging | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.