/* vim: set expandtab tabstop=2 shiftwidth=2 foldmethod=marker: */

var should  = require('should');
var fs  = require('fs');
var exec = require('child_process').exec;
var Log = require(__dirname + '/../');

describe('file log', function() {

  beforeEach(function (done) {
    exec('rm -rf "' + __dirname + '/tmp"', function (error) {
      should.ok(!error);
      done();
    });
  });

  /* {{{ should_log_create_and_write_works_fine() */
  it ('should_log_create_and_write_works_fine', function(done) {
    var _fn = __dirname + '/tmp/{YYYY}/test-{MM-DD}.log';
    var _me = Log.create({
      'file'  : _fn
    });
    _me.debug('i will be ignore');
    _me.notice("i am a bad boy");
    _me.close();
    setTimeout(function() {
      var _text = fs.readFileSync(_me.__logfile(), 'utf-8');
      _text.should.not.include("DEBUG:\t");
      _text.should.include("NOTICE:\t");
      _text.should.include("\"i am a bad boy\"");
      done();
    }, 50);
  });
  /* }}} */

  /* {{{ should_set_log_level_works_fine() */
  it ('should_set_log_level_works_fine', function(done) {
    var _fn = __dirname + '/tmp/test.log';
    var _me = Log.create({
      'file'  : _fn, 'level' : Log.WARN | Log.ERROR,
    });

    _me.debug('aa i will be ignore');
    _me.notice('bb i am a bad boy');
    _me.warn('cc i am a bad boy');
    _me.error('dd i am a bad boy');
    _me.close();
    _me.error('ee i am a bad boy');

    setTimeout(function() {
      var _text = fs.readFileSync(_fn, 'utf8');

      _text.should.not.include("DEBUG:\t");
      _text.should.not.include("NOTICE:\t");
      _text.should.include("WARN:\t");
      _text.should.include("ERROR:\t");
      _text.should.not.include("ee i am a bad boy");

      done();
    }, 50);
  });
  /* }}} */

  /* {{{ should_log_append_write_works_fine() */
  it('should_log_append_write_works_fine', function(done) {
    var _fn = __dirname + '/tmp/test.log';
    var lg1 = Log.create({'file' : _fn});
    var lg2 = Log.create({'file' : _fn});
    lg1.error('LOG1');
    lg1.debug('DEBUG');
    lg2.error('LOG2');
    lg1.close();
    lg2.close();

    setTimeout(function() {
      var _text = fs.readFileSync(_fn, 'utf8');
      _text.should.include("LOG1");
      _text.should.include("LOG2");
      done();
    }, 50);
  });
  /* }}} */

  /* {{{ should_exception_log_works_fine() */
  it('should_exception_log_works_fine', function (done) {
    var _fn = __dirname + '/tmp/test.log';
    Log.setExceptionLogger({'file' : _fn, 'level' : Log.WARN});

    Log.logException({'a' : 'I will not be loged'});

    var err = new Error('hello');
    Log.logException(err, {'key1' : 'value1', 'key2' : ['value2\naa']});

    /**
     * @ err对象不能被修改
     */
    err.name.should.eql('Error');

    setTimeout(function() {
      var _text = fs.readFileSync(_fn, 'utf8');
      _text.should.not.include('I will not be loged');
      _text.should.include('ErrorException');
      _text.should.include(err.stack);
      _text.should.include('key2: ["value2\\naa"]');
      done();
    }, 50);
  });
  /* }}} */

});

@@ -1307,32 +1307,76 @@
 am a bad boy');%0A
+    _me.setLogLevel(Log.ERROR %7C Log.DEBUG);%0A
     _me.error('d
@@ -1387,32 +1387,57 @@
 am a bad boy');%0A
+    _me.debug('debug2');%0A
     _me.close();
@@ -1579,32 +1579,43 @@
 ot.include(%22
-DEBUG:%5Ct
+aa i will be ignore
 %22);%0A      _t
@@ -1686,24 +1686,24 @@
 %22WARN:%5Ct%22);%0A
-
       _text.
@@ -1726,24 +1726,62 @@
 ERROR:%5Ct%22);%0A
+      _text.should.include('debug2');%0A
       _text.
@@ -3160,12 +3160,162 @@
 %7D%7D%7D */%0A%0A
+  it('should_console_log_when_open_fail', function (done) %7B%0A    var _me = Log.create(%7B'file' : ''%7D);%0A    _me.error('console.log');%0A    done();%0A  %7D);%0A%0A
 %7D);%0A

Add test case for open log error