0

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.

Alex Wade
  • 659
  • 1
  • 7
  • 27
  • Maybe log4cpp needs more than just a configuration change for a 64-bit version. There's a lot of gotchas to worry about with pointer and HANDLE sizes, for example. – Roger Rowland Apr 12 '13 at 14:02
  • I was thinking that may be the case. I'm looking at other logging frameworks now, but hopefully somebody who has used log4cpp with a 64-bit application sees this. – Alex Wade Apr 12 '13 at 15:30
  • To anyone who comes across this question, I wasn't able to get this working. My solution was to move over to google-glog https://code.google.com/p/google-glog/. It doesn't have a built in 64-bit configuration, but it was easy to create. – Alex Wade Apr 19 '13 at 13:10

2 Answers2

1

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.

Yorie
  • 151
  • 1
  • 4
  • I manually created the logs directory and the log file to be sure, and still got the crash. No issues in 32 bit. I will be revisiting this issue soon, although I found 64-bit log4cxx binaries that I know work. This means I won't have to figure out what was wrong with my build. – Alex Wade Aug 07 '13 at 17:01
  • Ok, I'm sorry. Probably your problem has another goal, it is strange that we have two equal errors with different causes.Sorry, but whatever I recommend replace "logs/bar.log" with "bar.log" for testing purposes. – Yorie Aug 08 '13 at 02:26
  • I tried that and got no difference. Also, the way I had it worked fine on the 32 bit version. – Alex Wade Aug 23 '13 at 00:01
0

Just discovered this question and tried to get this assertion. I have built log4cpp (ver 1.1) library and user1229080's test for Win32 and x64 platforms in MSVC2010 and have got no assertion.

I added few lines to get it compiled, and removed "logs" dir from the file path just to get rid of issues related to absent directories:

#include "stdafx.h"

#include <log4cpp/Category.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/BasicLayout.hh>

class Foo
{
    private:
        log4cpp::Appender* _logAppender, *_conAppender;
        log4cpp::Layout* _logAppenderLayout;
    public:
        Foo();
};

Foo::Foo()
{
    _conAppender = new log4cpp::OstreamAppender("con", &std::cout);
    _logAppender = new log4cpp::FileAppender("foo", "bar.log");
    _logAppenderLayout = new log4cpp::BasicLayout();
    _logAppender->setLayout(_logAppenderLayout);

    log4cpp::Category::getRoot().setPriority(log4cpp::Priority::DEBUG);
    log4cpp::Category::getRoot().addAppender(_logAppender);
    log4cpp::Category::getRoot().addAppender(_conAppender);


    // Crash on line below. (but not in msvc2010)
    log4cpp::Category::getRoot().debugStream() << "test" << log4cpp::eol;
}

int main(int argc, char* argv[]) {
    Foo f;

    return 0;
}

Which version of visual c++ you encountered the assertion on?

Alex InTechno
  • 1,181
  • 1
  • 7
  • 9