      1 var define = require("../../define.js").define,
      2 		base = require("../../base"),
      3 		promise = require("../../promise"),
      4 		string = base.string,
      5 		Promise = promise.Promise,
      6 		PromiseList = promise.PromiseList,
      7 		style = string.style,
      8 		format = string.format,
      9 		Appender = require("./appender"),
     10 		Level = require("../level"),
     11 		fs = require("fs");
     12
     13 /**
     14  * @class Appends messages to a file.
     15  *
     16  * <pre class="code">
     17  *      var fileAppender = new comb.logging.appenders.FileAppender({
     18  *                                       file : "/var/log/myLog.log"
     19  *                                      });
     20  * </pre>
     21  *
     22  * @name FileAppender
     23  * @augments comb.logging.appenders.Appender
     24  * @memberOf comb.logging.appenders
     25  *
     26  * @param {Object} [options] options to assign to this Appender
     27  * @param {String} [options.name="appender"] the name of this Appender. If you want two of the same type of appender
     28  *                                           on a logger it must have a different name.
     29  * @param {String} [options.pattern="[{[yyyy-MM-ddTHH:mm:ss:SSS (z)]timeStamp}] {[- 5]levelName} {[-20]name} - {message}"]
     30  *  <p>Available Options for formatting see {@link comb.string.format} for formatting options</p>
     31  *  <ul>
     32  *      <li>timeStamp - the timestamp of the event being logged</li>
     33  *      <li>level - the {@link comb.logging.Level} of the event</li>
     34  *      <li>levelName - the name of the level being logged</li>
     35  *      <li>name - the name of the logger logging the event</li>
     36  *      <li>message - the message being logged</li>
     37  * </ul>
     38  * @param {comb.logging.Level|String} [options.level=comb.logging.Level.INFO] the logging level of this appender
     39  *      <p><b>Note:</b> the level can be different from the logger in the case that you want a particular logger
     40  *      to only log particular event of a level. For example an appender that only logs errors. BEWARE that if the
     41  *      appenders level is lower than the logger is will not recieve any messages.</p>
     42  *
     43  * @param {String} [options.file="./log.log"] the file to log events to.
     44  * @param {String} [options.encoding="utf8"] the encoding of the file.
     45  * @param {Boolean} [options.overwrite=false] if true the log file is overwritten otherwise it is appended to.
     46  *
     47  */
     48 exports = module.exports = define(Appender, {
     49 			instance : {
     50
     51 				constructor : function(options) {
     52 					options = options || {};
     53 					!options.name && (options.name = "fileAppender");
     54 					this.__file = options.file || "./log.log";
     55 					this.__encoding = options.encoding || "utf8";
     56 					this.__overwrite = options.overwrite || false;
     57 					this.__writeStream = options.writeStream || fs.createWriteStream(this.__file, { flags: this.__overwrite ? "w" : 'a', encoding: this.__encoding});
     58 					this.super(arguments, [options]);
     59 					this.__pattern += "\n";
     60 					process.addListener("exit", base.hitch(this, "__onExit"));
     61 				},
     62
     63 				__onExit : function() {
     64 					var ret = new Promise();
     65 					var ws = this.__writeStream;
     66 					this.__writeStream = null;
     67 					ws.on("close", base.hitch(ret, "callback"));
     68 					ws.destroySoon();
     69 					return ret;
     70 				},
     71
     72 				append : function(event) {
     73 					var ws = this.__writeStream;
     74 					if (this._canAppend(event) && ws && ws.writable) {
     75 						var message = format(this.__pattern, event);
     76 						var level = event.level;
     77 						ws.write(message);
     78 					}
     79 				}
     80 			}
     81 		});