Coverage Report

Created: 2026-03-31 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/log4cplus/src/loglog.cxx
Line
Count
Source
1
// Module:  Log4CPLUS
2
// File:    loglog.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/streams.h>
22
#include <log4cplus/helpers/loglog.h>
23
#include <log4cplus/thread/syncprims-pub-impl.h>
24
#include <log4cplus/thread/threads.h>
25
#include <log4cplus/internal/env.h>
26
#include <log4cplus/consoleappender.h>
27
#include <log4cplus/exception.h>
28
#include <ostream>
29
#include <stdexcept>
30
31
32
namespace log4cplus { namespace helpers {
33
34
namespace
35
{
36
37
static tchar const PREFIX[] = LOG4CPLUS_TEXT("log4cplus: ");
38
static tchar const WARN_PREFIX[] = LOG4CPLUS_TEXT("log4cplus:WARN ");
39
static tchar const ERR_PREFIX[] = LOG4CPLUS_TEXT("log4cplus:ERROR ");
40
41
} // namespace
42
43
44
LogLog *
45
LogLog::getLogLog()
46
0
{
47
0
    return &helpers::getLogLog ();
48
0
}
49
50
51
LogLog::LogLog()
52
70
    : debugEnabled(TriUndef)
53
70
    , quietMode(TriUndef)
54
70
{ }
55
56
57
LogLog::~LogLog()
58
0
{ }
59
60
61
void
62
LogLog::setInternalDebugging(bool enabled)
63
0
{
64
0
    thread::MutexGuard guard (mutex);
65
66
0
    debugEnabled = enabled ? TriTrue : TriFalse;
67
0
}
68
69
70
void
71
LogLog::setQuietMode(bool quietModeVal)
72
0
{
73
0
    thread::MutexGuard guard (mutex);
74
75
0
    quietMode = quietModeVal ? TriTrue : TriFalse;
76
0
}
77
78
79
void
80
LogLog::debug(const log4cplus::tstring& msg) const
81
480k
{
82
480k
    logging_worker (tcout, &LogLog::get_debug_mode, PREFIX, msg);
83
480k
}
84
85
86
void
87
LogLog::debug(tchar const * msg) const
88
359k
{
89
359k
    logging_worker (tcout, &LogLog::get_debug_mode, PREFIX, msg);
90
359k
}
91
92
93
void
94
LogLog::warn(const log4cplus::tstring& msg) const
95
0
{
96
0
    logging_worker (tcerr, &LogLog::get_not_quiet_mode, WARN_PREFIX, msg);
97
0
}
98
99
100
void
101
LogLog::warn(tchar const * msg) const
102
0
{
103
0
    logging_worker (tcerr, &LogLog::get_not_quiet_mode, WARN_PREFIX, msg);
104
0
}
105
106
107
void
108
LogLog::error(const log4cplus::tstring& msg, bool throw_flag) const
109
0
{
110
0
    logging_worker (tcerr, &LogLog::get_not_quiet_mode, ERR_PREFIX, msg,
111
0
        throw_flag);
112
0
}
113
114
115
void
116
LogLog::error(tchar const * msg, bool throw_flag) const
117
0
{
118
0
    logging_worker (tcerr, &LogLog::get_not_quiet_mode, ERR_PREFIX, msg,
119
0
        throw_flag);
120
0
}
121
122
123
bool
124
LogLog::get_quiet_mode () const
125
0
{
126
0
    if (LOG4CPLUS_UNLIKELY (quietMode == TriUndef))
127
0
        set_tristate_from_env (&quietMode,
128
0
            LOG4CPLUS_TEXT ("LOG4CPLUS_LOGLOG_QUIETMODE"));
129
130
0
    return quietMode == TriTrue;
131
0
}
132
133
134
bool
135
LogLog::get_not_quiet_mode () const
136
0
{
137
0
    return ! get_quiet_mode ();
138
0
}
139
140
141
bool
142
LogLog::get_debug_mode () const
143
840k
{
144
840k
    if (LOG4CPLUS_UNLIKELY (debugEnabled == TriUndef))
145
36
        set_tristate_from_env (&debugEnabled,
146
36
            LOG4CPLUS_TEXT ("LOG4CPLUS_LOGLOG_DEBUGENABLED"));
147
148
840k
    return debugEnabled && ! get_quiet_mode ();
149
840k
}
150
151
152
void
153
LogLog::set_tristate_from_env (TriState * result, tchar const * envvar_name)
154
36
{
155
36
    tstring envvar_value;
156
36
    bool exists = internal::get_env_var (envvar_value, envvar_name);
157
36
    bool value = false;
158
36
    if (exists && internal::parse_bool (value, envvar_value) && value)
159
0
        *result = TriTrue;
160
36
    else
161
36
        *result = TriFalse;
162
36
}
163
164
165
template <typename StringType>
166
void
167
LogLog::logging_worker (tostream & os, bool (LogLog:: * cond) () const,
168
    tchar const * prefix, StringType const & msg, bool throw_flag) const
169
840k
{
170
840k
    bool output;
171
840k
    {
172
840k
        thread::MutexGuard guard (mutex);
173
840k
        output = (this->*cond) ();
174
840k
    }
175
176
840k
    if (LOG4CPLUS_UNLIKELY (output))
177
0
    {
178
        // XXX This is potential recursive lock of
179
        // ConsoleAppender::outputMutex.
180
0
        thread::MutexGuard outputGuard (ConsoleAppender::getOutputMutex ());
181
0
        os << prefix << msg << std::endl;
182
0
    }
183
184
840k
    if (LOG4CPLUS_UNLIKELY (throw_flag))
185
0
        throw log4cplus::exception (msg);
186
840k
}
void log4cplus::helpers::LogLog::logging_worker<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, bool (log4cplus::helpers::LogLog::*)() const, char const*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, bool) const
Line
Count
Source
169
480k
{
170
480k
    bool output;
171
480k
    {
172
480k
        thread::MutexGuard guard (mutex);
173
480k
        output = (this->*cond) ();
174
480k
    }
175
176
480k
    if (LOG4CPLUS_UNLIKELY (output))
177
0
    {
178
        // XXX This is potential recursive lock of
179
        // ConsoleAppender::outputMutex.
180
0
        thread::MutexGuard outputGuard (ConsoleAppender::getOutputMutex ());
181
0
        os << prefix << msg << std::endl;
182
0
    }
183
184
480k
    if (LOG4CPLUS_UNLIKELY (throw_flag))
185
0
        throw log4cplus::exception (msg);
186
480k
}
void log4cplus::helpers::LogLog::logging_worker<char const*>(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, bool (log4cplus::helpers::LogLog::*)() const, char const*, char const* const&, bool) const
Line
Count
Source
169
359k
{
170
359k
    bool output;
171
359k
    {
172
359k
        thread::MutexGuard guard (mutex);
173
359k
        output = (this->*cond) ();
174
359k
    }
175
176
359k
    if (LOG4CPLUS_UNLIKELY (output))
177
0
    {
178
        // XXX This is potential recursive lock of
179
        // ConsoleAppender::outputMutex.
180
0
        thread::MutexGuard outputGuard (ConsoleAppender::getOutputMutex ());
181
0
        os << prefix << msg << std::endl;
182
0
    }
183
184
359k
    if (LOG4CPLUS_UNLIKELY (throw_flag))
185
0
        throw log4cplus::exception (msg);
186
359k
}
187
188
189
} } // namespace log4cplus { namespace helpers {