Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / log4js / lib / appenders / file.js
1 "use strict";
2 var layouts = require('../layouts')
3 , path = require('path')
4 , fs = require('fs')
5 , streams = require('../streams')
6 , os = require('os')
7 , eol = os.EOL || '\n'
8 , openFiles = []
9 , levels = require('../levels');
10
11 //close open files on process exit.
12 process.on('exit', function() {
13   openFiles.forEach(function (file) {
14     file.end();
15   });
16 });
17
18 /**
19  * File Appender writing the logs to a text file. Supports rolling of logs by size.
20  *
21  * @param file file log messages will be written to
22  * @param layout a function that takes a logevent and returns a string
23  *   (defaults to basicLayout).
24  * @param logSize - the maximum size (in bytes) for a log file,
25  *   if not provided then logs won't be rotated.
26  * @param numBackups - the number of log files to keep after logSize
27  *   has been reached (default 5)
28  * @param compress - flag that controls log file compression
29  * @param timezoneOffset - optional timezone offset in minutes (default system local)
30  */
31 function fileAppender (file, layout, logSize, numBackups, compress, timezoneOffset) {
32   var bytesWritten = 0;
33   file = path.normalize(file);
34   layout = layout || layouts.basicLayout;
35   numBackups = numBackups === undefined ? 5 : numBackups;
36   //there has to be at least one backup if logSize has been specified
37   numBackups = numBackups === 0 ? 1 : numBackups;
38
39   function openTheStream(file, fileSize, numFiles) {
40     var stream;
41     if (fileSize) {
42       stream = new streams.RollingFileStream(
43         file,
44         fileSize,
45         numFiles,
46         { "compress": compress }
47       );
48     } else {
49       stream = fs.createWriteStream(
50         file,
51         { encoding: "utf8",
52           mode: parseInt('0644', 8),
53           flags: 'a' }
54       );
55     }
56     stream.on("error", function (err) {
57       console.error("log4js.fileAppender - Writing to file %s, error happened ", file, err);
58     });
59     return stream;
60   }
61
62   var logFile = openTheStream(file, logSize, numBackups);
63
64   // push file to the stack of open handlers
65   openFiles.push(logFile);
66
67   return function(loggingEvent) {
68     logFile.write(layout(loggingEvent, timezoneOffset) + eol, "utf8");
69   };
70
71 }
72
73 function configure(config, options) {
74   var layout;
75   if (config.layout) {
76     layout = layouts.layout(config.layout.type, config.layout);
77   }
78
79   if (options && options.cwd && !config.absolute) {
80     config.filename = path.join(options.cwd, config.filename);
81   }
82
83   return fileAppender(
84     config.filename,
85     layout,
86     config.maxLogSize,
87     config.backups,
88     config.compress,
89     config.timezoneOffset
90   );
91 }
92
93 function shutdown(cb) {
94   var completed = 0;
95   var error;
96   var complete = function(err) {
97     error = error || err;
98     completed++;
99     if (completed >= openFiles.length) {
100       cb(error);
101     }
102   };
103   if (!openFiles.length) {
104     return cb();
105   }
106   openFiles.forEach(function(file) {
107     if (!file.write(eol, "utf-8")) {
108       file.once('drain', function() {
109         file.end(complete);
110       });
111     } else {
112       file.end(complete);
113     }
114   });
115 }
116
117 exports.appender = fileAppender;
118 exports.configure = configure;
119 exports.shutdown = shutdown;