1 // Copyright (C) 2020 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) 2 // This Source Code Form is subject to the terms of the Mozilla Public 3 // License, v. 2.0. If a copy of the MPL was not distributed with this 4 // file, You can obtain one at http://mozilla.org/MPL/2.0/. 5 6 #include <iostream> 7 8 #include <vsomeip/runtime.hpp> 9 10 #include "../include/logger_impl.hpp" 11 #include "../../configuration/include/configuration.hpp" 12 13 namespace vsomeip_v3 { 14 namespace logger { 15 16 std::mutex logger_impl::mutex__; 17 18 void init(const std::shared_ptr<configuration> & _configuration)19logger_impl::init(const std::shared_ptr<configuration> &_configuration) { 20 std::lock_guard<std::mutex> its_lock(mutex__); 21 auto its_logger = logger_impl::get(); 22 its_logger->configuration_ = _configuration; 23 24 #ifdef USE_DLT 25 # define VSOMEIP_LOG_DEFAULT_CONTEXT_ID "VSIP" 26 # define VSOMEIP_LOG_DEFAULT_CONTEXT_NAME "vSomeIP context" 27 28 std::string its_context_id = runtime::get_property("LogContext"); 29 if (its_context_id == "") 30 its_context_id = VSOMEIP_LOG_DEFAULT_CONTEXT_ID; 31 32 DLT_REGISTER_CONTEXT(its_logger->dlt_, its_context_id.c_str(), VSOMEIP_LOG_DEFAULT_CONTEXT_NAME); 33 #endif 34 } 35 ~logger_impl()36logger_impl::~logger_impl() { 37 #ifdef USE_DLT 38 DLT_UNREGISTER_CONTEXT(dlt_); 39 #endif 40 } 41 42 std::shared_ptr<configuration> get_configuration() const43logger_impl::get_configuration() const { 44 return configuration_; 45 } 46 47 #ifdef USE_DLT 48 void log(level_e _level,const char * _data)49logger_impl::log(level_e _level, const char *_data) { 50 51 // Prepare log level 52 DltLogLevelType its_level; 53 switch (_level) { 54 case level_e::LL_FATAL: 55 its_level = DLT_LOG_FATAL; 56 break; 57 case level_e::LL_ERROR: 58 its_level = DLT_LOG_ERROR; 59 break; 60 case level_e::LL_WARNING: 61 its_level = DLT_LOG_WARN; 62 break; 63 case level_e::LL_INFO: 64 its_level = DLT_LOG_INFO; 65 break; 66 case level_e::LL_DEBUG: 67 its_level = DLT_LOG_DEBUG; 68 break; 69 case level_e::LL_VERBOSE: 70 its_level = DLT_LOG_VERBOSE; 71 break; 72 default: 73 its_level = DLT_LOG_DEFAULT; 74 }; 75 76 DLT_LOG_STRING(dlt_, its_level, _data); 77 } 78 #endif 79 80 static std::shared_ptr<logger_impl> *the_logger_ptr__(nullptr); 81 static std::mutex the_logger_mutex__; 82 83 std::shared_ptr<logger_impl> get()84logger_impl::get() { 85 #ifndef _WIN32 86 std::lock_guard<std::mutex> its_lock(the_logger_mutex__); 87 #endif 88 if (the_logger_ptr__ == nullptr) { 89 the_logger_ptr__ = new std::shared_ptr<logger_impl>(); 90 } 91 if (the_logger_ptr__ != nullptr) { 92 if (!(*the_logger_ptr__)) { 93 *the_logger_ptr__ = std::make_shared<logger_impl>(); 94 } 95 return (*the_logger_ptr__); 96 } 97 return (nullptr); 98 } 99 100 #ifndef _WIN32 101 static void logger_impl_teardown(void) __attribute__((destructor)); logger_impl_teardown(void)102static void logger_impl_teardown(void) 103 { 104 if (the_logger_ptr__ != nullptr) { 105 std::lock_guard<std::mutex> its_lock(the_logger_mutex__); 106 the_logger_ptr__->reset(); 107 delete the_logger_ptr__; 108 the_logger_ptr__ = nullptr; 109 } 110 } 111 #endif 112 113 } // namespace logger 114 } // namespace vsomeip_v3 115