2015-10-31 14:27:16 +00:00
|
|
|
var async = require('async');
|
|
|
|
var fs = require('fs.extra');
|
|
|
|
var path = require('path');
|
|
|
|
var userhome = require('userhome');
|
|
|
|
|
|
|
|
var Logs = function (config) {
|
|
|
|
this.config = config;
|
|
|
|
|
|
|
|
if (this.config.type === 'linux') {
|
|
|
|
fs.mkdirp(this.logsPath());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Logs.generateLogFileName = function () {
|
|
|
|
var dateStr = new Date().toISOString().
|
|
|
|
replace(/:/g, '-'). // Replace time dividers with dash
|
|
|
|
replace(/T/, '_'). // Remove date and time divider
|
|
|
|
replace(/\..+/, ''); // Remove milliseconds
|
|
|
|
return 'arma3server_' + dateStr + '.log';
|
|
|
|
};
|
|
|
|
|
|
|
|
Logs.prototype.generateLogFilePath = function () {
|
|
|
|
return path.join(this.logsPath(), Logs.generateLogFileName());
|
|
|
|
};
|
|
|
|
|
|
|
|
Logs.prototype.logsPath = function () {
|
|
|
|
if (this.config.type === 'linux') {
|
|
|
|
return path.join(this.config.path, 'logs');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.config.type === 'windows') {
|
|
|
|
return userhome('AppData', 'Local', 'Arma 3');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.config.type === 'wine') {
|
|
|
|
var username = process.env.USER;
|
|
|
|
return userhome('.wine', 'drive_c', 'users', username, 'Local Settings', 'Application Data', 'Arma 3');
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
Logs.prototype.logFiles = function (callback) {
|
|
|
|
var directory = this.logsPath();
|
|
|
|
|
|
|
|
if (directory === null) {
|
|
|
|
callback(null, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
fs.readdir(directory, function (err, files) {
|
|
|
|
if (err) {
|
|
|
|
callback (err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
files = files.map(function (file) {
|
|
|
|
return {
|
|
|
|
name: file,
|
|
|
|
path: path.join(directory, file),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
async.filter(files, function(file, cb) {
|
|
|
|
fs.stat(file.path, function (err, stat) {
|
|
|
|
file.size = stat.size;
|
|
|
|
cb(!err && stat.isFile());
|
|
|
|
});
|
|
|
|
}, function (files) {
|
|
|
|
files.sort(function (a, b) {
|
|
|
|
return a.name.toLowerCase().localeCompare(b.name.toLowerCase());
|
|
|
|
});
|
|
|
|
|
|
|
|
callback(null, files);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Logs.prototype.getLogFile = function (filename, callback) {
|
2015-11-21 18:02:46 +00:00
|
|
|
this.logFiles(function (err, files) {
|
2015-10-31 14:27:16 +00:00
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
} else {
|
|
|
|
var validLogs = files.filter(function (file) {
|
2015-11-22 10:06:40 +00:00
|
|
|
return file.name === filename;
|
2015-10-31 14:27:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (validLogs.length > 0) {
|
2015-11-22 10:11:35 +00:00
|
|
|
callback(null, validLogs[0]);
|
2015-10-31 14:27:16 +00:00
|
|
|
} else {
|
|
|
|
callback(null, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Logs;
|