//
// BEGIN FLOCK GPL
//
// Copyright Flock Inc. 2005-2008
// http://flock.com
//
// This file may be used under the terms of the
// GNU General Public License Version 2 or later (the "GPL"),
// http://www.gnu.org/licenses/gpl.html
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// END FLOCK GPL
//

const CC = Components.classes;
const CI = Components.interfaces;
const CR = Components.results;

Components.utils.import("resource:///modules/FlockXPCOMUtils.jsm");
FlockXPCOMUtils.debug = false;

const MODULE_NAME = "Flock Logging Console Writer";

const CLASS_NAME = "Flock Logging Console Writer";
const CLASS_ID = Components.ID("{442BDAE4-4FDC-4819-8748-C8659E415410}");
const CONTRACT_ID = "@flock.com/logging-console-writer;1";

/**************************************************************************
 * Component: Flock Logging Console Writer
 **************************************************************************/

// Constructor.
function flockLoggingConsoleWriter() {
  this._converter = CC["@mozilla.org/intl/scriptableunicodeconverter"]
                    .createInstance(CI.nsIScriptableUnicodeConverter);
  this._converter.charset = "UTF-8";
}

/**************************************************************************
 * Flock Logging Console Writer: XPCOM Component Creation
 **************************************************************************/

flockLoggingConsoleWriter.prototype = new FlockXPCOMUtils.genericComponent(
  CLASS_NAME,
  CLASS_ID,
  CONTRACT_ID,
  flockLoggingConsoleWriter,
  CI.nsIClassInfo.SINGLETON,
  [CI.flockILoggingObserver]
);

// FlockXPCOMUtils.genericModule() categories
flockLoggingConsoleWriter.prototype._xpcom_categories = [
  { category: "flockILoggingObserver" }
];

/**************************************************************************
 * Flock Logging Console Writer: Private Data and Functions
 **************************************************************************/

// Member variables.
flockLoggingConsoleWriter.prototype._converter = null;

flockLoggingConsoleWriter.prototype._pad =
function LogConsWriter__pad(aNumber, aPlaces) {
  var numberString = aNumber + "";
  while (numberString.length < aPlaces) {
    numberString = "0" + numberString;
  }
  return numberString;
}

/**************************************************************************
 * Flock Logging Console Writer: flockILoggingService Implementation
 **************************************************************************/

flockLoggingConsoleWriter.prototype.emit =
function LogConsWriter_emit(aDate, aLevel, aContext, aMessage) {

  var levels = ["all", "debug", "info", "warn", "error", "fatal"];
  var date = new Date(aDate);
  var dateString = date.toLocaleFormat("%H:%M:%S.")
                 + this._pad(date.getMilliseconds(), 3);
  var content = "[" + dateString + " flock:" + aContext
              + ":" + levels[aLevel] + "] " + aMessage
              + "\n";
  content = this._converter.ConvertFromUnicode(content)
          + this._converter.Finish();
  dump(content);
}

/**************************************************************************
 * END Flock Logging Console Writer
 **************************************************************************/


/**************************************************************************
 * XPCOM Support - Module Construction
 **************************************************************************/

// Create array of components.
var gComponentsArray = [flockLoggingConsoleWriter];

// Generate a module for XPCOM to find.
var NSGetModule = FlockXPCOMUtils.generateNSGetModule(MODULE_NAME,
                                                      gComponentsArray);

/**************************************************************************
 * END XPCOM Support
 **************************************************************************/
