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.

Test offline blog editor: ScribeFire

ScribeFire was easy to install but it would only find the first of my several blogs I have in my one wordpress.com account. I could not get it to find my other ones. However I was able to Edit the blog URL and title and that seemed to work but adding new blogs would always have to go through same process. Also, categories not hierarchical.

That said, it shows the blog entries and you can reload any one for editing, and several posts can be edited in parallel.

Here is a link to an image:

You can resize the image while maintaining aspect ratio and can change width/height but you can edit it.

ScribeFire does provide a live preview but it didn’t work as well as WLW’s (layout of page was wrong). As with WLW, can edit source HTML too.

Save draft possible? Yes, after clicking “puglish” you get the option of saving to drafts (but not local drafts). Category hierarchy was flattened, as did BlogDesk, really annoying. Interface not as polished or sophisticated as WLW.

So far: WLW is #1, followed by a tie for second place between BlogDesk and ScribeFire.

Conditional Comments in HTML to Spot MS IE

IE is crippled and annoying for web developers (I’m a desktop software developer and just dabble in web development and already find it annoying). One thing MS has done is allow IE to parse comments to see if it contains a “condition”. Because the condition is in a comment, it is ignored by non-IE browsers. This helps decide at HTML load time what style sheet to use based on the client (agent) the user is using.  Here are examples taken from the MSDN knowledge base:

<!--[if IE]><p>You are using (some version of) Internet Explorer.</p><![endif]-->

<!--[if IE 7]><p>You are using version 7 of Internet Explorer</p><![endif]-->

<!--[if !(IE 7)]><p>You are using Internet Explorer but not version 7.</p><![endif]-->

<!--[if gte IE 7]><p>You are using Internet Explorer, version 7 or greater.</p><![endif]-->

<!--[if (gte IE 5.5)&(lt IE 7)]><p>You are using Internet Explorer, version 5.5.x or 6.x.</p><![endif]-->

<!--[if true]>You are using Internet Explorer (assuming no other browser ever supports condition comments).<![endif]-->

Note that, as pointed out by some posters on MSDN, testing for !IE means no browsers will use the comment since only IE supports conditional comments! (for now anyways).

I may have to use this for the PyPubSub website.


Moved blog to here

My blog used to be at clemworm.wordpress.com. Moved it here. WordPress Import/Export took care of all except the settings (theme, which widgets and where etc). Made all non-popular posts of old site private, leaving only a few public with a note redirecting here.

Follow

Get every new post delivered to your Inbox.