summaryrefslogtreecommitdiff
path: root/implementation/logger/src/logger_impl.cpp
blob: b909c34e49e2707c8fe16e68e5102a1abc069ce9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (C) 2020-2021 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <iostream>

#include <vsomeip/runtime.hpp>

#include "../include/logger_impl.hpp"
#include "../../configuration/include/configuration.hpp"

namespace vsomeip_v3 {
namespace logger {

std::mutex logger_impl::mutex__;

void
logger_impl::init(const std::shared_ptr<configuration> &_configuration) {
    std::lock_guard<std::mutex> its_lock(mutex__);
    auto its_logger = logger_impl::get();
    its_logger->configuration_ = _configuration;

#ifdef USE_DLT
#   define VSOMEIP_LOG_DEFAULT_CONTEXT_ID              "VSIP"
#   define VSOMEIP_LOG_DEFAULT_CONTEXT_NAME            "vSomeIP context"

#ifndef ANDROID
    std::string its_context_id = runtime::get_property("LogContext");
    if (its_context_id == "")
        its_context_id = VSOMEIP_LOG_DEFAULT_CONTEXT_ID;

    DLT_REGISTER_CONTEXT(its_logger->dlt_, its_context_id.c_str(), VSOMEIP_LOG_DEFAULT_CONTEXT_NAME);
#endif
#endif
}

logger_impl::~logger_impl() {
#ifdef USE_DLT
#ifndef ANDROID
    DLT_UNREGISTER_CONTEXT(dlt_);
#endif
#endif
}

std::shared_ptr<configuration>
logger_impl::get_configuration() const {
    return configuration_;
}

#ifdef USE_DLT
#ifndef ANDROID
void
logger_impl::log(level_e _level, const char *_data) {

    // Prepare log level
    DltLogLevelType its_level;
    switch (_level) {
    case level_e::LL_FATAL:
        its_level = DLT_LOG_FATAL;
        break;
    case level_e::LL_ERROR:
        its_level = DLT_LOG_ERROR;
        break;
    case level_e::LL_WARNING:
        its_level = DLT_LOG_WARN;
        break;
    case level_e::LL_INFO:
        its_level = DLT_LOG_INFO;
        break;
    case level_e::LL_DEBUG:
        its_level = DLT_LOG_DEBUG;
        break;
    case level_e::LL_VERBOSE:
        its_level = DLT_LOG_VERBOSE;
        break;
    default:
        its_level = DLT_LOG_DEFAULT;
    };

    DLT_LOG_STRING(dlt_, its_level, _data);
}
#endif
#endif

static std::shared_ptr<logger_impl> *the_logger_ptr__(nullptr);
static std::mutex the_logger_mutex__;

std::shared_ptr<logger_impl>
logger_impl::get() {
#if defined(__linux__) || defined(ANDROID)
    std::lock_guard<std::mutex> its_lock(the_logger_mutex__);
#endif
    if (the_logger_ptr__ == nullptr) {
        the_logger_ptr__ = new std::shared_ptr<logger_impl>();
    }
    if (the_logger_ptr__ != nullptr) {
        if (!(*the_logger_ptr__)) {
            *the_logger_ptr__ = std::make_shared<logger_impl>();
        }
        return (*the_logger_ptr__);
    }
    return (nullptr);
}

#if defined(__linux__) || defined(ANDROID)
static void logger_impl_teardown(void) __attribute__((destructor));
static void logger_impl_teardown(void)
{
    // TODO: This mutex is causing a crash due to changes in the way mutexes are defined.
    // Since this function only runs on the main thread, no mutex should be needed. Leaving a 
    // comment pending a refactor.
    // std::lock_guard<std::mutex> its_lock(the_logger_mutex__);
    if (the_logger_ptr__ != nullptr) {
        the_logger_ptr__->reset();
        delete the_logger_ptr__;
        the_logger_ptr__ = nullptr;
    }
}
#endif

} // namespace logger
} // namespace vsomeip_v3