const FLOCK_LOGGER_CID = Components.ID('{05428cec-9fd4-4955-afc9-eaaf4c6d9f9d}');
const FLOCK_LOGGER_CONTRACTID = '@flock.com/logger;1';
const FLOCK_LOGGER_IID = Components.interfaces.flockILogger;

const LOGGER_DIR_NAME = "log";
const LOGGER_EXT = ".log";

function flockLogger() {
}

flockLogger.prototype= {
};

flockLogger.prototype.init = function (aModuleName) {
  // Prepare the log directory
  var dirService = Components.classes['@mozilla.org/file/directory_service;1'].getService(Components.interfaces.nsIProperties);
  var profileDir = dirService.get('ProfD', Components.interfaces.nsILocalFile);
  var logdir = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
  logdir.initWithPath(profileDir.path);
  logdir.append(LOGGER_DIR_NAME);
  if (!logdir.exists())
    logdir.create(1, 0700);

  // Prepare the log file for the requested module
  var file = Components.classes['@mozilla.org/file/local;1'].createInstance(Components.interfaces.nsILocalFile);
  file.initWithPath(profileDir.path);
  file.append(LOGGER_DIR_NAME);
  file.append(aModuleName + LOGGER_EXT);
  if(!file.exists()) file.createUnique(0,0600);

  transport = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
  transport.init( file, 0x04 | 0x08 | 0x10, 064, 0 );

  this.mFile = file;
  this.mTransport = transport;
}

flockLogger.prototype.write = function (aContent) {
  if (this.isLoggerOn())
    this.mTransport.write(aContent, aContent.length);
}


flockLogger.prototype.isLoggerOn = function (aContent) {
  var prefsService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  try {
    return prefsService.getBoolPref("flock.logging.on");
  } catch(e) {
    dump("Error "+e+"\n");
    return false;
  }
}

///////////////////////////////////////////////////////////////////////////////////////////////
// Factory Definitions
/////////////////////////////////////////////////////////////////////////////////////////////
var flockLoggerModule = {
    registerSelf: function(compMgr, fileSpec, location, type) {
        compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
        compMgr.registerFactoryLocation(FLOCK_LOGGER_CID, 
                                        "flockLogger JS component", 
                                        FLOCK_LOGGER_CONTRACTID, 
                                        fileSpec, 
                                        location,
                                        type);
    },

    getClassObject: function(compMgr, cid, iid) {
        if (!cid.equals(FLOCK_LOGGER_CID))
            throw Components.results.NS_ERROR_NO_INTERFACE;

        if (!iid.equals(Components.interfaces.nsIFactory))
            throw Components.results.NS_ERROR_NOT_IMPLEMENTED;

        return flockLoggerFactory;
    },

    canUnload: function(compMgr) { return true; }
};

var flockLoggerFactory = {
    createInstance: function(outer, iid) {
        if (outer != null)
            throw Components.results.NS_ERROR_NO_AGGREGATION;
    
        if (!iid.equals(FLOCK_LOGGER_IID) &&
            !iid.equals(Components.interfaces.nsISupports))
            throw Components.results.NS_ERROR_INVALID_ARG;

        return new flockLogger();
    }
}

/* module initialisation */
function NSGetModule(comMgr, fileSpec) { return flockLoggerModule; }

