0

I have a properties file for log4cxx configuration with a variable name ${name}:

Properties file example:

log4j.rootLogger=INFO, FILE

# FILE
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.File=./${name}
log4j.appender.FILE.MaxFileSize=16MB
log4j.appender.FILE.MaxBackupIndex=10
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p %m%n

How can I set the variable ${name} from my cpp file or cmake file. I tried with the following code but it doesn't work:

#include <log4cxx/logger.h>
#include <log4cxx/propertyconfigurator.h>
#include <log4cxx/helpers/properties.h>

using namespace log4cxx;

static LoggerPtr logger(Logger::getLogger(""));

int main() {
    PropertyConfigurator::configure("./assets/logger.properties");

#ifndef NDEBUG
    logger->setLevel(log4cxx::Level::getDebug());
#endif

    return EXIT_SUCCESS;
}

Thanks in advance

Carlo Corradini
  • 2,927
  • 2
  • 18
  • 24
  • Are you trying to do a textual replacement of `${name}` with the value held by the variable `name` in the properties file? The example cpp code you posted doesn't clearly express what you are trying to accomplish. How is `name` set? Is it an environment variable? – Kevin Mar 25 '20 at 16:37
  • I'm trying to emulate the example with Java like in the answer from this [https://stackoverflow.com/questions/2810926/how-to-give-dynamic-file-name-in-the-appender-in-log4j-xml](https://stackoverflow.com/questions/2810926/how-to-give-dynamic-file-name-in-the-appender-in-log4j-xml). My purpose is to have a single properties file for multiple executable and then changing the fileName for each executable – Carlo Corradini Mar 25 '20 at 17:08
  • You could use CMake to configure a *separate* `logger.properties` file for each executable, each with the name of executable in the inserted log file name. Would having a separate properties file for each executable work for your use case? – Kevin Mar 25 '20 at 17:23
  • Unfortunately it's not possible. I must have a single properties file and then the fileName set on the main or from any macro expansion from the compiler. Thanks in advance – Carlo Corradini Mar 25 '20 at 17:35

1 Answers1

0

Found out the solution: Use setenv from stdlib. Here is the working solution:

#include <stdlib.h> // Include the library
#include <log4cxx/logger.h>
#include <log4cxx/propertyconfigurator.h>
#include <log4cxx/helpers/properties.h>

using namespace log4cxx;

static LoggerPtr logger(Logger::getLogger(""));

int main() {
    // Set the env variable
    // The identifier must be the same as the one in the log4cxx configuration file
    setenv("name", "NAME_OF_THE_CURRENT_LOG_FILE", true);

    PropertyConfigurator::configure("./assets/logger.properties");

#ifndef NDEBUG
    logger->setLevel(log4cxx::Level::getDebug());
#endif

    return EXIT_SUCCESS;
}

I hope it was useful not only for me :)

Carlo Corradini
  • 2,927
  • 2
  • 18
  • 24