Logging in Rust using Log

Having used a few logging frameworks/abstractions in .net, I was looking for an abstraction in Rust. Fortunately, easily found Log from Crates.io home page.

So, just thought of doing a simple proof of concept to see how my experience goes.

As a first step, created a new app using cargo

$ cargo new --bin log_sample

This creates a folder log_sample in my current folder.

Now, modified main.rs to look like below..

Basically, added info! and error! calls to see the output, using log and env_logger crates.

Now, if i run the application,

$ cargo run
     Running `target/debug/log_sample`
ERROR:log_sample: error
Hello, world!

I don’t see “starting up!” appearing here. I tried to run using the app..

$ ./target/debug/log_sample 
ERROR:log_sample: error
Hello, world!

This also do not show up the “starting up”. Is there is something wrong? Not really. By default, it is logging only errors, which makes sense. How do I turn on the info logging also? Instructions from env_logger documentation is clear enough, and I tried the following.

$ RUST_LOG=info ./target/debug/log_sample 
INFO:log_sample: starting up
ERROR:log_sample: error
Hello, world!

There we go!! I do see the “starting up” now. So, the environment variable RUST_LOG is controlling what kind of data to log. Once I turned on info log using RUST_LOG=info, the info! calls also shows its output.

So, that was a cool abstraction for logging. Pretty good indeed!!

References:
https://github.com/rust-lang/log
http://doc.rust-lang.org/log/log/index.html
http://rust-lang.github.io/log/env_logger/

Hello Rust on a weekday evening!!

I was pretty interested, when Rust reached 1.0. Always wanted to say hello to rust some day or the other. Its approach on handling memory without using Garbage collector is very interesting. Rust way of ‘Ownerships’ and scopes were unique and innovative on its own, which makes me think, why this approach wasn’t tried before (Or, has somebody else already gave it a shot?!)

Anyway, today evening, I thought I should say Hello to Rust (rustlang). So, how did my experiment go?

Installation

Installation was fairly easy. I just had to run the following command in my terminal

curl -sSf https://static.rust-lang.org/rustup.sh | sh

HelloWorld!

Created a folder, hello_world, and created a new file main.rs

fn main() {
    println!("Hello, World!");
}

After saving the file, typed following in terminal..

rustc main.rs

That’s it. The application is created in the same folder (main), and running it was as easy as ..

./main

This prints “Hello, World!” in the terminal.

Based on my reading so far, it appears Rust is having a feel of high level languages, while still being a low level language, like C.

I’m very excited to say hello to Rust, and I intend to play with Rust more.

References

http://doc.rust-lang.org/book/

Getting started with Asp.Net VNext in Ubuntu/Linux – Step by Step

Happy to find that the next version of Asp.Net will be officially supported by Microsoft on Linux and Macs🙂

Thought of giving it a shot on Ubuntu Linux to see how it works..

Credits:
Followed these guides/scripts..

https://github.com/aspnet/home
http://carolynvanslyck.com/blog/2014/09/dotnet-vnext-impressions/
http://www.ganshani.com/blog/2014/12/shell-script-to-setup-net-on-linux/

Step 1: Install Mono

wget http://download.mono-project.com/repo/xamarin.gpg
sudo apt-key add xamarin.gpg
echo "deb http://download.mono-project.com/repo/debian wheezy main" | sudo tee --append /etc/apt/sources.list.d/mono-xamarin.list
sudo apt-get update
sudo apt-get install mono-complete
 
sudo certmgr -ssl -m https://go.microsoft.com
sudo certmgr -ssl -m https://nugetgallery.blob.core.windows.net
sudo certmgr -ssl -m https://nuget.org
mozroots --import --sync

Step 2: Install K Version Manager (KVM)

curl -sSL https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.sh | sh && source ~/.kre/kvm/kvm.sh

Step 3: Install K Runtime Environment (KRE)

kvm upgrade

Step 4: Get Sample Codes

git clone https://github.com/aspnet/Home.git

Step 5: Run Console Sample

cd Home/samples/ConsoleApp
kpm restore
k run

Step 6: Run Web Sample

cd Home/samples/HelloWeb
kpm restore
k kestrel

Now, go and launch your browser and navigate to http://localhost:5004

This gets some errors like.. “Object reference not set to an instance of an object”. As of now, to resolve this error, need to get a compatible version of libuv

wget http://dist.libuv.org/dist/v1.0.0-rc2/libuv-v1.0.0-rc2.tar.gz
tar -xvf libuv-v1.0.0-rc2.tar.gz
cd libuv-v1.0.0-rc2/
./gyp_uv.py -f make -Duv_library=shared_library
make -C out
sudo cp out/Debug/lib.target/libuv.so /usr/lib/libuv.so.1.0.0-rc2
sudo ln -s libuv.so.1.0.0-rc2 /usr/lib/libuv.so.1

Now, if you navigate to http://localhost:5004, you will see a nice welcome page🙂

That’s it.. Go ahead and try other samples from the downloaded repository folder.
Running asp.net vnext applications in Ubuntu/Linux is as easy as this!!

Using Python / pywinauto for automating Windows GUI applications

Install pywinauto

I prefer installing python packages using pip. Refer to my previous blog https://techsaju.wordpress.com/2013/03/06/install-python-and-pip-on-windows/ for installing python and pip in windows

Once pip is installed, run the following from the commandline to install pywinauto

pip install pywinauto

Install swapy

Download and install swapy from https://code.google.com/p/swapy/

Swapy is a nice tool for inspecting the GUI apps, which helps greatly in genearating pywinauto code

Sample Codes

Automating Notepad

import pywinauto
pwa_app = pywinauto.application.Application()
pwa_app.start_('notepad.exe')

#Type some keys into the editbox
pwa_app.UntitledNotepad.Edit.TypeKeys("test{SPACE}from{SPACE}pywinauto")

#Get handle to the window
w_handle = pywinauto.findwindows.find_windows(title=u'Untitled - Notepad', class_name='Notepad')[0]
window = pwa_app.window_(handle=w_handle)

#Get text from the edit box
edit = window['Edit']
props = edit.GetProperties()
print props['Texts']

#Trigger the About menu item
window.MenuItem(u'&Help->&About Notepad').Click()

#Close the About window
w_about_handle = pywinauto.findwindows.find_windows(title=u'About Notepad', class_name='#32770')[0]
w_about = pwa_app.window_(handle=w_about_handle)
w_about.TypeKeys('%{F4}')

#Playing with list/combobox
window.MenuItem(u'F&ormat->&Font...').Click()

w_format_handle = pywinauto.findwindows.find_windows(title=u'Font', class_name='#32770')[0]
w_format = pwa_app.window_(handle=w_format_handle)

combo = w_format['ComboBox']
combo_props  = combo.GetProperties()
print combo_props['Texts'] #Prints all items in the combobox

Install Python and pip on Windows

Install Python

Install Python 2.7.3 from http://www.python.org/download/releases/2.7.3/

I installed to the default path C:\Python27.

Modify system/user environment variable PATH to include this path.

Install easy_install and pip

Download setup_tools for python 2.7 from https://pypi.python.org/pypi/setuptools and run the installer wizard.

This installs easy_install.exe in C:\Python27\Scripts (I’m using the default path, your path could vary)

Add this path also to the PATH environment variable.

From command line, run the following to install pip. This installs pip.exe to the same path  C:\Python27\Scripts

easy_install pip

Reference: http://blog.troygrosfield.com/2010/12/18/installing-easy_install-and-pip-for-python/

Mount usb drive in ArchLinux / Raspberry Pi

I prefer mounting devices by UUID, in general.

So, step 1, I did was to find the UUID of the usb drive. Run the following command..

sudo blkid -o list -c /dev/null

In my case, the command returned like below

device                      fs_type     label        mount point                     UUID
--------------------------------------------------------------------------------------------------------------------------
/dev/mmcblk0                                         (in use)                        
/dev/mmcblk0p1              vfat                     /boot                           44C8-CEF1
/dev/mmcblk0p2              ext4                     /                               fcee8534-f5f0-42ee-83ac-f943f878ee67
/dev/sda1                   vfat                     /media/data                     DB86-F67F

Here, DB86-F67F is the UUID of my usb drive!!

I wanted the normal user to be able to mount the drive. So, added the following into /etc/fstab file.

UUID=DB86-F67F /media/data vfat user,noauto,noatime,flush 0 0

Create a dir like, /media/data, and as a normal user run the following to mount the usb drive

mount /media/data

That’s all! I have mounted the usb drive into /media/data. You can replace /media/data with anything else you like!!

Reference: https://wiki.archlinux.org/index.php/USB_Storage_Devices

Using tmux to remotely work on Raspberry Pi

After having bought the device, I’m finding it to be quite cool. Considering the fact that it takes a very less power (around 3.5W) makes it good to okay it running all the time🙂

And, being able to run node.js from Raspberry Pi is quite useful. In order to keep the node.js running, even after I quit the ssh session, I wanted something, like ‘screen’ unix command. Finally, I ended up choosing tmux than screen, as tmux appears to be a modern equivalent with active development going on..

Btw, I used the ArchLinux Arm, instead of raspbian, as I liked the fact it is a rolling release distro, which also I wanted to experiment with. (Having used Debian/Ubuntu for a long time now)

Anyway, coming to the point, the process is quite easy for using tmux from cherrypy.
ssh server was already installed with Archlinux ARM. So, this step is taken care. (If not, just install an ssh server, like openssh)

The next step is to install tmux.
Use pacman to install tmux..

sudo pacman -S tmux

Now, from my other machine (I typically use ConnectBot in my Android Phone/tablet, with Hacker’s keyboard for better keyboard experience), connect to the Raspberry Pi device, and just type

tmux

This creates a new tmux session in Raspberry Pi. Here we need to run the necessary commands, say, run the node.js app.
Once we are done, just execute the following keyboard combination, to detach the tmux session.
Ctrl + B D (i.e., Press Ctrl + B and then press key D)

Please note, the tmux session still continues to run in the Raspberry Pi device🙂

Now, if we need to login back after some time and attach to the tmux session..

tmux attach

That’s it. Now, our shell will be connected to the old tmux session, as if we were still continuing to operate. Isn’t it cool🙂
I’m loving it. Especially, now I can use my Pi for downloading stuff, without having my main pc switched on all the time..

References:
http://robots.thoughtbot.com/post/2641409235/a-tmux-crash-course
http://blog.hawkhost.com/2010/06/28/tmux-the-terminal-multiplexer

Using express to generate an app

Continuing from the previous post on getting started with node.js, let’s now create a simple app using the express app generation tool, which helps in setting the basic app code, and also creates the recommended directory structure.

Install express globally

npm install -g express

I want to create an app with sessions support, and also wanted to use ejs for templating

express --sessions --ejs myapp

Install dependancies

cd myapp
npm install

Run the app

node app

That’s it. Now, navigate to http://localhost:3000 and you’ll see the barebone app running!!

Reference Url: http://expressjs.com/guide.html

Getting started with node.js

What should I try this week end? One thing I thought of doing for some time was, exploring node.js. Reasons? #1 – Yes, just wanted to continue my learning with something new #2 – was interested in exploring usage of Javascript on the server side (than the usual browser side)!!

Installation

Installation in ubuntu was fairly easy, once I found this page by googling https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager

sudo apt-get install python-software-properties
sudo add-apt-repository ppa:chris-lea/node.js
sudo apt-get update
<code>sudo apt-get install nodejs npm</code>

Note: Same url has installation instructions for Windows and other plats.

Hello World!

As always, just starting with a helloworld program: Typed the following in a new file (helloworld.js):

console.log('hello world');

Opened a terminal window and ran the following:

node helloworld.js

Yes! Got ‘Hello World!’ displayed at the terminal. Cool!

Hello Http!

First helloworld program was okay! What’s next? How about a helloworld web page?
(File: hellohttp.js)

var http = require('http')

var requestListener = function (req, res) {
res.writeHead(200);
res.end('hello, http!');
};

var server = http.createServer(requestListener);

server.listen(process.env.PORT || 8080, process.env.IP || "0.0.0.0");

Again, went to the terminal and ran

node hellohttp.js

Opened browser and navigated to http://localhost:8080 and I see “hello, http!” displayed in the browser! Cool! Isn’t it!!

Hello http from express!

Let me finish by making use of node package ‘express’, which simplifies url routing. First step was to install ‘express’ package using npm (node package manager, which was installed at the beginning along with node) Opened terminal and ran the following to install ‘express’

npm install express

Note: We need to execute this form the same folder as our code files (or, any of its parent folders) Now, onto a sample code:

var express = require('express');
var app = express();

app.use(express.logger());

app.get('/', function(req, res) {
res.send('hello http from express');
});

app.listen(process.env.PORT || 8080);

At the end of this little exercise over a weekend day, node.js seems very interesting! Need to explore further on html templating, database connectivity, etc. Let me see how it goes🙂

(Re-posted from http://techsaju.blogspot.in/2013/01/getting-started-with-nodejs.html)