This file is an example of how to use the current module name to select the Log4cxx configuration file.
#include "config.h"
#ifdef _WIN32
#include <windows.h>
#elif __APPLE__
#include <mach-o/dyld.h>
#elif (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
#include <unistd.h>
#else
#include <cstring>
#endif
namespace {
auto DefaultConfigurationFileNames(std::string& altPrefix) -> std::vector<std::string> {
std::vector<std::string> result;
static const int bufSize = 4096;
char buf[bufSize+1] = {0}, pathSepar = '/';
uint32_t bufCount = 0;
#if defined(_WIN32)
GetModuleFileName(NULL, buf, bufSize);
pathSepar = '\\';
#elif defined(__APPLE__)
bufCount = bufSize;
_NSGetExecutablePath(buf, &bufCount);
#elif (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE >= 500) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L)
std::ostringstream exeLink;
exeLink << "/proc/" << getpid() << "/exe";
bufCount = readlink(exeLink.str().c_str(), buf, bufSize);
if (0 < bufCount)
buf[bufCount] = 0;
#else
strncpy(buf, "auto-configured", bufSize);
#endif
std::string programFileName(buf);
auto slashIndex = programFileName.rfind(pathSepar);
if (std::string::npos != slashIndex) {
altPrefix = programFileName.substr(0, slashIndex + 1);
{
LogString msg = LOG4CXX_STR(
"Alternate prefix [");
msg += LOG4CXX_STR("]");
}
result.push_back(programFileName.substr(slashIndex + 1));
{
LogString msg(LOG4CXX_STR(
"Alternate configuration file name ["));
msg += LOG4CXX_STR("]");
}
auto dotIndex = result.back().rfind('.');
if (std::string::npos != dotIndex)
{
result.push_back(result.back());
result.back().erase(dotIndex);
{
LogString msg(LOG4CXX_STR(
"Alternate configuration file name ["));
msg += LOG4CXX_STR("]");
}
}
}
else if (!programFileName.empty()) {
auto dotIndex = result.back().rfind('.');
if (std::string::npos != dotIndex) {
programFileName.erase(dotIndex);
result.push_back(programFileName);
{
LogString msg(LOG4CXX_STR(
"Alternate configuration file name ["));
msg += LOG4CXX_STR("]");
}
}
}
result.push_back("log4cxx");
result.push_back("log4j");
return result;
}
void SelectConfigurationFile() {
#if defined(_DEBUG)
#endif
const char* extension[] = { ".xml", ".properties", 0 };
std::string altPrefix;
for (auto baseName : DefaultConfigurationFileNames(altPrefix)) {
int i = 0;
for (; extension[i]; ++i) {
File current_working_dir_candidate(baseName + extension[i]);
if (current_working_dir_candidate.
exists(pool)) {
break;
}
if (!altPrefix.empty()) {
File alt_dir_candidate(altPrefix + baseName + extension[i]);
if (alt_dir_candidate.
exists(pool)) {
break;
}
}
}
if (extension[i])
return;
}
}
}
namespace com { namespace foo {
auto getLogger(
const std::string& name) ->
LoggerPtr {
static struct log4cxx_initializer {
log4cxx_initializer() {
SelectConfigurationFile();
}
~log4cxx_initializer() {
LogManager::shutdown();
}
} initialiser;
return name.empty()
? LogManager::getRootLogger()
: LogManager::getLogger(name);
}
} }
static void configure(const LayoutPtr &layout=LayoutPtr())
Add a ConsoleAppender to the root logger that formats output using layout.
static void setConfigurationFileName(const LogString &path)
Make path the configuration file used by configure().
static void setConfigurationWatchSeconds(int seconds)
Make seconds the time a background thread will delay before checking for a change to the configuratio...
An abstract representation of file and directory path names.
Definition file.h:41
bool exists(log4cxx::helpers::Pool &p) const
Determines if file exists.
LogString getPath() const
Get file path.
static void setInternalDebugging(bool newValue)
Start/stop outputing debug messages if newValue is true/false respectively.
static bool isDebugEnabled()
Are debug messages sent to SystemErrWriter?
static void debug(const LogString &msg)
Output msg to SystemErrWriter if internal debug logging is enabled.
static unsigned int decode(const std::string &in, std::string::const_iterator &iter)
Decodes next character from a UTF-8 string.
Definition configuration.h:25
std::basic_string< logchar > LogString
Definition logstring.h:60
std::shared_ptr< Logger > LoggerPtr
Definition defaultloggerfactory.h:27