Coverage Report

Created: 2026-05-16 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/log4cplus/src/loggerimpl.cxx
Line
Count
Source
1
// Module:  Log4CPLUS
2
// File:    loggerimpl.cxx
3
// Created: 6/2001
4
// Author:  Tad E. Smith
5
//
6
//
7
// Copyright 2001-2017 Tad E. Smith
8
//
9
// Licensed under the Apache License, Version 2.0 (the "License");
10
// you may not use this file except in compliance with the License.
11
// You may obtain a copy of the License at
12
//
13
//     http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software
16
// distributed under the License is distributed on an "AS IS" BASIS,
17
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
// See the License for the specific language governing permissions and
19
// limitations under the License.
20
21
#include <log4cplus/internal/internal.h>
22
#include <log4cplus/spi/loggerimpl.h>
23
#include <log4cplus/appender.h>
24
#include <log4cplus/hierarchy.h>
25
#include <log4cplus/helpers/loglog.h>
26
#include <log4cplus/spi/loggingevent.h>
27
#include <log4cplus/spi/rootlogger.h>
28
#include <log4cplus/thread/syncprims-pub-impl.h>
29
30
31
namespace log4cplus { namespace spi {
32
33
//////////////////////////////////////////////////////////////////////////////
34
// Logger Constructors and Destructor
35
//////////////////////////////////////////////////////////////////////////////
36
LoggerImpl::LoggerImpl(const log4cplus::tstring& name_, Hierarchy& h)
37
230
  : name(name_),
38
230
    ll(NOT_SET_LOG_LEVEL),
39
230
    parent(nullptr),
40
230
    additive(true),
41
230
    hierarchy(h)
42
230
{
43
230
}
log4cplus::spi::LoggerImpl::LoggerImpl(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, log4cplus::Hierarchy&)
Line
Count
Source
37
68
  : name(name_),
38
68
    ll(NOT_SET_LOG_LEVEL),
39
68
    parent(nullptr),
40
68
    additive(true),
41
68
    hierarchy(h)
42
68
{
43
68
}
log4cplus::spi::LoggerImpl::LoggerImpl(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, log4cplus::Hierarchy&)
Line
Count
Source
37
162
  : name(name_),
38
162
    ll(NOT_SET_LOG_LEVEL),
39
162
    parent(nullptr),
40
162
    additive(true),
41
162
    hierarchy(h)
42
162
{
43
162
}
44
45
46
LoggerImpl::~LoggerImpl()
47
0
{
48
0
}
49
50
51
//////////////////////////////////////////////////////////////////////////////
52
// Logger Methods
53
//////////////////////////////////////////////////////////////////////////////
54
55
void
56
LoggerImpl::callAppenders(const InternalLoggingEvent& event)
57
697k
{
58
697k
    int writes = 0;
59
2.20M
    for(const LoggerImpl* c = this; c != nullptr; c=c->parent.get()) {
60
1.79M
        writes += c->appendLoopOnAppenders(event);
61
1.79M
        if(!c->additive) {
62
294k
            break;
63
294k
        }
64
1.79M
    }
65
66
    // No appenders in hierarchy, warn user only once.
67
697k
    if(!hierarchy.emittedNoAppenderWarning && writes == 0) {
68
0
        helpers::getLogLog().error(
69
0
            LOG4CPLUS_TEXT("No appenders could be found for logger (")
70
0
            + getName()
71
0
            + LOG4CPLUS_TEXT(")."));
72
0
        helpers::getLogLog().error(
73
0
            LOG4CPLUS_TEXT("Please initialize the log4cplus system properly."));
74
0
        hierarchy.emittedNoAppenderWarning = true;
75
0
    }
76
697k
}
77
78
79
void
80
LoggerImpl::closeNestedAppenders()
81
2.18M
{
82
2.18M
    SharedAppenderPtrList appenders = getAllAppenders();
83
2.18M
    for (auto & appenderPtr : appenders)
84
365k
    {
85
365k
        Appender & appender = *appenderPtr;
86
365k
        if (! appender.isClosed())
87
365k
            appender.close();
88
365k
    }
89
2.18M
}
90
91
92
bool
93
LoggerImpl::isEnabledFor(LogLevel loglevel) const
94
4.82M
{
95
4.82M
    if(hierarchy.disableValue >= loglevel) {
96
0
        return false;
97
0
    }
98
4.82M
    return loglevel >= getChainedLogLevel();
99
4.82M
}
100
101
102
void
103
LoggerImpl::log(LogLevel loglevel,
104
                const log4cplus::tstring& message,
105
                const char* file,
106
                int line,
107
                const char* function)
108
0
{
109
0
    if(isEnabledFor(loglevel)) {
110
0
        forcedLog(loglevel, message, file, line, function ? function : "");
111
0
    }
112
0
}
113
114
115
void
116
LoggerImpl::log(spi::InternalLoggingEvent const & ev)
117
0
{
118
0
    if (isEnabledFor(ev.getLogLevel ()))
119
0
        forcedLog(ev);
120
0
}
121
122
123
LogLevel
124
LoggerImpl::getChainedLogLevel() const
125
4.82M
{
126
9.64M
    for(const LoggerImpl *c=this; c != nullptr; c=c->parent.get()) {
127
9.64M
        if(c->ll != NOT_SET_LOG_LEVEL) {
128
4.82M
            return c->ll;
129
4.82M
        }
130
9.64M
    }
131
132
0
    helpers::getLogLog().error(
133
0
        LOG4CPLUS_TEXT("LoggerImpl::getChainedLogLevel()- No valid LogLevel found"),
134
0
        true);
135
0
    return NOT_SET_LOG_LEVEL;
136
4.82M
}
137
138
139
Hierarchy&
140
LoggerImpl::getHierarchy() const
141
0
{
142
0
    return hierarchy;
143
0
}
144
145
146
bool
147
LoggerImpl::getAdditivity() const
148
0
{
149
0
    return additive;
150
0
}
151
152
153
void
154
LoggerImpl::setAdditivity(bool additive_)
155
1.93M
{
156
1.93M
    additive = additive_;
157
1.93M
}
158
159
160
void
161
LoggerImpl::forcedLog(LogLevel loglevel,
162
                      const log4cplus::tstring& message,
163
                      const char* file,
164
                      int line,
165
                      const char* function)
166
0
{
167
0
    spi::InternalLoggingEvent & ev = internal::get_ptd ()->forced_log_ev;
168
0
    assert (function);
169
0
    ev.setLoggingEvent (this->getName(), loglevel, message, file, line,
170
0
        function);
171
0
    callAppenders(ev);
172
0
}
173
174
175
void
176
LoggerImpl::forcedLog(spi::InternalLoggingEvent const & ev)
177
697k
{
178
697k
    callAppenders(ev);
179
697k
}
180
181
182
} } // namespace log4cplus { namespace spi {