Monday, October 6, 2014

Install OEM Windows in VirtualBox

The following steps can be followed to install OEM windows onto a virtual box.

  • Create new virtual machine
  • Edit the .vdm file of the virtual machine and add the following content under the element.
<ExtraDataItem name="VBoxInternal/Devices/pcbios/0/Config/DmiBIOSVendor" value="LENOVO"/>
<ExtraDataItem name="VBoxInternal/Devices/pcbios/0/Config/DmiSystemVendor" value="LENOVO"/>
  • Install OEM Windows onto the virtual machine.

In the example, the OEM is for IBM Corporation.

Friday, April 25, 2014

Build XulRunner with Visual C++ 2013

1. Prerequisites

1.1 MozillaBuild

1.2 Visual Studio Express 2013 for Windows Desktop

1.3 [June 2010 DirectX SDK] http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=6812

  • If you met with S1023 error whiling installing the DirectX SDK, please uninstall all Microsoft Visual C++ 2010 x86/x64 Redistributable packages (See this for details).

2. Source code

  • Download XulRunner source code from here
  • Extract to c:\build\mozilla
  • Apply the patch for vc12
  • Add #include <algorith> to C:\build\mozilla\toolkit\components\protobuf\google\protobuf\wire_format_lite_inl.h

3. Configure (x86)

Create a .mozconfig file in c:\build\mozilla folder. Enter the following content:

mk_add_options MOZ_CO_PROJECT=xulrunner
mk_add_options MOZ_OBJDIR=c:/build/mozilla/obj-x86
ac_add_options --enable-application=xulrunner

ac_add_options --disable-debug
ac_add_options --disable-debug-symbols
ac_add_options --disable-javaxpcom
ac_add_options --disable-tests
ac_add_options --disable-ipdl-tests
ac_add_options --disable-activex
ac_add_options --disable-activex-scripting
ac_add_options --disable-installer
ac_add_options --disable-crashreporter
ac_add_options --disable-updater
ac_add_options --disable-update-channel
ac_add_options --disable-update-packaging
ac_add_options --disable-maintenance-service
ac_add_options --disable-accessibility
ac_add_options --disable-logging
ac_add_options --disable-services-healthreport
ac_add_options --disable-telemetry-reporting 
ac_add_options --disable-parental-controls 
ac_add_options --disable-windows-mobile-components 
ac_add_options --disable-necko-wifi
ac_add_options --disable-pdfjs
ac_add_options --disable-accessibility
ac_add_options --disable-gamepad

ac_add_options --with-windows-version=601

4.Build (x86)

Start /start-shell-msvc2013.bat, and run the commands below.

cd /c/build/mozilla
python build/pymake/make.py  -f client.mk build
python build/pymake/make.py  -f client.mk sdk

Once the build completed, the sdk archive can be found in C:\build\mozilla\obj-x86\dist\sdk.

For more detailed documentation. See Mozilla’s Official Build Instructions

Thursday, April 24, 2014

Download a Folder from Github

Here is way to download a single folder from a Github repository without fully clone the repo.

For example, if you want to download https://github.com/jack/foo/tree/master/bar folder, you could use the command below.

svn checkout https://github.com/jack/foo/trunk/bar

Note that the tree/master is replaced with trunk.

Monday, April 14, 2014

Delete System Files On Windows 7

On Windows 7, system file are protected from being deleted. The following commands can be used to delete system files.

set THE_FILE_TO_DELETE=C:\Windows\IME\IMESC5\DICTS\PINTLGT.IMD
takeown /f %THE_FILE_TO_DELETE%
cacls %THE_FILE_TO_DELETE% /G YourUserName:F
del %THE_FILE_TO_DELETE%

Create Windows PE 5.1 Image

Listed below are the instructsions to create a Windows PE 5.1 x86 disk. To build an amd64 version, replace all ‘x86’ to ‘amd64’ in the commands below.

Prerequisites

Steps

  • Start “Deployment and Imaging Tools Environment” as administrator

  • Copy a basic PE image.

copype x86 C:\WinPE_x86
  • Mount the PE image to local directory
Dism /Mount-Image /ImageFile:"C:\WinPE_x86\media\sources\boot.wim" /index:1 /MountDir:"C:\WinPE_x86\mount"
  • (Optional) Add packages. For example, the following command adds .Net Framework to the PE
Dism /Add-Package /Image:"C:\WinPE_x86\mount" /PackagePath:"C:\Program Files (x86)\Windows Kits\8.1\Assessment and Deployment Kit\Windows Preinstallation Environment\x86\WinPE_OCs\WinPE-NetFx.cab"
  • (Optional) Verify packages using the following command (optional)
Dism /Get-Packages /Image:"C:\WinPE_x86\mount"
  • (Optional) Add thridparty programs. For example, the following command adds 7Zip to the PE
copy "C:\Program Files (x86)\7-Zip\7z*.*" C:\WinPE_x86\mount\Windows\System32
  • (Optional) Change PE boot drive letter. For example the following command change the PE’s boot drive to V:
Dism /image:C\WinPE_x86\mount /Set-TargetPath:V:\
  • Unmount the image and commit changes
Dism /Unmount-Image /MountDir:"C:\WinPE_x86\mount" /commit
  • Deploy the PE image to an ISO
MakeWinPEMedia /ISO C:\WinPE_x86 C:\WinPE_x86\WinPE_x86.iso
  • Deploy the PE image to an USB drive (e.g. U:)
MakeWinPEMedia /UFD C:\WinPE_x86 U:

Saturday, March 29, 2014

Windows Batch Scripting Tips

Find files recursively

for /r %%f in (*.abc) do echo %%f

or

for /f "delims=|" %%f in ('dir /B /S *.abc') do echo %%f

Block of statements

(The parentheses must be used exactly as shown)

if %VAR% == 0 (
    echo "statement 1"
    echo "statement 2"
)

Thursday, March 20, 2014

Setup Schroot Environment On Ubuntu

Here is my note about how to setup Ubuntu Lucid schroot environment on a Ubuntu box. Please refer to Ubuntu Document for details.

Software Prerequisits

sudo apt-get install debootstrap
sudo apt-get install schroot

Create a config file for Ubuntu Lucid (x86)

sudo vi /etc/schroot/chroot.d/lucid_i386.conf

Fill in the following content. Please make sure change the user bob to your actual username.

[lucid_i386]
description=Ubuntu 10.04 for i386
directory=/srv/chroot/lucid_i386
personality=linux32
type=directory
root-users=bob
users=bob

Get environment files

sudo mkdir -p /srv/chroot/lucid_i386
sudo debootstrap --arch=i386 lucid /srv/chroot/lucid_i386/ http://archive.ubuntu.com/ubuntu/

Done. Verify environment works

schroot -l
schroot -c lucid_i386 -u root
lsb_release -a

Here is the steps for Ubuntu Lucid (x64)

sudo vi /etc/schroot/chroot.d/lucid_amd64.conf
[lucid_amd64]
description=Ubuntu 10.04 for amd64
directory=/srv/chroot/lucid_amd64
#personality=linux32
type=directory
root-users=bob
users=bob
sudo mkdir -p /srv/chroot/lucid_amd64
sudo debootstrap --arch=amd64 lucid /srv/chroot/lucid_amd64/ http://archive.ubuntu.com/ubuntu/

For Debian Squeeze (x64)

sudo vi /etc/schroot/chroot.d/squeeze_amd64.conf
[squeeze_amd64]
description=Ubuntu 10.04 for amd64
directory=/srv/chroot/squeeze_amd64
#personality=linux32
type=directory
root-users=bob
users=bob
sudo mkdir -p /srv/chroot/squeeze_amd64
sudo debootstrap --arch=amd64 squeeze /srv/chroot/squeeze_amd64/ http://ftp.debian.org/debian/

Saturday, March 15, 2014

Setup Debian in a Virtualbox

The following tips can make it convenient to use debian in a virtual box, but please keep in mind that they are not good practices for a production server.

Disable Sudo Password Requirement

On ubuntu/debian, you could disable sudo password prompt by following the steps below.

sudo visudo

Change

%sudo ALL=(ALL) ALL

to

%sudo ALL=(ALL) NOPASSWD: ALL

Auto login

The following steps can let you log in automatically to a console when boots.

sudo vi /etc/inittab

Change the line

1:2345:respawn:/sbin/getty 38400 tty1

to

1:2345:respawn:/bin/login -f user </dev/tty1 >/dev/tty1 2>&1

Disable Grub Screen

The following steps can disable grub splash screen when boots

sudo vi /etc/default/grub

Change the value of GRUB_TIMEOUT to 0

And then run the following command to update grub config.

sudo update-grub

Friday, February 21, 2014

Targeting Windows XP with Visual C++ 2013

Build apps using Visual C++ 2013 that target Windows XP, special steps are needed.

Environment Variables to Set

The following codes is quoted from this blog.

set INCLUDE=%ProgramFiles(x86)%\Microsoft SDKs\Windows\7.1A\Include;%INCLUDE%
set PATH=%ProgramFiles(x86)%\Microsoft SDKs\Windows\7.1A\Bin;%PATH%
set LIB=%ProgramFiles(x86)%\Microsoft SDKs\Windows\7.1A\Lib;%LIB%
set CL=/D_USING_V110_SDK71_;%CL%

For building x64 version, change LIB to this one.

set LIB=%ProgramFiles(x86)%\Microsoft SDKs\Windows\7.1A\Lib\x64;%LIB%

For x86 console/windows applications, set /SUBSYSTEM accordingly:

:: for console application
set LINK=/SUBSYSTEM:CONSOLE,5.01 %LINK%
:: for gui application
set LINK=/SUBSYSTEM:WINDOWS,5.01 %LINK%

For x64 console/windows applications:

:: for console application
set LINK=/SUBSYSTEM:CONSOLE,5.02 %LINK%
:: for gui application
set LINK=/SUBSYSTEM:WINDOWS,5.02 %LINK%

Work with CMake

In CMake there is a WIN32 parameter for add_executable command. It works like this:

add_executable(MyExe WIN32 main.cpp) # this exe will link with /SUBSYSTEM:WINDOWS
add_executable(AnotherExe main.cpp)  # this one will link with /SUBSYSTEM:CONSOLE

When we not target Windows XP, it works correctly. But when we target Windows XP, obviously the link flags are not correct. To fix that, we need add a file to override make rules in CMake. CMAKE_USER_MAKE_RULES_OVERRIDE_CXX is for that purpose.

In your project file, add the following code.

cmake_minimum_required(VERSION 2.8)
set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX your_path/overrides.cmake)

# please make sure the variable is set before the `project` command.
# project(MyApp) 

Then in overrides.cmake, add the following codes.

if(WIN32)
    # if target winxp
    if(TARGETING_XP_64)
        SET(CMAKE_CREATE_WIN32_EXE /SUBSYSTEM:WINDOWS,5.02)
        SET(CMAKE_CREATE_CONSOLE_EXE /SUBSYSTEM:CONSOLE,5.02)
    elif(TARGETING_XP)
        SET(CMAKE_CREATE_WIN32_EXE /SUBSYSTEM:WINDOWS,5.01)
        SET(CMAKE_CREATE_CONSOLE_EXE /SUBSYSTEM:CONSOLE,5.01)
    endif()
endif()

Now, to build targetting Windows XP, you could run cmake like the folowing.

cmake -G "YourGenerator" -DTARGETING_XP=On path_to_your_project.cmake

And the following for XP x64.

cmake -G "YourGenerator" -DTARGETING_XP_64=On path_to_your_project.cmake

Tuesday, February 18, 2014

Core Dump on Ubuntu

Listed below are just several notes about core dump on Ubuntu.

The location of core dump files

The following command can print out where will the core dump files be placed.

cat /proc/sys/kernel/core_pattern

If the output start with a | character, the kernel then will write the core dump to the stdin of the command after the | character.

Core dump file size

The following command can remove core dump file size limit.

ulimit -c unlimited

Apport

On ubuntu, core dump files are forwarded to Apport by default. So if you can’t find the core file in your current directory, you may want to try /var/crash. Probably you will see crash report files there.

The command apport-unpack can be used to extract the core files out of a crash report files.

Friday, February 14, 2014

RPATH on Windows

On Linux, we can specify shared library search paths for our executable/libraries using RPATH. On Windows, however, there is no such easy way to do that. A possible solution is to use /DELAYLOAD and SetDllDirectory.

Ways to Load a DLL

Basically speaking, there are three ways to load a DLL on windows.
- Implicitly loading. In this way, we link our application against the .lib file of the DLL. The DLL will be loaded once the application was invoked.
- Explicitly loading. That is the way we call LoadLibrary Win32 API to load a DLL. The search path can be changed by calling SetDllDirectory function.
- Delayed loading. Similar to Implicit loading, but the DLL won’t be loaded until once of functions in it got called.

Delay loading

/DELAYLOAD is a linker option of MSVC compiler (version 6 or later) that can tell the linker which DLL we want to delay load. So, to simulate the functionality of RPATH, we can specify the DLL should be delay loaded via /DELAYLOAD at link time, and call SetDllDirectory function to set the search path. After that, there are three ways to force load the library.

  • We can simply call a function that is exported in that library to force loading the library.
  • Call LoadLibrary to explicitly load the library.
  • Call __HrLoadAllImportsForDll to force load the library.

Listed below is an example to use the above method to delay load a DLL. Please note that to make the code work, delayimp.lib should be linked against.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <delayimp.h>
#include <string>

bool loadTheLibraryNow(const char* dir) {
    const char* baseDllName = "library-to-delay-load.dll";

    if (!dir)
        return false;

    // backup current dll directory
    char previousDllDirectory[MAX_PATH] = { '\0' };
    if (!::GetDllDirectoryA(MAX_PATH, previousDllDirectory)) {
        previousDllDirectory[0] = '\0';
    }

    // set new current dll directory to
    if (!::SetDllDirectoryA(dir))
        return false;

    // force load the dll
    bool result = SUCCEEDED(__HrLoadAllImportsForDll(baseDllName));

    // restore the dll directory to old value
    ::SetDllDirectoryA(previousDllDirectory);

    return result;
}

Please See here for more details.

Build Qt5 Statically with Visual C++ 2013

1. ICU

1.1 Prerequisites

  • Download Cygwin installer from here
  • Install Cygwin and make sure make package is selected

1.2 Download ICU

  • Download ICU from here
  • Extract the zip file to C:\build\icu and make sure folder C:\build\icu\source exists
  • Open c:\build\icu\source\runConfigureICU
  • Replace all /MD to /MT (of course that will also replace /MDd to /MTd)

1.4 Build ICU

x86 version

:: Start a command prompt and setup x86 build env

cd c:\build\icu\source
set PATH=C:\cygwin\bin;%PATH%
dos2unix *
dos2unix -f configure
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86

:: Build the x86 static release version

mkdir c:\build\icu\build\x86-static-release && cd c:\build\icu\build\x86-static-release
bash ../../source/runConfigureICU Cygwin/MSVC --prefix=/cygdrive/c/build/icu/dist/x86-static-release --enable-static --disable-shared
make -j8 && make install

:: Build the x86 static debug version

mkdir c:\build\icu\build\x86-static-debug && cd c:\build\icu\build\x86-static-debug
bash ../../source/runConfigureICU --enable-debug --disable-release Cygwin/MSVC --prefix=/cygdrive/c/build/icu/dist/x86-static-debug --enable-static --disable-shared 
make && make install

::

x64 version

:: Start a command prompt and setup x64 build env

cd c:\build\icu\source
set PATH=C:\cygwin\bin;%PATH%
"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86_amd64

:: Build the x64 static release version

mkdir c:\build\icu\build\x64-static-release && cd c:\build\icu\build\x64-static-release
bash ../../source/runConfigureICU Cygwin/MSVC --prefix=/cygdrive/c/build/icu/dist/x64-static-release --enable-static --disable-shared
make -j8 && make install

:: Build the x64 static debug version

mkdir c:\build\icu\build\x64-static-debug && cd c:\build\icu\build\x64-static-debug
bash ../../source/runConfigureICU --enable-debug --disable-release Cygwin/MSVC --prefix=/cygdrive/c/build/icu/dist/x64-static-debug --enable-static --disable-shared
make && make install

::

2. Qt

2.1 Prerequisites

2.2 Change files

  • Open c:\build\qt-everywhere-opensource-src-5.2.1\qtbase\mkspecs\win32-msvc2013\qmake.conf
    • Remove embed_manifest_dll embed_manifest_exe from the line CONFIG += line
    • Make the following change
QMAKE_CFLAGS_RELEASE    = -O2 -MD
QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MD -Zi
QMAKE_CFLAGS_DEBUG      = -Zi -MDd

to

QMAKE_CFLAGS_RELEASE    = -O2 -MT
QMAKE_CFLAGS_RELEASE_WITH_DEBUGINFO += -O2 -MT -Zi -Fd$(DESTDIR)$(QMAKE_TARGET).pdb
QMAKE_CFLAGS_DEBUG      = -Zi -MTd -Fd$(DESTDIR)$(QMAKE_TARGET).pdb

2.3 Build Qt Base

x86-release version

  • Extract Qt Source to c:\build\qt-everywhere-opensource-src-5.2.1
  • Add Python, Ruby, Perl, Jom to PATH
  • Add GnuWin32 to PATH
:: Add GnuWin32 to PATH
set PATH=%PATH%;c:\build\qt-everywhere-opensource-src-5.2.1\gnuwin32\bin

:: Setup Visual C++ Environment

"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\vcvarsall.bat" x86

:: Add ICU to INLCUDE, LIB, PATH

set INCLUDE=%INCLUDE%;c:\build\icu\dist\x86-static-release\include
set LIB=%LIB%;c:\build\icu\dist\x86-static-release\lib
set PATH=%PATH%;c:\build\icu\dist\x86-static-release\lib

:: Add OPENSSL to INLCUDE, LIB, PATH

set INCLUDE=%INCLUDE%;c:\build\\openssl\dist\x86-static-release\include
set LIB=%LIB%;c:\build\\openssl\dist\x86-static-release\lib
set PATH=%PATH%;c:\build\\openssl\dist\x86-static-release\lib

:: Create build directory

mkdir c:\build\qt-everywhere-opensource-src-5.2.1\build\x86-static-release
cd c:\build\qt-everywhere-opensource-src-5.2.1\build\x86-static-release

:: Configure

..\..\configure -prefix c:\build\qt-everywhere-opensource-src-5.2.1\dist\x86-static-release -platform win32-msvc2013 -static -release -c++11 -opensource -confirm-license -icu -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -openssl-linked -skip qtwebkit -nomake tests -nomake examples -mp -make-tool jom OPENSSL_LIBS="-lssleay32 -llibeay32 -lgdi32 -lcrypt32 -luser32 -ladvapi32"

:: Build and install

jom -j8
jom install

:: HACK: copy required libs manually

cp C:\build\qt-everywhere-opensource-src-5.2.1\build\x86-static-debug\qtbase\lib\translator_*.lib c:\build\qt-everywhere-opensource-src-5.2.1\dist\x86-static-release\lib
cp C:\build\qt-everywhere-opensource-src-5.2.1\build\x86-static-debug\qtbase\lib\preprocessor*.lib c:\build\qt-everywhere-opensource-src-5.2.1\dist\x86-static-release\lib

::

The steps to build debug version and x64 versions are similar to the ones above.

Statically Link Against an LGPL'd Library in a Closed Source Project

Statically link against a LGPL’d library in a closed source project is possible, if object format of your application is provided. In that way, a user can modify the LGPL’d library and relink the application. See GPL FAQ and here for more details.

Thursday, February 13, 2014

Test If a Type Has a Member Function

Sometimes we want to handle types with some specific member functions specially. The followin examples shows how to do that using SFINAE and enable_if.

#include <iostream>
#include <type_traits>

using namespace std;

// SFINAE test
template<typename T>
class has_test {
    template<typename U, U> class check {};
    template<typename C> static char f(check<void(C::*)(int), &C::test>*);
    template<typename C> static long f(...);
public:
    static const bool value = (sizeof(f<T>(nullptr)) == sizeof(char));
};

template<typename T>
std::enable_if_t<has_test<T>::value, void> test(T& t) {
    cout << typeid(T).name() << " has a 'test' member function. " << endl;
}

template<typename T>
std::enable_if_t<!has_test<T>::value, void> test(T& t) {
    cout << typeid(T).name() << " has no 'test' member function. " << endl;
}

struct A {
    void test(int) {}
};

struct B {
};

int main() {
    A a;
    B b;
    test(a);
    test(b);
}

Here is the output of the program.

struct A has a 'test' member function. 
struct B has no 'test' member function. 

Tuesday, February 11, 2014

Launch Program in Debugger Automatically

Sometimes I have the requirement to launch an application automatically (and always) in debugger. For example, if we have a pair of master and slave programs. The master program will start the slave program and we want to debug the slave program.

On Windows, there is a simple way to do that. Basically, we just need to add a string value Debugger=PathToYourDebugger.exe to registry under the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\YourProgram.exe. Here, PathToYourDebugger.exe is the full path to your debugger and YourProgram.exe is the full path to your program.

After that, every time you start YourProgram.exe, the debugger will be launched.

The commands below can make it even easier.

To enable automatically-debugger-launching for a program

REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\YourProgram.exe"  /f /t REG_SZ /v debugger /d "PathToYourDebugger.exe"

To disable automatically-debugger-launching for a program

REG DELETE "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\YourProgram.exe" /f

Wednesday, January 29, 2014

Koenig Lookup and Stream Operator

Today I met with a wired problem with my C++ code. It tooks me several hours to fix it and it turns out it is related to Koeing Lookup and Name Hidding.

Problem

Here is the sample code that has problem.

#include <iostream>

namespace A {
    struct Foo { int data; };
}

namespace B {
    template<typename T>
    void output(const T& t) { std::cout << t; }

    struct Bar { int data; };
    std::ostream& operator<< (std::ostream& stream, const Bar& bar) {
        return stream << bar.data; 
    }
}

std::ostream& operator<< (std::ostream& stream, const A::Foo& foo) {
    return stream << foo.data; 
}

int main() {
    A::Foo foo;
    B::output(foo);
}

I got the following compilation error with it. Basially the compiler complains that it can’t find the operator<<(std::ostream&, const A::Foo&) function.

c:\path\main.cpp:9: error: C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const A::Foo' (or there is no acceptable conversion)

Solution

The fix is simply moving operator<<(std::ostream&, const A::Foo&) into namespace A.

Why?

Listed below are why the error occurs, and why the fix works.

Basically speaking, when C++ compiler tries to find an unqualified function, it will search in two different ways:

  • A: Search the namespacs from the one where the function is called up to the global namespace (also take into account of using namespace and using) until found.
    • For example, in the sample code, it will search for the operator<<(std::ostream&, const A::Foo&) function both B and global namespaces.
  • B: Koenig lookup - search in the set of associated namespaces of all its argument types.

    • For example, in the sample code, it will search for the operator<<(std::ostream&, const A::Foo&) function in the following two namespaces:

      • std, the namespace of the first argument type ostream
      • A, the namespace of the second argument Foo
    • Here is the definition on Wikipedia of Koenig Lookup.

      In the C++ programming language, argument-dependent lookup (ADL), or argument-dependent name lookup, applies to the lookup of an unqualified function name depending on the types of the arguments given to the function call.

In the sample code, since the function operator<<(std::ostream&, const A::Foo&) can be found using method A, then it should work. But it does not!!!

The problem is that there is already a function operator<< in namespace B. Though it has different signature from the one we are looking for, compiler simply stop searching up. That is why the function can’t be found. Basically, it is because of Name Hiding.

The reason why the fix works is that once we move the function into namespace A, then it can be found using method B. So basically it is because of Koenig Lookup.

Monday, January 27, 2014

STL documentation in QtCreator

The post shows how to enable STL documentation in QtCreator.

  • Go to http://en.cppreference.com/w/Cppreference:Archives and download the latest Qt help book
  • Extract the .qtc file out of the achive downloaded
  • Open QtCreator, go to Tools -> Options -> Help -> Documentation tab
  • Click Add button and select the .qtc file
  • Restart QtCreator

That’s it. You could now move your cursor to a STL keyword and press F1 to view its documentation.

Friday, January 24, 2014

Example to Use Boost.Log

The following code is an example that illustrates how to use Boost.Log.

Several Notes:

  • In the example, debug_output_backend (e.g. DebugView, or Visual Studio Output Pane) is used. It support multi-process logging. It is only available on Windows. On linux, syslog_backend is a good alternative.
  • Log level control is implemented using a global varable g_log_level and a boost log filter function log_level_filter.
  • Messages can be logged by send them to log streams, e.g. LOG(), LOG_ERROR.
  • All the items that output to log stream should support operator<<(std::ostream&) or operator<<(boost::log::record_ostream&)

See also Boost.Log

#include <iostream>
#include <iomanip>
#include <string>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sinks/debug_output_backend.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/phoenix/bind/bind_function_object.hpp>
#include <boost/log/support/date_time.hpp>

using namespace std;
using namespace boost;
using namespace boost::log;

namespace {

    // log severity level
    enum log_level {
        Debug,
        Info,
        Warning,
        Error
    };

    // logger type with severity level and multithread support
    typedef sources::severity_logger_mt<log_level> logger_t;

    // windows debugger output (e.g. DbgView) sink type with synchronization support
    typedef sinks::synchronous_sink<sinks::debug_output_backend> sink_t;

    // global variables
    log_level g_log_level = Info;  // log level
    logger_t* g_logger; // logger

    // define the keywords to use in filter and formatter
    BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", log_level)
    BOOST_LOG_ATTRIBUTE_KEYWORD(timestamp, "TimeStamp", 
        attributes::local_clock::value_type)
    BOOST_LOG_ATTRIBUTE_KEYWORD(processid, "ProcessID", 
        attributes::current_process_id::value_type)
    BOOST_LOG_ATTRIBUTE_KEYWORD(threadid, "ThreadID", 
        attributes::current_thread_id::value_type)

    // the filter used to filter log records according to level
    bool log_level_filter(value_ref<log_level, tag::severity> const& level) {
        return level >= g_log_level;
    }

    // stream output support for log_level
    std::ostream& operator<<(std::ostream& stream, log_level level) {
        static const char* strings[] = { "debug", " info", " warn", "error" };
        int l = static_cast<size_t>(level);
        if (l >= 0 && l < sizeof(strings) / sizeof(strings[0]))
            stream << strings[l];
        else
            stream << l;
        return stream;
    }

    void initialize_logger() {
        // create a logger
        g_logger = new logger_t();

        boost::shared_ptr<core> core = core::get();

        // add attributes
        core->add_global_attribute("TimeStamp", attributes::local_clock());
        core->add_global_attribute("ProcessID", attributes::current_process_id());
        core->add_global_attribute("ThreadID", attributes::current_thread_id());

        // add level filter
        core->set_filter(phoenix::bind(&log_level_filter, severity.or_none()));

        // create a debug output sink
        boost::shared_ptr<sink_t> sink(new sink_t());

        // sink formatter
        sink->set_formatter(expressions::stream
            << "[" << processid << "]"
            << "[" << threadid << "]"
            << "[" << expressions::format_date_time(
                                        timestamp, "%Y-%m-%d %H:%M:%S") << "]"
            << "[" << severity << "] "
            << expressions::smessage);

        // add sink
        core->add_sink(sink);
    }

    void cleanup_logger() {
        if (g_logger) {
            delete g_logger;
            g_logger = nullptr;
        }
    }

    // macros to get log streams
    #define LOG_DEBUG()     BOOST_LOG_STREAM_SEV(*g_logger, Debug) \ 
        << __FUNCTION__ << "(): "
    #define LOG_INFO()      BOOST_LOG_STREAM_SEV(*g_logger, Info) \ 
        << __FUNCTION__ << "(): "
    #define LOG_WARNING()   BOOST_LOG_STREAM_SEV(*g_logger, Info) \ 
        << __FUNCTION__ << "(): "
    #define LOG_ERROR()     BOOST_LOG_STREAM_SEV(*g_logger, Error) \ 
        << __FUNCTION__ << "(): "
    #define LOG()           LOG_INFO()
}

void doSomething() {
    LOG() << "Log message from doSomethng()";
}

int main(int argc, char** argv) {
    initialize_logger();

    LOG() << "Application started";
    LOG_ERROR() << "An error occured";
    doSomething();

    LOG_DEBUG() << "This message won't be log if g_log_level >= Info";
    g_log_level = Debug;
    LOG_DEBUG() << "This message will be log if g_log_level >= Debug";

    cleanup_logger();
    return 0;
}