/*!
 * project - module
 * Copyright(c) 2014 dead_horse <dead_horse@qq.com>
 * MIT Licensed
 */

'use strict';

/**
 * Module dependencies.
 */

var os = require('os');
var util = require('util');
var utility = require('utility');

/**
 * Expose `formater`
 */

module.exports = text;
text.json = json;
text.both = both;


function text(err) {
  var infos = json(err);
  return util.format('%s nodejs.%s: %s\npid: %s\ndomainThrown: %s\nHost: %s\nURL: %s\nData: %j\n%s\n\n',
    infos.time,
    infos.name,
    infos.stack,
    infos.pid,
    infos.domainThrown,
    infos.hostname,
    infos.url,
    infos.data,
    infos.time
  );
}

function both(err) {
  var infos = json(err);
  var text = util.format('%s nodejs.%s: %s\npid: %s\ndomainThrown: %s\nHost: %s\nURL: %s\nData: %j\n%s\n\n',
    infos.time,
    infos.name,
    infos.stack,
    infos.pid,
    infos.domainThrown,
    infos.hostname,
    infos.url,
    infos.data,
    infos.time
  );

  return {
    text: text,
    json: infos
  }
}

function json(err) {
  if (!(err instanceof Error)) throw new TypeError('input non-error object: ' + err);

  var infos = {
    name: err.name,
    message: err.message,
    url: err.url || '',
    data: err.data || '',
    pid: process.pid,
    code: err.code
  };

  if (err.name === 'Error' && typeof err.code === 'string') {
    infos.name = err.code + err.name;
  }

  if (err.host) {
    infos.host = err.host;
    infos.message += ' (' + infos.host + ')';
  }
  infos.domainThrown = !!(err.domain_thrown || err.domainThrown);
  infos.name = infos.name + 'Exception';
  infos.time = utility.logDate();
  infos.hostname = os.hostname();
  // name and stack could not be change on node 0.11+
  var errName = err.name;

  var errStack = err.stack || 'no_stack';
  infos.stack = errName + ': ' + infos.message + '\n' + errStack.substring(errStack.indexOf('\n') + 1);
  return infos;
}

@@ -375,260 +375,20 @@
 urn 
-util.
 format(
-'%25s nodejs.%25s: %25s%5Cnpid: %25s%5CndomainThrown: %25s%5CnHost: %25s%5CnURL: %25s%5CnData: %25j%5Cn%25s%5Cn%5Cn',%0A    infos.time,%0A    infos.name,%0A    infos.stack,%0A    infos.pid,%0A    infos.domainThrown,%0A    infos.hostname,%0A    infos.url,%0A    infos.data,%0A    infos.time%0A  
+infos
 );%0A%7D
@@ -452,260 +452,20 @@
 t = 
-util.
 format(
-'%25s nodejs.%25s: %25s%5Cnpid: %25s%5CndomainThrown: %25s%5CnHost: %25s%5CnURL: %25s%5CnData: %25j%5Cn%25s%5Cn%5Cn',%0A    infos.time,%0A    infos.name,%0A    infos.stack,%0A    infos.pid,%0A    infos.domainThrown,%0A    infos.hostname,%0A    infos.url,%0A    infos.data,%0A    infos.time%0A  
+infos
 );%0A%0A
@@ -1397,12 +1397,412 @@
 rn infos;%0A%7D%0A
+%0Avar formatTpl = '%25s nodejs.%25s: %25s' + os.EOL +%0A  'pid: %25s' + os.EOL +%0A  'domainThrown: %25s' + os.EOL +%0A  'Host: %25s' + os.EOL +%0A  'URL: %25s' + os.EOL +%0A  'Data: %25j' + os.EOL +%0A  '%25s';%0A%0Afunction format(infos) %7B%0A  return util.format(formatTpl,%0A    infos.time,%0A    infos.name,%0A    infos.stack,%0A    infos.pid,%0A    infos.domainThrown,%0A    infos.hostname,%0A    infos.url,%0A    infos.data,%0A    infos.time%0A  );%0A%7D%0A

support os.EOL