LCOV - code coverage report
Current view: top level - libs/logger - logger.cpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 24 31 77.4 %
Date: 2018-12-05 17:11:35 Functions: 12 15 80.0 %

          Line data    Source code
       1             : /*
       2             : Copyright Soramitsu Co., Ltd. 2016 All Rights Reserved.
       3             : 
       4             : Licensed under the Apache License, Version 2.0 (the "License");
       5             : you may not use this file except in compliance with the License.
       6             : You may obtain a copy of the License at
       7             : 
       8             :      http://www.apache.org/licenses/LICENSE-2.0
       9             : 
      10             : Unless required by applicable law or agreed to in writing, software
      11             : distributed under the License is distributed on an "AS IS" BASIS,
      12             : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13             : See the License for the specific language governing permissions and
      14             : limitations under the License.
      15             : */
      16             : 
      17             : #include "logger/logger.hpp"
      18             : 
      19             : namespace logger {
      20             :   const std::string end = "\033[0m";
      21             : 
      22             :   std::string red(const std::string &string) {
      23           1 :     const std::string red_start = "\033[31m";
      24           1 :     return red_start + string + end;
      25           1 :   }
      26             : 
      27             :   std::string yellow(const std::string &string) {
      28           1 :     const std::string yellow_start = "\033[33m";
      29           1 :     return yellow_start + string + end;
      30           1 :   }
      31             : 
      32             :   std::string output(const std::string &string) {
      33           0 :     return yellow("---> " + string);
      34           0 :   }
      35             : 
      36             :   std::string input(const std::string &string) {
      37           0 :     return red("<--- " + string);
      38           0 :   }
      39             : 
      40             :   static void setGlobalPattern(spdlog::logger &logger) {
      41           0 :     logger.set_pattern("[%Y-%m-%d %H:%M:%S.%F] %n %v");
      42           0 :   }
      43             : 
      44             :   static void setDebugPattern(spdlog::logger &logger) {
      45        1446 :     logger.set_pattern("[%Y-%m-%d %H:%M:%S.%F][th:%t][%l] %n %v");
      46        1446 :   }
      47             : 
      48             :   static std::shared_ptr<spdlog::logger> createLogger(const std::string &tag,
      49             :                                                       bool debug_mode = true) {
      50        1446 :     auto logger = spdlog::stdout_color_mt(tag);
      51        1446 :     if (debug_mode) {
      52        1446 :       setDebugPattern(*logger);
      53        1446 :     } else {
      54           0 :       setGlobalPattern(*logger);
      55             :     }
      56        1446 :     return logger;
      57        1446 :   }
      58             : 
      59             :   Logger log(const std::string &tag) {
      60       21985 :     static std::mutex mutex;
      61       21985 :     std::lock_guard<std::mutex> lock(mutex);
      62       21985 :     auto logger = spdlog::get(tag);
      63       21985 :     if (logger == nullptr) {
      64        1446 :       logger = createLogger(tag);
      65        1446 :     }
      66       21985 :     return logger;
      67       21985 :   }
      68             : 
      69             :   Logger testLog(const std::string &tag) {
      70           6 :     return log(tag);
      71             :   }
      72             : 
      73             :   std::string boolRepr(bool value) {
      74         494 :     return value ? "true" : "false";
      75             :   }
      76             : 
      77             : }  // namespace logger

Generated by: LCOV version 1.13