Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / lib / appenders / dateFile.js
1 "use strict";
2 var streams = require('../streams')
3 , layouts = require('../layouts')
4 , path = require('path')
5 , os = require('os')
6 , eol = os.EOL || '\n'
7 , openFiles = [];
8
9 //close open files on process exit.
10 process.on('exit', function() {
11   openFiles.forEach(function (file) {
12     file.end();
13   });
14 });
15
16 /**
17  * File appender that rolls files according to a date pattern.
18  * @filename base filename.
19  * @pattern the format that will be added to the end of filename when rolling,
20  *          also used to check when to roll files - defaults to '.yyyy-MM-dd'
21  * @layout layout function for log messages - defaults to basicLayout
22  * @timezoneOffset optional timezone offset in minutes - defaults to system local
23  */
24 function appender(filename, pattern, alwaysIncludePattern, layout, timezoneOffset) {
25   layout = layout || layouts.basicLayout;
26
27   var logFile = new streams.DateRollingFileStream(
28     filename,
29     pattern,
30     { alwaysIncludePattern: alwaysIncludePattern }
31   );
32   openFiles.push(logFile);
33
34   return function(logEvent) {
35     logFile.write(layout(logEvent, timezoneOffset) + eol, "utf8");
36   };
37
38 }
39
40 function configure(config, options) {
41   var layout;
42
43   if (config.layout) {
44     layout = layouts.layout(config.layout.type, config.layout);
45   }
46
47   if (!config.alwaysIncludePattern) {
48     config.alwaysIncludePattern = false;
49   }
50
51   if (options && options.cwd && !config.absolute) {
52     config.filename = path.join(options.cwd, config.filename);
53   }
54
55   return appender(
56     config.filename,
57     config.pattern,
58     config.alwaysIncludePattern,
59     layout,
60     config.timezoneOffset
61   );
62 }
63
64 function shutdown(cb) {
65   var completed = 0;
66   var error;
67   var complete = function(err) {
68     error = error || err;
69     completed++;
70     if (completed >= openFiles.length) {
71       cb(error);
72     }
73   };
74   if (!openFiles.length) {
75     return cb();
76   }
77   openFiles.forEach(function(file) {
78     if (!file.write(eol, "utf-8")) {
79       file.once('drain', function() {
80         file.end(complete);
81       });
82     } else {
83       file.end(complete);
84     }
85   });
86 }
87
88 exports.appender = appender;
89 exports.configure = configure;
90 exports.shutdown = shutdown;