|
| 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.h" |
| 18 | +#include "product_version.h" // Provides getVendorFolder() and getProductFolder() |
| 19 | +#include <log4cxx/logmanager.h> |
| 20 | +#include <log4cxx/defaultconfigurator.h> |
| 21 | +#include <log4cxx/basicconfigurator.h> |
| 22 | +#include <log4cxx/helpers/transcoder.h> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +namespace com { namespace foo { |
| 26 | + |
| 27 | +// Retrieve the \c name logger pointer. |
| 28 | +// Configure Log4cxx on the first call. |
| 29 | +auto getLogger(const std::string& name) -> LoggerPtr { |
| 30 | + using namespace log4cxx; |
| 31 | + static struct log4cxx_initializer { |
| 32 | + log4cxx_initializer() { |
| 33 | + auto vendorFolder = getVendorFolder(); |
| 34 | + auto productFolder = getProductFolder(); |
| 35 | + LOG4CXX_DECODE_CHAR(lsVendorFolder, vendorFolder); |
| 36 | + LOG4CXX_DECODE_CHAR(lsProductFolder, productFolder); |
| 37 | + |
| 38 | + // Allow expansion of ${CURRENT_VENDOR_FOLDER} and ${CURRENT_PRODUCT_FOLDER} |
| 39 | + // when loading a configuration from a file |
| 40 | + auto& props = spi::Configurator::properties(); |
| 41 | + props.setProperty(LOG4CXX_STR("CURRENT_VENDOR_FOLDER"), lsVendorFolder); |
| 42 | + props.setProperty(LOG4CXX_STR("CURRENT_PRODUCT_FOLDER"), lsProductFolder); |
| 43 | + |
| 44 | + // Check every 5 seconds for configuration file changes |
| 45 | + DefaultConfigurator::setConfigurationWatchSeconds(5); |
| 46 | + |
| 47 | + // Look for a configuration file in the current working directory |
| 48 | + // and the same directory as the program |
| 49 | + std::vector<LogString> paths |
| 50 | + { LOG4CXX_STR(".") |
| 51 | + , LOG4CXX_STR("${PROGRAM_FILE_PATH.PARENT_PATH}") |
| 52 | + }; |
| 53 | + std::vector<LogString> names |
| 54 | + { LOG4CXX_STR("${PROGRAM_FILE_PATH.STEM}.xml") |
| 55 | + , LOG4CXX_STR("${PROGRAM_FILE_PATH.STEM}.properties") |
| 56 | + }; |
| 57 | + auto status = spi::ConfigurationStatus::NotConfigured; |
| 58 | + auto selectedPath = LogString(); |
| 59 | + std::tie(status, selectedPath) = DefaultConfigurator::configureFromFile(paths, names); |
| 60 | + if (status == spi::ConfigurationStatus::NotConfigured) |
| 61 | + BasicConfigurator::configure(); // Send events to the console |
| 62 | + } |
| 63 | + ~log4cxx_initializer() { |
| 64 | + LogManager::shutdown(); |
| 65 | + } |
| 66 | + } initialiser; |
| 67 | + return name.empty() |
| 68 | + ? LogManager::getRootLogger() |
| 69 | + : LogManager::getLogger(name); |
| 70 | +} |
| 71 | + |
| 72 | +} } // namespace com::foo |
0 commit comments