|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +#include "config-qt.h" |
| 18 | +#include <log4cxx/logmanager.h> |
| 19 | +#include <log4cxx-qt/configuration.h> |
| 20 | +#include <log4cxx/helpers/loglog.h> |
| 21 | +#include <QCoreApplication> |
| 22 | +#include <QVector> |
| 23 | +#include <QFileInfo> |
| 24 | +#include <QDir> |
| 25 | + |
| 26 | +namespace com { namespace foo { |
| 27 | + |
| 28 | +// Provide the name of the configuration file to Log4cxx. |
| 29 | +// Reload the configuration on a QFileSystemWatcher::fileChanged event. |
| 30 | +void ConfigureLogging() { |
| 31 | + static struct log4cxx_finalizer { |
| 32 | + ~log4cxx_finalizer() { |
| 33 | + log4cxx::LogManager::shutdown(); |
| 34 | + } |
| 35 | + } finaliser; |
| 36 | + QFileInfo app{QCoreApplication::applicationFilePath()}; |
| 37 | + QString basename{app.baseName()}; |
| 38 | + QVector<QString> paths = |
| 39 | + { QString(".") |
| 40 | + , app.absoluteDir().absolutePath() |
| 41 | + }; |
| 42 | + QVector<QString> names = |
| 43 | + { QString(basename + ".xml") |
| 44 | + , QString(basename + ".properties") |
| 45 | + , QString("MyApp.properties") |
| 46 | + , QString("log4cxx.xml") |
| 47 | + , QString("log4cxx.properties") |
| 48 | + , QString("log4j.xml") |
| 49 | + , QString("log4j.properties") |
| 50 | + }; |
| 51 | +#if defined(_DEBUG) |
| 52 | + log4cxx::helpers::LogLog::setInternalDebugging(true); |
| 53 | +#endif |
| 54 | + log4cxx::qt::Configuration::configureFromFileAndWatch(paths, names); |
| 55 | +} |
| 56 | + |
| 57 | +// Retrieve the \c name logger pointer. |
| 58 | +auto getLogger(const QString& name) -> LoggerPtr { |
| 59 | + return name.isEmpty() |
| 60 | + ? log4cxx::LogManager::getRootLogger() |
| 61 | + : log4cxx::LogManager::getLogger(name.toStdString()); |
| 62 | +} |
| 63 | + |
| 64 | +// Retrieve the \c name logger pointer. |
| 65 | +auto getLogger(const char* name) -> LoggerPtr { |
| 66 | + return name |
| 67 | + ? log4cxx::LogManager::getLogger(name) |
| 68 | + : log4cxx::LogManager::getRootLogger(); |
| 69 | +} |
| 70 | + |
| 71 | +} } // namespace com::foo |
0 commit comments