log4cplus on Windows

Requirements:
VS Studio C++ 2010 Express edition and the source code can be obtained from log4cplus at sourceforge.

Unpack the log4cplus sources. Open the log4cplus.sln solution file located in the msvc8 folder. The solution explorer should contain the log4cplus_dll project.
The default target is set to DEBUG mode. Make sure to alter the target to DEBUG_UNICODE!
Open the context menu for this entry and hit compile.

The log4cplusUD.lib and log4cplusUD.dll should be created.

1>     Bibliothek ".\.\Win32\log4cplus_dll.Debug_Unicode\log4cplusUD.lib" und Objekt ".\.\Win32\log4cplus_dll.Debug_Unicode\log4cplusUD.exp" werden erstellt.
1>  log4cplus_dll.vcxproj -> *\log4cplus-1.0.4\msvc8\.\Win32\log4cplus_dll.Debug_Unicode\log4cplus_dll.dll
========== Alles neu erstellen: 1 erfolgreich, Fehler bei 0, 0 übersprungen ==========

Create some folders (e.g. bin, lib, and include) at a specific location and place the binary, the library and the header files into it.


Add the bin folder (e.g. C:\Users\Public\bin) to the path environment variable.

VS 2010 integration:
Create a new console application using VS 2010. Open the project properties and add the additional include folder. Make sure you are targeting the DEBUG mode.

Add the additional lib folder and add the required log4cplusUD.lib as follows.

Try to compile without any erros. Make sure if you target DEBUG mode that you used lib4cplusUD.lib not the lib4cplusD.lib or the lib4cplus.lib!

Modify the working directory for the debugger to the $TargetDir. Otherwise you have to fiddle the path to the properties file on your own. Take a look at the #ifdef DEBUG pragma of the main.cpp underneath.

Create a simple property file (e.g. named log.properties) for the application and place this file into the output folder where your application gets compiled to (e.g. *\Debug).
Make sure you have the privileges to create a file in the location – and this location exists – defined by the property file.

log4cplus.rootLogger=INFO, STDOUT, FILEAPPENDER
#log4cplus.logger.main=INFO
#log4cplus.logger.utils=FILEAPPENDER

log4cplus.appender.STDOUT=log4cplus::ConsoleAppender
log4cplus.appender.STDOUT.layout=log4cplus::PatternLayout
log4cplus.appender.STDOUT.layout.ConversionPattern=%d{%m/%d/%y %H:%M:%S} [%t] %-5p %c{2} %%%x%% - %m [%l]%n

log4cplus.appender.FILEAPPENDER=log4cplus::RollingFileAppender
log4cplus.appender.FILEAPPENDER.File=/tmp/consoleapplication.log
log4cplus.appender.FILEAPPENDER.MaxFileSize=5MB
#log4cplus.appender.FILEAPPENDER.MaxFileSize=500KB
log4cplus.appender.FILEAPPENDER.MaxBackupIndex=1
log4cplus.appender.FILEAPPENDER.layout=log4cplus::TTCCLayout

In VS modify the main.cpp to the following.

#include "stdafx.h"

#include
#include

#ifdef DEBUG
#include
#endif

int _tmain(int argc, wchar_t *argv[])
{
    // Use the log4cplus namespace
    using namespace log4cplus;

    // Load the properties
    #ifdef DEBUG
	std::wstring applicationPath(argv[0]);
	std::wstringstream fullqualifedPath;
	fullqualifedPath << applicationPath.substr(0, applicationPath.find_last_of(L"/\\")) << L"\\" << L"log.properties";
	PropertyConfigurator::doConfigure(fullqualifedPath.str());
#else
	PropertyConfigurator::doConfigure(L"log.properties");
#endif

    // Create the logger
    const Logger logger = Logger::getInstance("main");

    // Log with INFO level
    if (logger.isEnabledFor(INFO_LOG_LEVEL))
    {
        LOG4CPLUS_INFO(logger, "Application startup");
    }

    // Log with INFO level
    if (logger.isEnabledFor(INFO_LOG_LEVEL))
    {
        LOG4CPLUS_INFO(logger, "Application shutdown");
    }
}

Make sure you have set the build target to DEBUG not RELEASE.

Compile and run the application. If the PATH variable does not include the log4cplusUD.dll an error message should appear at startup. If the property file cannot be found, the logger will complain about not having any appenders.

log4cplus:ERROR No appenders could be found for logger (main).
log4cplus:ERROR Please initialize the log4cplus system properly.

The following output should be generated on the console.

01/08/12 06:50:05 [3079005904] INFO main %% - Application startup [../Log4CplusClient/main.cpp:18]
01/08/12 06:50:05 [3079005904] INFO main %% - Application shutdown [../Log4CplusClient/main.cpp:24]

The consoleapplication.log looks like.

2090 [3592] INFO main <> - Application startup
2106 [3592] INFO main <> - Application shutdown
Posted in Common | Tagged , , | 1 Comment

log4cplus on Ubuntu

Requirements:
A valid gcc toolchain and the source code can be obtained from log4cplus at sourceforge.
Unpack the source code and just follow the instruction from the install.txt. Make sure you have the necessary permission (sudo).

  1. Start the terminal and navigate to the location where the source code was unpacked.
    Configure the package for your system.
    sudo ./configure
  2. Compile the package.
    make
  3. Install with the necessary privileges the package.
    sudo make install
  4. Refresh the library cache.
    sudo ldconfig

The default library and header location for version 1.0.4 are /usr/local/lib and /usr/local/include. Make sure the liblog4cplus.a, liblog4cplus.so libraries and headers exists.

QT Creator integration:
Create a new console application and open the created project file.

#-------------------------------------------------
#
# Project created by QtCreator 2012-01-08T07:03:26
#
#-------------------------------------------------
QT       += core
QT       -= gui

TARGET = Log4CplusClient
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

Add the log4cplus library and header by adding the following two lines to the project file:

TEMPLATE = app

LIBS += -L/usr/local/lib/log4cplus -llog4cplus
INCLUDEPATH += -L/usr/local/include/log4cplus

SOURCES += main.cpp

And try to compile it without any errors.

Create a simple property file (e.g. named log.properties) for the application and place this file into the output folder where your application gets compiled to (e.g. *-build-desktop).
Make sure you have the privileges to create a file in the location defined by the property file.

log4cplus.rootLogger=INFO, STDOUT, FILEAPPENDER
#log4cplus.logger.main=INFO
#log4cplus.logger.utils=FILEAPPENDER

log4cplus.appender.STDOUT=log4cplus::ConsoleAppender
log4cplus.appender.STDOUT.layout=log4cplus::PatternLayout
log4cplus.appender.STDOUT.layout.ConversionPattern=%d{%m/%d/%y %H:%M:%S} [%t] %-5p %c{2} %%%x%% - %m [%l]%n

log4cplus.appender.FILEAPPENDER=log4cplus::RollingFileAppender
log4cplus.appender.FILEAPPENDER.File=/tmp/consoleapplication.log
log4cplus.appender.FILEAPPENDER.MaxFileSize=5MB
#log4cplus.appender.FILEAPPENDER.MaxFileSize=500KB
log4cplus.appender.FILEAPPENDER.MaxBackupIndex=1
log4cplus.appender.FILEAPPENDER.layout=log4cplus::TTCCLayout

In QT creator modify the main.cpp to the following.

#include <log4cplus/configurator.h>
#include <log4cplus/logger.h>

int main(int argc, char *argv[])
{
// Use the log4cplus namespace
using namespace log4cplus;

// Load the properties
PropertyConfigurator::doConfigure("log.properties");

// Create the logger
const Logger logger = Logger::getInstance("main");

// Log with INFO level
if (logger.isEnabledFor(INFO_LOG_LEVEL))
{
LOG4CPLUS_INFO(logger, "Application startup");
}

// Log with INFO level
if (logger.isEnabledFor(INFO_LOG_LEVEL))
{
LOG4CPLUS_INFO(logger, "Application shutdown");
}
}

Make sure you have set the build target to DEBUG not RELEASE. When using the RELEASE mode the compiler should complain about the missing pthread references.

Compile and run the application. If you did not refresh the library cache after installing the log4cplus package the compiler should complain about the liblog4cplus.so.4 library. If the property file cannot be found, the logger will complain about not having any appenders.

log4cplus:ERROR No appenders could be found for logger (main).
log4cplus:ERROR Please initialize the log4cplus system properly.

The following output should be generated on the console.

01/08/12 06:50:05 [3079005904] INFO main %% - Application startup [../Log4CplusClient/main.cpp:18]
01/08/12 06:50:05 [3079005904] INFO main %% - Application shutdown [../Log4CplusClient/main.cpp:24]

The consoleapplication.log looks like.

2090 [3592] INFO main <> - Application startup
2106 [3592] INFO main <> - Application shutdown
Posted in Common | Tagged , , , | Leave a comment

Building boost on linux

A valid gcc toolchain is required.

Build or obtain the bjam build tool e.g. using a package manager.

Image

Download the boost sources (http://www.boost.org/) and extract them into a folder e.g. /developer/src/boost in the following refered as $SOURCES. Copy the bjam build tool from /usr/bin/ into the $SOURCES folder.

Modify the configuration files as needed. For instance, the $SOURCES/bootstrap.sh file to the following. Make sure you have write permissions to the lib folder.

TOOLSET=”gcc”

LIBDIR=/usr/local/lib/boost

Mark the $SOURCES/bootstrap.sh and the $SOURCES/tools/build/v2/engine/build.sh as executable.

chmod +x

Otherwise calling the bootstrap.sh leads to a “permission denied” error!

Execute bootstrap.sh … happy compiling – be patient
…failed updating 60 targets…
…skipped 12 targets…
…updated 875 targets…

After that, run ./bjam install. E.G. you can specify the adress model by using address-model=64 for 64 Bit.
…failed updating 60 targets…
…skipped 12 targets…
…updated 9731 targets…

Posted in Common | Tagged , , | 1 Comment

C++11 Features in Visual C++ 11

A short summary of the supported features of VS C++ 11.
C++ 11 Features in Visual C++ 11

Posted in Common | Leave a comment

The demise of the low level Programmer.

Very nice low level stuff.

The demise of the low level programmer

Posted in Common | Leave a comment

Have your cake and eat it too: QuickThread Toolkit

Intel Software Network Blog: Introducing QuickThread Toolkit

Posted in Common | Leave a comment

Windows 8 Runtime: Break circular dependencies

Underneath there is some code snippet from one of the SDK samples. Be aware of circular dependencies, get familiar with weak pointers!

auto view = ref new View();
view->Initialize(m_window, m_applicationView);
view->Run();

// Must delete the view explicitly in order to break a circular dependency
// between View and CoreWindow. View holds on to a CoreWindow reference most
// typically for window activation, while CoreWindow refers back to View when
// event handlers are hooked up. Without breaking this circular dependency,
// neither View nor CoreWindow object gets to clean up. It's also important
// to note that a 'delete' call on a ref class instance simply means calling
// into a class destructor in order to explicitly break a cycle. It doesn't
// actually deallocate any memory.
delete view;
Posted in Common | Leave a comment