LCOV - code coverage report
Current view: top level - src/common - log_util.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 23 42 54.8 %
Date: 2024-04-23 00:09:24 Functions: 3 3 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :   Copyright 2018-2024, Barcelona Supercomputing Center (BSC), Spain
       3             :   Copyright 2015-2024, Johannes Gutenberg Universitaet Mainz, Germany
       4             : 
       5             :   This software was partially supported by the
       6             :   EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).
       7             : 
       8             :   This software was partially supported by the
       9             :   ADA-FS project under the SPPEXA project funded by the DFG.
      10             : 
      11             :   This file is part of GekkoFS.
      12             : 
      13             :   GekkoFS is free software: you can redistribute it and/or modify
      14             :   it under the terms of the GNU General Public License as published by
      15             :   the Free Software Foundation, either version 3 of the License, or
      16             :   (at your option) any later version.
      17             : 
      18             :   GekkoFS is distributed in the hope that it will be useful,
      19             :   but WITHOUT ANY WARRANTY; without even the implied warranty of
      20             :   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      21             :   GNU General Public License for more details.
      22             : 
      23             :   You should have received a copy of the GNU General Public License
      24             :   along with GekkoFS.  If not, see <https://www.gnu.org/licenses/>.
      25             : 
      26             :   SPDX-License-Identifier: GPL-3.0-or-later
      27             : */
      28             : 
      29             : #include <common/log_util.hpp>
      30             : 
      31             : #include <spdlog/sinks/basic_file_sink.h>
      32             : #include <vector>
      33             : #include <algorithm>
      34             : #include <list>
      35             : 
      36             : using namespace std;
      37             : 
      38             : spdlog::level::level_enum
      39          33 : gkfs::log::get_level(string level_str) {
      40          33 :     char* parse_end;
      41          33 :     auto level = strtoul(level_str.c_str(), &parse_end, 10);
      42          33 :     if(parse_end != (level_str.c_str() + level_str.size())) {
      43             :         // no conversion could be performed. Must be a string then
      44           0 :         ::transform(level_str.begin(), level_str.end(), level_str.begin(),
      45           0 :                     ::tolower);
      46           0 :         if(level_str == "off"s)
      47             :             return spdlog::level::off;
      48           0 :         else if(level_str == "critical"s)
      49             :             return spdlog::level::critical;
      50           0 :         else if(level_str == "err"s)
      51             :             return spdlog::level::err;
      52           0 :         else if(level_str == "warn"s)
      53             :             return spdlog::level::warn;
      54           0 :         else if(level_str == "info"s)
      55             :             return spdlog::level::info;
      56           0 :         else if(level_str == "debug"s)
      57             :             return spdlog::level::debug;
      58           0 :         else if(level_str == "trace"s)
      59             :             return spdlog::level::trace;
      60             :         else
      61           0 :             throw runtime_error(fmt::format(
      62             :                     "Error: log level '{}' is invalid. Check help/readme.",
      63           0 :                     level_str));
      64             :     } else
      65          33 :         return get_level(level);
      66             : }
      67             : 
      68             : spdlog::level::level_enum
      69          66 : gkfs::log::get_level(unsigned long level) {
      70          66 :     switch(level) {
      71             :         case 0:
      72             :             return spdlog::level::off;
      73           0 :         case 1:
      74           0 :             return spdlog::level::critical;
      75           0 :         case 2:
      76           0 :             return spdlog::level::err;
      77           0 :         case 3:
      78           0 :             return spdlog::level::warn;
      79          33 :         case 4:
      80          33 :             return spdlog::level::info;
      81           0 :         case 5:
      82           0 :             return spdlog::level::debug;
      83          33 :         default:
      84          33 :             return spdlog::level::trace;
      85             :     }
      86             : }
      87             : 
      88             : void
      89          33 : gkfs::log::setup(const vector<string>& loggers_name,
      90             :                  spdlog::level::level_enum level, const string& path) {
      91             : 
      92             :     /* Create common sink */
      93          33 :     auto file_sink = make_shared<spdlog::sinks::basic_file_sink_mt>(path);
      94             : 
      95             :     /* Create and configure loggers */
      96          66 :     auto loggers = list<shared_ptr<spdlog::logger>>();
      97         132 :     for(const auto& name : loggers_name) {
      98         198 :         auto logger = make_shared<spdlog::logger>(name, file_sink);
      99          99 :         logger->flush_on(spdlog::level::trace);
     100          99 :         loggers.push_back(logger);
     101             :     }
     102             : 
     103             :     /* register loggers */
     104         132 :     for(const auto& logger : loggers) {
     105         297 :         spdlog::register_logger(logger);
     106             :     }
     107             : 
     108             :     // set logger format
     109          66 :     spdlog::set_pattern("[%C-%m-%d %H:%M:%S.%f] %P [%L][%n] %v");
     110             : 
     111          33 :     spdlog::set_level(level);
     112          33 : }

Generated by: LCOV version 1.16