log4cplus: Dynamic loading configuration script, how to slove this problem? THX

when I use "ConfigureAndWatchThread" in main function, it's work

int main()
{
    ......
    Logger root = Logger::getRoot();
    log4cplus::ConfigureAndWatchThread configureThread("log4cplus.properties", 1 * 1000);
    ......
}

buf, if I use "ConfigureAndWatchThread" in other function, dynamic load configure doesn't work! codes as follow:

void b()
{
    ConfigureAndWatchThread configureThread("log4cplus.properties", 5 * 1000);
}
void a()
{
    b();
}

int main()
{
    ......
    Logger root = Logger::getRoot();
    a();
    ........
}

1 answer

  • answered 2018-11-08 08:39 wilx

    This is basic variable lifetime issue. The configureThread variable goes out of scope in b() and that kills the whole thread that is watching for configuration changes. While, when you have it in main(), it stays alive as long as you do not exit main() which is for the entirety of your application run time.