8

Debug assertion fails when trying to write to Log4cpp Stream

 2 years ago
source link: https://www.codesd.com/item/debug-assertion-fails-when-trying-to-write-to-log4cpp-stream.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Debug assertion fails when trying to write to Log4cpp Stream

advertisements

I'm trying to enable logging with log4cpp in the following way.

class Foo
{
    private:
        log4cpp::Appender* _logAppender;
        log4cpp::Layout* _logAppenderLayout;
}

Foo::Foo()
{
    _logAppender = new log4cpp::FileAppender("foo", "logs/bar.log"));
    _logAppenderLayout = new log4cpp::BasicLayout();
    _logAppender.setLayout(_logAppenderLayout);
    log4cpp::Category::getRoot().setPriority(log4cpp::Priority::DEBUG);
    log4cpp::Category::getRoot().addAppender(_logAppender);

    // Crash on line below.
    log4cpp::Category::getRoot().debugStream() << "test";
}

When I get to the line where I try to write "test" to the log, I get a crash that says "Debug Assertion Failed!" The assertion is in f:\dd\vctools\crt_bld_self_64_amd64\crt\src\write.c Line 67. The assert that fails is

fh >= 0 && (unsigned)fh < (unsigned)_nhandle

I have created the logs directory and the bar.log file to make sure it exists. I have also confirmed that both my application and the library were built as 64-bit multithreaded debug DLLs. There was no 64 bit build in the log4cpp source, so I created one based on the 32-bit build configuration. I'm using the latest version of log4cpp.


It is old post, but I guess solution for this problem may be useful for somebody.

Most probably you just forgot to create directory "logs" that is in your code.

This is the problem of closed stream: logger does not auto-create directories for your logs, so, no directory -> no file -> open file failed -> invalid file handler -> exception. You should create directories manually. Macros assertions and no more info is sad.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK