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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright (C) 2011-2024 Internet Systems Consortium, Inc. ("ISC")
//
// 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 <config.h>


#include <algorithm><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <cstring><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iomanip><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <iostream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stdarg.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stdio.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <sstream><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <boost/make_shared.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/lexical_cast.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/static_assert.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <boost/algorithm/string.hpp><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <log4cplus/configurator.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <log4cplus/consoleappender.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <log4cplus/fileappender.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <log4cplus/loggingmacros.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <log4cplus/syslogappender.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <log4cplus/version.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <log/logger.h>
#include <log/logger_impl.h>
#include <log/logger_level.h>
#include <log/logger_level_impl.h>
#include <log/logger_name.h>
#include <log/logger_manager.h>
#include <log/message_dictionary.h>
#include <log/message_types.h>
#include <log/interprocess/interprocess_sync_file.h>
#include <log/interprocess/interprocess_sync_null.h>

// Note: as log4cplus and the Kea logger have many concepts in common, and
// thus many similar names, to disambiguate types we don't "use" the log4cplus
// namespace: instead, all log4cplus types are explicitly qualified.

using namespace std;

namespace isc {
namespace log {

/// @brief detects whether file locking is enabled or disabled
///
/// The lockfile is enabled by default. The only way to disable it is to
/// set KEA_LOCKFILE_DIR variable to 'none'.
/// @return true if lockfile is enabled, false otherwise
bool lockfileEnabled() {
    const char* const env = getenv("KEA_LOCKFILE_DIR");
    if (env && boost::iequals(string(env), string("none"))) {
        return (false);
    }

    return (true);
}

// Constructor.  The setting of logger_ must be done when the variable is
// constructed (instead of being left to the body of the function); at least
// one compiler requires that all member variables be constructed before the
// constructor is run, but log4cplus::Logger (the type of logger_) has no
// default constructor.
LoggerImpl::LoggerImpl(const string& name) :
    name_(expandLoggerName(name)),
    logger_(log4cplus::Logger::getInstance(name_))
{
    if (lockfileEnabled()) {
        sync_ = new interprocess::InterprocessSyncFile("logger");
    } else {
        sync_ = new interprocess::InterprocessSyncNull("logger");
    }
}

// Destructor. (Here because of virtual declaration.)

LoggerImpl::~LoggerImpl() {
    delete sync_;
}

/// \brief Version
std::string
LoggerImpl::getVersion() {
    std::ostringstream ver;
    ver << "log4cplus ";
    ver << log4cplus::versionStr;
    return (ver.str());
}

// Set the severity for logging.
void
LoggerImpl::setSeverity(isc::log::Severity severity, int dbglevel) {
    Level level(severity, dbglevel);
    logger_.setLogLevel(LoggerLevelImpl::convertFromBindLevel(level));
}

// Return severity level
isc::log::Severity
LoggerImpl::getSeverity() {
    Level level = LoggerLevelImpl::convertToBindLevel(logger_.getLogLevel());
    return level.severity;
}

// Return current debug level (only valid if current severity level is DEBUG).
int
LoggerImpl::getDebugLevel() {
    Level level = LoggerLevelImpl::convertToBindLevel(logger_.getLogLevel());
    return level.dbglevel;
}

// Get effective severity.  Either the current severity or, if not set, the
// severity of the root level.
isc::log::Severity
LoggerImpl::getEffectiveSeverity() {
    Level level = LoggerLevelImpl::convertToBindLevel(logger_.getChainedLogLevel());
    return level.severity;
}

// Return effective debug level (only valid if current effective severity level
// is DEBUG).
int
LoggerImpl::getEffectiveDebugLevel() {
    Level level = LoggerLevelImpl::convertToBindLevel(logger_.getChainedLogLevel());
    return level.dbglevel;
}


// Output a general message
boost::shared_ptr<string>
LoggerImpl::lookupMessage(const MessageID& ident) {
    return (boost::make_shared<string>(string(ident) + " " +
        MessageDictionary::globalDictionary()->getText(ident)));
}

// Replace the interprocess synchronization object

void
LoggerImpl::setInterprocessSync(interprocess::InterprocessSync* sync) {
    if (sync == NULL) {
        isc_throw(BadInterprocessSync,
                  "NULL was passed to setInterprocessSync()");
    }

    delete sync_;
    sync_ = sync;
}

void
LoggerImpl::outputRaw(const Severity& severity, const string& message) {
    // Use a mutex locker for mutual exclusion from other threads in
    // this process.
    std::lock_guard<std::mutex> mutex_locker(LoggerManager::getMutex());

    // Use an interprocess sync locker for mutual exclusion from other
    // processes to avoid log messages getting interspersed.
    interprocess::InterprocessSyncLocker locker(*sync_);

    if (!locker.lock()) {
        LOG4CPLUS_ERROR(logger_, "Unable to lock logger lockfile");
    }

    switch (severity) {
        case DEBUG:
            LOG4CPLUS_DEBUG(logger_, message);
            break;

        case INFO:
            LOG4CPLUS_INFO(logger_, message);
            break;

        case WARN:
            LOG4CPLUS_WARN(logger_, message);
            break;

        case ERROR:
            LOG4CPLUS_ERROR(logger_, message);
            break;

        case FATAL:
            LOG4CPLUS_FATAL(logger_, message);
            break;

        case NONE:
             break;

        default:
            LOG4CPLUS_ERROR(logger_,
                            "Unsupported severity in LoggerImpl::outputRaw(): "
                            << severity);
    }

    if (!locker.unlock()) {
        LOG4CPLUS_ERROR(logger_, "Unable to unlock logger lockfile");
    }
}

bool
LoggerImpl::hasAppender(OutputOption::Destination const destination) {
    // Get the appender for the name under which this logger is registered.
    log4cplus::SharedAppenderPtrList appenders(
        log4cplus::Logger::getInstance(name_).getAllAppenders());

    // If there are no appenders, they might be under the root name.
    if (appenders.size() == 0) {
        appenders = log4cplus::Logger::getInstance(getRootLoggerName()).getAllAppenders();
    }

    for (const log4cplus::helpers::SharedObjectPtr<log4cplus::Appender>& logger : appenders) {<--- Consider using std::any_of algorithm instead of a raw loop.
        if (destination == OutputOption::DEST_CONSOLE &&
            dynamic_cast<log4cplus::ConsoleAppender*>(logger.get())) {
            return true;
        } else if (destination == OutputOption::DEST_FILE &&
                   dynamic_cast<log4cplus::FileAppender*>(logger.get())) {
            return true;
        } else if (destination == OutputOption::DEST_SYSLOG &&
                   dynamic_cast<log4cplus::SysLogAppender*>(logger.get())) {
            return true;
        }
    }
    return false;
}

} // namespace log
} // namespace isc