Checking out WebRTC with git

The default instructions for getting started with WebRTC (can be found here) use the SVN repository to check out.

As it turns out, there is also a git version of the repository: http://git.chromium.org/external/webrtc.git

But how to use it? We have to adapt the gclient command a bit because all the tools expect WebRTC to be in the ‘trunk’ directory. Here’s the magic bit that makes it all work:

mkdir webrtc_git
cd webrtc_git
gclient config --name=trunk http://git.chromium.org/external/webrtc.git
gclient sync
cd trunk
make

On Linux don’t forget to install both ALSA and PulseAudio libs:

sudo apt-get install libpulse-dev libasound2-dev

Debian fonts

For my own reference: do “sudo apt-get install ttf-liberation” to get some sensible fonts on Debian :)

And if I end up on ubuntu 12.04 LTS, do this for firefox fix:

sudo rm /etc/fonts/conf.d/10-hinting-slight.conf
sudo ln -s /etc/fonts/conf.avail/10-hinting-full.conf /etc/fonts/conf.d/

Informit: Interview with C++ Author Nicolai Josuttis

An interesting interview with Nicolai Josuttis (of “The C++ Standard Library: A Tutorial and Reference” fame) can be found here:

http://www.informit.com/articles/article.aspx?p=1846582&WT.mc_id=IT_NL_Content_2012_3_7&rll=1

Well that was not something I’d expect from a “famous” guy like Nicolai. I very much enjoyed the first edition of the book but I will be a bit sceptical to buy this updated version. While I have no doubt that the writer is able to understand all the additions in C++11 just by experimenting and write a decent book about it, I’m not sure if it will contain the most practical advice due to his lack of real-world use of these features.

However, one thing that echoes from the interview and resonates quite well with me is that overall the language is improving in big steps, it is also growing more complicated and complicated with each new version. The design-by-committee approach is leaving us with a specification that only a few persons on the planet can actually keep in their head and use. Hiding new stuff behind slightly-changed operators and other gimmicks decrease the readability of code and makes you wonder what hidden features they added this time that can and will bite you in the ass :)

I love C++ but it’s obviously getting so complicated that even a well-known expert like Nicolai is having problems following it all. On the other hand, it provides a lot of fun exploration for programmers like me so I’m off to play with the new features :)

Debian snapshots

This is one site I constantly forget about: http://snapshot.debian.org/

It contains a daily snapshot of the Debian repositories, very useful for retrieving older .deb versions!

Simple chroot instructions for debian squeeze

These commands create a very basic chroot environment on my ubuntu 10.04 laptop. It’s nice to create a dedicated build environment, isolate an application or (in my case) test building/deployment on debian machines locally.

On host machine, execute as root!:

# install deboostrap, only need to run this once
apt-get install debootstrap

# create chroot target dir, replace with desired name
cd /opt
mkdir squeeze_chroot

# install debian squeeze 64-bit, will take some time and download packages
debootstrap --arch amd64 squeeze /opt/squeeze_chroot/

# edit /etc/fstab, add these lines (I'm not mounting /home) and save file:
/tmp            /opt/squeeze_chroot/tmp  none   bind            0       0
/proc           /opt/squeeze_chroot/proc proc   defaults        0       0
/dev            /opt/squeeze_chroot/dev  none   bind            0       0

# bind-mount the chroot stuff
mount -a

Now a basic chroot environment is created, let’s enter it and customize it a little

# change into chroot
chroot /opt/squeeze_chroot

# you'll now be in the / directory of your chroot
pwd

# fix some basic stuff
apt-get update
apt-get install locales

# Select only en_US and en_GB variants. Choose en_US.UTF-8 as default
dpkg-reconfigure locales

So that’s it, you’re good to go. Install things like ‘build-essential’, ‘subversion’ and ‘git-core’ at your own convenience. Your homedir will be /root.

There’s plenty of customization to do (remember, this is a full working Debian install) but I’ll leave that as an exercise for the reader :)

If you want to exit your chroot, just enter

exit

Book review: Toy Stories (James May)

../../../_images/toystories.jpg

Since I was forced to rest a bit this week, I finally had the time to finish this book. Even though it’s not hardcore tech, it still tickled my Nerd interest so here’s my review.

Toy Stories by James May (of Top Gear fame) gives the background stories and detailed information to accompany the highly recommended BBC series of the same name.

The premise is that May takes 6 toys from his youth and applies them on a massive scale in today’s world. The toys are:

  • Plasticine
  • Meccano
  • Airfix
  • Hornby model railway
  • Scalextric (racing)
  • Lego

The complete history of (and stories behind) each toy is described, which makes for detailed but interesting reading material. After this, there are some behind the scenes looks at each individual “stunt”. Most of this material does not duplicate of the TV series so watching the episodes is recommended for the full picture.

I’ve really enjoyed this book, especially the “Build a full size bridge from Meccano”, “Recreate an old railway line with H0 gauge trains and rails” and “Build a full size house from Lego” chapters. Even the toys that I have no affinity with (for instance Plasticine) were fun to read about just because of the informal (and sometimes funny) writing style of James May. The history of each toy comes to life and is put in a proper perspective with regard to the time they were invented.

The only negative note is the last chapter on Lego, it seems a bit rushed and is not as detailed as the rest of the chapters. For the rest, it’s a fun book to read and it is illustrated with a lot of nice pictures. Highly recommended!

Book review: Version Control by Example (Eric Sink)

../../../_images/1802_image001.jpg

Since the project that I’m involved in is moving from Subversion to GIT, I was looking for a nice book to get me started. A tip on Hacker News pointed me to this book, these guys are even nice enough to send you a free copy (no strings attached).

The book starts out with a thorough description of current second generation Version Control Systems (VCS). By this, they mean that the repository is centralized on a server somewhere (CVS, SVN). A list of generic commands is then formulated and it’s filled in for SVN (things like checkout, commit, revert etc). After this, one chapter is spent on detailing an example workflow with 2 persons in SVN. The examples are complete and easy to follow.

The second part of the book starts with general information about Distributed VCS solutions, including the pros and cons of such a solution. After these general chapters, systems like Mercurial, GIT and Veracity (the VCS made by the guys who wrote this book) are each detailed in their own chapter. For each VCS, a workflow example that matches the SVN example is given so you can easily compare between the different systems. For each system the generalized table of commands is filled in with the proper equivalent for each tool.

I found this book contained a nice introduction to GIT (even though I might need some more advanced tutorials to really get it) and by re-using the example workflow, the comparison to SVN was easy. The writing style is clear and informal. The jokes are a bit lame but they lighten up what would probably otherwise be a very dry book! I’d definitely recommend it to people who need to get started with SVN or newer systems like GIT.

Switchboard: a curl-like tool for XMPP

I’m doing some testing and had the need for a scriptable XMPP client (for instance to create a lot of accounts automatically). I’ve ended up with Switchboard and I really like it a lot! Example invocation:

benjamin@benjamin-laptop:/var/lib/gems/1.8/bin$ ./switchboard -j user@something.com -p somepassword roster list
=> Switchboard started.
user@somethingcom's roster:
otherguy@something.com
moreotherguy@something.com
Shutdown initiated.

Easy right! Just do this on ubuntu/debian to get it:

sudo apt-get install rubygems
sudo gem install switchboard
cd /var/lib/gems/1.8/bin
./switchboard

I’m not very familiar with Ruby apps (probably this needs to be added to your path) but the tool works nicely!

More extensive documentation can be found here

Boost preprocessor + enums

Update 2015

I’ve received a very friendly e-mail this weekend from Anton Bachin, the author of the better enums library. Some time has passed since I originally wrote the post below and C++ has improved quite a lot in the meantime, his library seems a much nicer solution! So feel free to read along below but if you have a need for this functionality, definitely consider using his library instead. Thanks Anton for bringing it to my attention!

Original post

A recurring problem in C++ is printing out enum values. Of course you can just do ‘std::cout << enumval << std::endl;’ but that will only print the numeric value. For logging purposes it would be nice to print out the textual representation of the enum value.

Usually what people do is add some kind of utility ‘toString’ method and add a load of ‘if’/’case’ statements that will match all enum values and return a string. I found this to be error-prone, because you will need to update both the enum and this utility function at the same time to keep consistent. So I thought about this for a while and decided perhaps the Boost preprocessor library could come to the rescue!

Check out this sample code:

#include <string>
#include <iostream>
#include <boost/preprocessor.hpp>

// Used in toString() method
#define ENUM_TO_STR(unused,data,elem) \
if (parm == elem) return BOOST_PP_STRINGIZE(elem);

class EnumTest
{
public:
   // Need to undef if because you might have
   // multiple enum definitions in a file
#undef SEQ
#define SEQ (_INVALID_)(VALUE1)(VALUE2)(VALUE3)(_MAX_)

   enum SampleEnum
   {
      BOOST_PP_SEQ_ENUM(SEQ)
   };

   static const std::string toString(const SampleEnum parm)
   {
      BOOST_PP_SEQ_FOR_EACH(ENUM_TO_STR,~,SEQ)
      return "_INVALID_";
   }
};

int main()
{
   EnumTest::SampleEnum t1 = EnumTest::VALUE1;
   std::cout << EnumTest::toString(t1) << std::endl;
}

Some comments:

  1. The enum is generated by the BOOST_PP_SEQ_ENUM macro, which relies on a preprocessor definition called ‘SEQ’ to contain a list of values. These values should be encapsulated in () braces.
  2. The static ‘toString’ method uses the BOOST_PP_SEQ_FOR_EACH macro. This macro repeats a specified statement (in this case the ENUM_TO_STR macro) for each element in SEQ. The ‘~’ will be passed as additional data to the ENUM_TO_STR macro and put in the ‘unused’ parameter. I don’t use this functionality here but it could be useful in other places :)

If you want to see the generated code from the preprocessor here is the result (the ‘if’ statements are a bit messy, I could insert a newline there perhaps):

enum SampleEnum
{
   _INVALID_, VALUE1, VALUE2, VALUE3, _MAX_
};

static const std::string toString(const SampleEnum parm)
{
   if (parm == _INVALID_) return "_INVALID_"; if (parm == VALUE1) return "VALUE1"; if (parm == VALUE2) return "VALUE2"; if (parm == VALUE3) return "VALUE3"; if (parm == _MAX_) return "_MAX_";
   return "_INVALID_";
}

It’s not extremely pretty but it works :)

Quick and dirty ‘find’ and ‘du’ trick

Want to know how much space the files you found with the ‘find -name’ command occupy? Try this:

find -name \*SOMEPATTERN\* -print0 | du -c --files0-from=-

What happens here is:

  1. Add the ‘-print0’ parameter to the find command to use 0 instead of newline after each file
  2. Add the ‘–files0-from=-‘ to instruct ‘du’ to read a 0-terminated list of filenames, the ‘-‘ specifies ‘read from stdin’

Simple!

Retrieving load averages in your C/C++ program

Instead of parsing /proc/loadavg directly, there is a nice convenience function in <cstdlib> (or stdlib.h if you’re using C):

#include <cstdlib>
#include <iostream>

double averages[3];
std::cout << getloadavg(averages, 3) << " elements retrieved (should be 3)" << std::endl;
std::cout << "Average 1-min: " << averages[0] << std::endl;
std::cout << "Average 5-min: " << averages[1] << std::endl;
std::cout << "Average 15-min: " << averages[2] << std::endl;

Need a graphical diff tool? Try Meld!

When I still ran Windows, I used ‘beyond compare’ a lot for directory and file comparisons. For working with SVN the tortoise SVN diff viewer was my preferred choice.

On Linux, I eventually settled for ‘kdiff3’ but it’s a quirky program that complicates things very badly. There is merge support in there but it’s pretty horrible. For SVN, I used eclipse (since I’m already using CDT anyway) and that’s pretty good!

In my search for a directory/fill new tool, I stumled upon ‘Meld’ (http://meld.sourceforge.net/). It provides 2-way or 3-way file and directory diffs and as a bonus it can also diff against a VCS. It’s clean, simple and no-nonsense. The merging functionality is easy to use and looks a lot like the Eclipse one. Highly recommended!

Generating test files on windows

Need to generate a test file of a certain size on a Windows machine? On linux you’d use the ‘dd’ tool and use /dev/zero as an input. Luckily, there is an equivalent on Windows:

fsutil file createnew <filename> <size in bytes>

So for example,. this will create a 1MB test file:

fsutil file createnew test.bin 1048576

Nice for testing harddrives that give disk failures :)

Specific CXXFLAGS for each makefile target

The problem: my Makefile has two targets. One for a production buid and one for a unittest build. For the unittest build, I want to specify different CXXFLAGS (disable optimization for instance). This turns out to be difficult!

One option is to specify some variable on the make command line like this:

make test TESTFLAGS=1

and then to check in the Makefile for its existence:

# Generic flags shared by all builds
CXXFLAGS=-Wall

# Check for test flag
ifeq ($(TESTFLAGS),)
   #empty flags, release setting
   CXXFLAGS += -O2
else
   #debug setting
   CXXFLAGS += -g3 -ggdb -O0 -fprofile-arcs -ftest-coverage
endif

Of course, this is cumbersome! So I figured out you can specify requirements for each build target spanning multiple lines. This allows you to specify it like this:

#unittest target
$(TEST_TARGET): CXXFLAGS += -g3 -ggdb -O0 -fprofile-arcs -ftest-coverage
$(TEST_TARGET): $(OBJS) unittest.o $(TEST_OBJS)

#production build
$(TARGET): CXXFLAGS += -O2
$(TARGET): $(OBJS) main.o $(TEST_OBJS)

Of course the same trick will work for other variables like LDFLAGS. I’ve been searching and experimenting for this trick a long time, I thought sharing this would be nice :)