
/*
log levels:
0,1,2,3
verbose,info,warn,error

Default setting for logs is "info"
Default setting to show is "info"

Possible values of level/loglevel:
verbose,info,warn,error,win,silent

silent quiets everything


*/


module.exports = log
var sys = require("sys")
  , npm = require("../../npm")

var doColor
try {
  var stdio = require("stdio")
  doColor = stdio.isStderrATTY()
} catch (ex) {
  doColor = true
}

function colorize (msg, color) {
  if (!msg) return ""
  if (!doColor) return msg
  return "\033["+color+"m"+msg+"\033[0m"
}

var l = 0
  , LEVEL = { verbose : l++
            , info    : l++
            , WARN    : l++
            , ERROR   : l++
            , win     : 0x15AAC5
            , silent  : 0xDECAFBAD
            }
  , COLOR = {}
  , SHOWLEVEL
Object.keys(LEVEL).forEach(function (l) {
  LEVEL[LEVEL[l]] = l
  LEVEL[l.toLowerCase()] = LEVEL[l]
  if (l === "silent") return
  log[l] = log[l.toLowerCase()] =
    function (msg, pref, cb) { return log(msg, pref, l, cb) }
})
// COLOR[LEVEL.verbose] = '0'
COLOR[LEVEL.info] = 32
COLOR[LEVEL.warn] = 33
COLOR[LEVEL.error] = 31
for (var c in COLOR) COLOR[LEVEL[c]] = COLOR[c]
COLOR.npm = 37
COLOR.pref = 35

var logBuffer = []
  , ini = require("./ini")

function log (msg, pref, level, cb) {
  if (typeof level === "function") cb = level, level = null
  var show
  if (typeof SHOWLEVEL !== "undefined") {
    show = SHOWLEVEL
    if (show === LEVEL.silent || show === "silent") {
      return cb && cb()
    }
  } else {
    show = npm.config.get("loglevel")
    if (!show) return logBuffer.push(arguments)
    show = show.split(",")[0]
    if (!isNaN(show)) show = +show
    else if (!LEVEL.hasOwnProperty(show)) {
      sys.error("Invalid loglevel config: "+JSON.stringify(show))
      show = "info"
    }
    if (isNaN(show)) show = LEVEL[show]
    else show = +show
  }
  if (level == null) level = LEVEL.info
  if (isNaN(level)) level = LEVEL[level]
  else level = +level
  if (typeof msg !== "string" && !(msg instanceof Error)) {
    msg = sys.inspect(msg, 0, 4)
  }
  if (msg instanceof Error) {
    msg = msg.stack || msg.toString()
    level = LEVEL.error
  }
  if (!ini.resolved) {
    return logBuffer.push([msg, pref, level])
  }
  if (logBuffer.length && !logBuffer.discharging) {
    logBuffer.push([msg, pref, level])
    logBuffer.discharging = true
    logBuffer.forEach(function (l) { log.apply(null, l) })
    logBuffer.length = 0
    delete logBuffer.discharging
    return
  }
  SHOWLEVEL = show

  if (!isFinite(level) || level < show) return cb && cb()
  // console.error("level, showlevel, show", level, show, (level >= show))
  pref = colorize("npm", COLOR.npm)
       + (COLOR[level] ? " "+colorize(LEVEL[level], COLOR[level]) : "")
       + (pref && COLOR.pref ? " " + colorize(pref, COLOR.pref) : "")
  if (msg.indexOf("\n") !== -1) {
    msg = msg.split(/\n/).join("\n"+pref+" ")
  }
  msg = pref+" "+msg
  sys.error(msg)
  cb && cb()
}

log.er = function (cb, msg) {
  if (!msg) throw new Error(
    "Why bother logging it if you're not going to print a message?")
  return function (er) {
    if (er) log.error(msg)
    cb.apply(this, arguments)
  }
}

@@ -544,17 +544,18 @@
 var l = 
-0
+-1
 %0A  , LEV
@@ -560,16 +560,44 @@
 EVEL = %7B
+ silly   : l++%0A            ,
  verbose

add silliness