Lua, Javascript, Syslog, and MongoDb

Embedding a Scripting language in C++ apps

Scripting is an important part of application extendability and customizability, and can give the user a lot of power. Adding a scripting capability to an application involves the choice of a suitable scripting language, a corresponding scripting engine, and an easy SDK to A. make the scripting engine interpret script strings and script files and return results, and B. make C++ functions, classes and data available to the script engine for use in the scripts.

Many use Lua as it is fairly compact and quite expressive, supporting even a few OO concepts like classes and methods. There are several C++ bindings listed on the Lua bindings web page, but they focus on half of the problem: the half that is concerned with item B from the previous paragraph. For item B I much prefer SWIG or tolua++ as it does all the type checking/conversions for me. A few like diliculum or wxScript or OOLua provide item A but only partially, or in a clunky, cumbersome way, or have significant dependencies (boost, or wxWidgets) because they all attempt to provide item B as well.

I wrote a simple wrapper way back, was useful on a project back then but also doesn’t support getting an interpreted “chunk” into C++ for re-use in application. Perhaps I should create a clear test case, and extract from the items above just the Lua interpreter wrapper and package that. Add to list of projects 😉

Javascript in C++ or C# apps

In looking at scripting on Windows, options for me were Lua, Power Shell, Python, Batch (“DOS”), or bash. Though I love Python as an application language, it is a bit on the heavy side for scripting. Batch is completely and utterly frustrating to use with an arcane syntax and counter-intuitive rules for strings etc. Power Shell is MS Windows specific. Lua as the above problems that I don’t have a nice solution to, but of course if I really wanted Lua certainly would be a good option, especially with things like Lua for Windows and Lua Binaries projects.

I found out that MS Windows XP and above, via Windows Scripting Host, support a javascript engine. And javascript is not bad at all. Javascript is quite a simple language, not as nice as Python but certainly very quick to develop in. It may be more powerful than Lua, and many more people know javascript than Lua. However, whereas Lua has one engine (from lua.org), there are a few popular JS engines: V8 by Google, SpiderMonkey by Mozilla, and Rhino (java).

Bindings that allow embedding of JS scripting in a C++ application are engine-specific. For instance V8 has its own bindings, with a V8-juice providing yet a higher-level binding SDK above V8. Similarly, there is SpiderApe for SpiderMonkey, but Ape dev seems to have stopped. Perhaps for same reason, there is no SWIG module for Javascript. Apparently can’t be done for SpiderMonkey, but I don’t see why it couldn’t be done for V8.

Scripting with C#

C# is quite a nice language. At least, after a couple of weeks of using it 🙂 I’ve been doing C++ and Python and Java for years and C# is a nice in-between. It is also supported on both Windows and Linux. So why not use it for scripting? There are mainly two ways of doing this: CS-Script and directly using the C# engine’s builtin code compiler. Note that Lua is very easy to embed in a C# application via the LuaInterface C# assembly. Advantage of pure csharp scripting is that there is no binding necessary, but advantage of Lua is simplicity. But could be used: Lua for quick setup/config scripts, and C# for more complex logic that would benefit from stronger type-checking and debugging.

CS-Script is like shell scripting, but it is not interactive. There seems to be an interactive c# script shell in Mono but not in .NET.

Another interesting creature is Script#: it converts C# code to javascript. This allows you to use the very powerful tools available with C# for development. However, for shell scripting there is no advantage: why translate to another language? similarly for embedded scripting there is no advantage: stick with C# so no binding necessary. Would be useful for making C# code available to web browser.

MongoDB

A database engine that can be used from many different languages (C++, javascript) based on objects rather than relations.

Graylog2 for Syslog

There are a couple free applications that will gather syslog data and allow some filtering but I have been disappointed with them (like kiwilog and another I can’t remember). Then recently I found Graylog2 which uses MongoDB to store and query log messages, and Ruby on Rails to provide a nice web interface to the data.

Leave a comment