A barebone logger for Go

While working on a Go library, I was looking for an elegant solution for logging. After following many discussions on the forum, I came up with the following barebone which I think is decent performant and clean. Check it out!

package main

import (
        "flag"
        "log"
)

type Logger struct {
        log_func func(string, ...interface{})
}

func do_log(fmt string, args ...interface{}) {
        log.Printf(fmt, args...)
}

func do_not_log(fmt string, args ...interface{}) {
        // Do nothing..
}

func (logger *Logger) init_logger(enable_log bool) {
        if enable_log == true {
                logger.log_func = do_log
        } else {
                logger.log_func = do_not_log
        }
}

func main() {
        logger := new(Logger)

        // command line option
        enable_log := flag.Bool("enable_log", false, "Enable logging")
        flag.Parse()

        logger.init_logger(*enable_log)

        logger.log_func("%s", "log me!")
        logger.log_func("%s", "log me again..!")
}

Output:

$ ./logger
$ ./logger –enable_log
2013/01/28 11:41:19 log me!
2013/01/28 11:41:19 log me again..!
$

Locating current position on map

HTML5 introduced new set of specifications which include geolocation APIs. Using them one can find the location of a device. While exploring it further, I found a simple Javascript program that neatly demonstrates the overall idea (references (i)). The following code has been modified a bit from the original one to make it more verbose.

  if(navigator.geolocation) {

    navigator.geolocation.getCurrentPosition(

      // success callback, where 'position' holds the found coordinates.
      function(position) {
      // Instantiate a Google Map's LatLng object using the position coordinates.
      initialLocation = new google.maps.LatLng(position.coords.latitude,
                                               position.coords.longitude);
      /*..
        Now 'initialLocation' can be used to place a marker on the map or
        other coordinates related stuff.
      ..*/
      },

      // error callback, where 'positionError' holds various error related
      // attributes (see reference (ii) for details).
      function(positionError) {
      // log the error message
      console.log("Geolocation service failed. MSG: " + positionError.message);
      },
      {maximumAge:Infinity, timeout:10000});
  }

  // Browser doesn't support Geolocation
  else {
    console.log("Your browser doesn't support geolocation.");
  }
}

References:
i) https://developers.google.com/maps/articles/geolocation
ii) http://dev.w3.org/geo/api/spec-source.html

Shell script: recursive find/replace

Lately I had a requirement of finding & replacing a specific string from all the files under a directory tree, while keeping the file permissions intact. Here is the shell script that I eventually came up..

#!/bin/bash

cd target_dir

for _file in `find .`;
do
  if [ ! -d $_file ]; then
    sed "s/SEARCH_STR/REPLACE_STR/g" $_file > tmp; cp tmp $_file; rm tmp;
  fi
done

Hope this would help somebody.. disfrutar!