Tomáš Pecka | ba2dc31 | 2021-01-23 22:29:11 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016-2021 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Jan Kundrát <jan.kundrat@cesnet.cz> |
| 5 | * Written by Tomáš Pecka <tomas.pecka@cesnet.cz> |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | #include "sysrepo.h" |
| 10 | #include "utils/log.h" |
| 11 | |
| 12 | extern "C" { |
| 13 | #include <sysrepo.h> |
| 14 | } |
| 15 | |
| 16 | extern "C" { |
| 17 | /** @short Propagate sysrepo events to spdlog */ |
| 18 | static void spdlog_sr_log_cb(sr_log_level_t level, const char* message) |
| 19 | { |
| 20 | // Thread safety note: this is, as far as I know, thread safe: |
| 21 | // - the static initialization itself is OK |
| 22 | // - all loggers which we instantiate are thread-safe |
| 23 | // - std::shared_ptr::operator-> is const, and all const members of that class are documented to be thread-safe |
| 24 | static auto log = spdlog::get("sysrepo"); |
| 25 | assert(log); |
| 26 | switch (level) { |
| 27 | case SR_LL_NONE: |
| 28 | case SR_LL_ERR: |
| 29 | log->error(message); |
| 30 | break; |
| 31 | case SR_LL_WRN: |
| 32 | log->warn(message); |
| 33 | break; |
| 34 | case SR_LL_INF: |
| 35 | log->info(message); |
| 36 | break; |
| 37 | case SR_LL_DBG: |
| 38 | log->debug(message); |
| 39 | break; |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | namespace velia::utils { |
| 45 | |
| 46 | /** @short Setup sysrepo log forwarding |
| 47 | You must call cla::utils::initLogs prior to this function. |
| 48 | */ |
| 49 | void initLogsSysrepo() |
| 50 | { |
| 51 | sr_log_set_cb(spdlog_sr_log_cb); |
| 52 | } |
| 53 | |
| 54 | void valuesToYang(const std::map<std::string, std::string>& values, std::shared_ptr<::sysrepo::Session> session, std::shared_ptr<libyang::Data_Node>& parent) |
| 55 | { |
| 56 | for (const auto& [propertyName, value] : values) { |
| 57 | spdlog::get("main")->trace("propertyName: {}, value: {}", propertyName, value.c_str()); |
| 58 | |
| 59 | if (!parent) { |
| 60 | parent = std::make_shared<libyang::Data_Node>( |
| 61 | session->get_context(), |
| 62 | propertyName.c_str(), |
| 63 | value.c_str(), |
| 64 | LYD_ANYDATA_CONSTSTRING, |
| 65 | LYD_PATH_OPT_OUTPUT); |
| 66 | } else { |
| 67 | parent->new_path( |
| 68 | session->get_context(), |
| 69 | propertyName.c_str(), |
| 70 | value.c_str(), |
| 71 | LYD_ANYDATA_CONSTSTRING, |
| 72 | LYD_PATH_OPT_OUTPUT); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | } |