PDA

View Full Version : How to integrate Log4Qt into a project?



kei
7th August 2012, 18:17
Hi, i currently try to integrate Log4Qt (http://gitorious.org/log4qt/) into my project but nothing seems to work. My setup as following:

/path-to-programming-stuff/librarys/log4qt/ <- Clone of the git repository of log4qt
/path-to-programming-stuff/librarys/log4qt-install/ <- Compiled log4qt libraries after installing
/path-to-programming-stuff/qt/TestProject/ <- Project which should use log4qt

The test project is a simple out of the box qt project with a mainwindow. To be exact it's the thing you get if you create a new qt widget project.

My first try was to compile log4qt according to the readme as a shared library with mingw of the qt installation. Make and make install worked perfectly.
My pro file:

QT += core gui

TARGET = LogTest
TEMPLATE = app

INCLUDEPATH += /path-to-programming-stuff/librarys/Log4Qt-install/include

LIBS += -L /path-to-programming-stuff/librarys/Log4Qt-install/lib libLog4Qt

SOURCES += main.cpp \
MainWindow.cpp

HEADERS += MainWindow.h

FORMS += MainWindow.ui

And the MainWindow.cpp:

#include <log4qt/ConsoleAppender>
#include <log4qt/LogManager>
#include <log4qt/Logger>
#include <log4qt/TTCCLayout>
#include <QDebug>

#include "MainWindow.h"
#include "ui_MainWindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);

qDebug() << "Help";

Log4Qt::LogManager::rootLogger();
Log4Qt::TTCCLayout *p_layout = new Log4Qt::TTCCLayout();

p_layout->setName(QLatin1String("My Layout"));
p_layout->activateOptions();

// Create an appender
Log4Qt::ConsoleAppender *p_appender = new Log4Qt::ConsoleAppender(p_layout, Log4Qt::ConsoleAppender::STDOUT_TARGET);
p_appender->setName(QLatin1String("My Appender"));
p_appender->activateOptions();

// Set appender on root logger
Log4Qt::Logger::rootLogger()->addAppender(p_appender);

Log4Qt::Logger::logger(QLatin1String("My Logger"))->info("Hello World!");
}

MainWindow::~MainWindow()
{
delete ui;
}

The directory "/path-to-programming-stuff/librarys/log4qt-install/bin/" with the compiled library is added to the project properties (PATH). This compiles but crashes at runtime with "The program has unexpectedly finished." No error message.

Second idea was to use include() and the Log4Qt.pri file:

QT += core gui

TARGET = LogTest
TEMPLATE = app

include(/path-to-programming-stuff/librarys/log4qt/src/Log4Qt.pri)

SOURCES += main.cpp \
MainWindow.cpp

HEADERS += MainWindow.h

FORMS += MainWindow.ui

This doesn't compile and gives following error:

In file included from ..\..\librarys\log4qt\src\appenderskeleton.cpp:38:
..\..\librarys\log4qt\src\/spi/filter.h:33:31: error: helpers/logobject.h: No such file or directory
..\..\librarys\log4qt\src\/spi/filter.h:35:34: error: helpers/logobjectptr.h: No such file or directory
mingw32-make.exe[1]: *** [release/appenderskeleton.o] Error 1
mingw32-make.exe: *** [release] Error 2

Also the includes in MainWindow.cpp don't work anymore. No log4qt class can be found with qt creator.

Does anyone has an idea?

d_stranz
8th August 2012, 05:18
The directory "/path-to-programming-stuff/librarys/log4qt-install/bin/" with the compiled library is added to the project properties (PATH). This compiles but crashes at runtime with "The program has unexpectedly finished." No error message.


Ever thought about running your program in the debugger and actually seeing what's going wrong by looking at the call stack when it crashes?

It simply astounds me that people compile their programs in release mode, run it, it crashes, and then are amazed that they get a message box that tells them "The program has unexpectedly finished" with no other information. That's what compiling in debug mode and running under a debugger is for. It tells you why the program unexpectedly finished, and the answer is usually that you made a programming error that caused the crash, and the debugger usually points you to exactly the line in your code that is responsible for it.

kei
8th August 2012, 17:15
Sorry forgot to mention but yeah i did that, too. It crashes with "Executable failed: During startup program exited with code 0x0000139". Some sources say this means there is a dll missing. But even if i copy all dlls in the same folder as the executable it crashes with "The application was unable to start correctly (0xc0000005)".

Eumcoz
9th August 2012, 22:53
I would go for method two, http://log4qt.sourceforge.net/ has an easy guide, but it seems like you did that. Make sure the paths exists, and the case is right(my log4qt.pri is all lowercase). Make sure that your slashes are the correct way, it's weird how your error:


..\..\librarys\log4qt\src\/spi/filter.h

has \ during your path and / during the path to the library.

My sample include is


include(./Utilities/log4qt/src/log4qt/log4qt.pri)

It's probably a typo, so go over everything carefully.

kei
12th August 2012, 16:24
After playing a bit i found that it works with the original version (from sourceforge) but not with the newer fork (from gitorious). Currently i'm using the original version which works fine. I also got the fork working but not in a nice manner (building it with the given .pro-file and including the source folder which converts to ugly includes). The fork gives an installation description which consists of compiling with cmake and installing with mingw (which copies necessary files into the qt sdk folder and registers it). After this you can include everything in a nice manner (e.g. #include <Log4Qt/ConsoleAppender>) and you only need to add "CONFIG+=Log4Qt" in the .pro-file. Well in theory. But when compiling it fails and cannot find the log4qt classes.

So far thx for the help.

d_stranz
13th August 2012, 22:15
"The application was unable to start correctly (0xc0000005)"

If this is a Windows platform, this means you have a segmentation fault due to either trying to use a NULL pointer or a pointer that contains garbage because it is uninitialized. Put a breakpoint in main() and start stepping through until you hit the crash, and there's your bad pointer.

And in Visual Studio at least, you generally cannot mix-and-match debug and release mode DLLs and executables. If you're putting release mode DLLs into a folder and then trying to run debug-mode apps, you could be running into trouble because the memory layout of release and debug mode objects is different.