Apache Log4cxx  Version 1.6.1
Loading...
Searching...
No Matches
com/foo/config-qt.cpp

This file is an example of how to use the Qt event loop to monitor the Log4cxx configuration file.

/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config-qt.h"
#include <QCoreApplication>
#include <QFileInfo>
#include <QDir>
namespace com { namespace foo {
using namespace log4cxx;
// Provide the name of the configuration file to Log4cxx.
// Reload the configuration on a QFileSystemWatcher::fileChanged event.
void ConfigureLogging() {
static struct log4cxx_initializer {
log4cxx_initializer() {
qInstallMessageHandler(qt::messageHandler);
}
~log4cxx_initializer() {
}
} initialiser;
QFileInfo app{QCoreApplication::applicationFilePath()};
QString basename{app.baseName()};
QVector<QString> paths =
{ QString(".")
, app.absoluteDir().absolutePath()
};
QVector<QString> names =
{ QString(basename + ".xml")
, QString(basename + ".properties")
, QString("MyApp.properties")
, QString("log4cxx.xml")
, QString("log4cxx.properties")
};
auto selectedPath = QString();
std::tie(status, selectedPath) = qt::Configuration::configureFromFileAndWatch(paths, names);
BasicConfigurator::configure(); // Send events to the console
}
// Retrieve the \c name logger pointer.
auto getLogger(const QString& name) -> LoggerPtr {
return name.isEmpty()
: LogManager::getLogger(name.toStdString());
}
// Retrieve the \c name logger pointer.
auto getLogger(const char* name) -> LoggerPtr {
return name
}
} } // namespace com::foo
static void configure(const LayoutPtr &layout=LayoutPtr())
Add a ConsoleAppender that formats output using layout to the root logger.
static LoggerPtr getRootLogger()
Retrieve the root logger from the LoggerRepository.
static LoggerPtr getLogger(const std::string &name)
Retrieve the name Logger instance from the LoggerRepository using the default factory to create it if...
static void shutdown()
Safely close and remove all appenders in all loggers including the root logger.
static std::tuple< spi::ConfigurationStatus, QString > configureFromFileAndWatch(const QVector< QString > &directories, const QVector< QString > &filenames)
Select the file to configure Log4cxx and watch the file for changes.
LOG4CXX_EXPORT void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &message)
The messageHandler function is a log4cxx replacement of the standard Qt message handler.
@ NotConfigured
Definition configurator.h:33
Definition configuration.h:26
std::shared_ptr< Logger > LoggerPtr
Definition defaultloggerfactory.h:27