Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var fs = require("fs"),
path = require("path");
module.exports = function Helpers(config) {

function cleanOutFile(filePath) {
fs.truncateSync(filePath, 0);
if(config && config.logger) {
config.logger.info("+cleared out file: " + filePath);
}
}

function createFile(filePath) {
if(ensureDirectories(filePath)) {
fs.writeFileSync(filePath, "");
if(config && config.logger) {
config.logger.info("+created ast output file: ");
}
}
cleanOutFile(filePath)
}

function ensureDirectories(filePath) {
var parentDir = path.dirname(filePath);
if(fs.existsSync(parentDir)) {
return true;
}

ensureDirectories(parentDir);
fs.mkdirSync(parentDir);
return true;
}

return {
cleanOutFile: cleanOutFile,
createFile: createFile,
ensureDirectories: ensureDirectories
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var stringifyOnce = function(obj, replacer, indent){
var printedObjects = [];
var printedObjectKeys = [];
var passingIndent = 4;
if(indent) {
passingIndent = indent;
}

function printOnceReplacer(key, value){
if ( printedObjects.length > 2000){ // browsers will not print more than 20K, I don't see the point to allow 2K.. algorithm will not be fast anyway if we have too many objects
return 'object too long';
}
var printedObjIndex = false;
printedObjects.forEach(function(obj, index){
if(obj===value){
printedObjIndex = index;
}
});

if ( key == ''){ //root element
printedObjects.push(obj);
printedObjectKeys.push("root");
return value;
}

else if(printedObjIndex+"" != "false" && typeof(value)=="object"){
if ( printedObjectKeys[printedObjIndex] == "root"){
return "(pointer to root)";
}else{
return "(see " + ((!!value && !!value.constructor) ? value.constructor.name.toLowerCase() : typeof(value)) + " with key " + printedObjectKeys[printedObjIndex] + ")";
}
}else{

var qualifiedKey = key || "(empty key)";
printedObjects.push(value);
printedObjectKeys.push(qualifiedKey);
if(replacer){
return replacer(key, value);
}else{
return value;
}
}
}
return JSON.stringify(obj, printOnceReplacer, passingIndent);
};

module.exports = stringifyOnce;
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
var fs = require('fs'),
path = require('path'),
os = require('os'),
fileHelpers = require("./file_helpers")();

module.exports = function(setting){

var logDirectory = path.dirname(setting.logPath);
if (!fs.existsSync(logDirectory)){
console.error("couldn't find logDirectory so it will be created in place:" + setting.logPath);
fileHelpers.ensureDirectories(setting.logPath);
}
if (os.type().indexOf('Windows') === -1) {
var appLogStat = fs.statSync(logDirectory);
if (canWrite(process.uid === appLogStat.uid, process.gid === appLogStat.gid, appLogStat.mode)){
console.error("ERROR WRITING TO LOG FILE DIRECTORY : " + logDirectory);
process.exit(-1);
}
}

var appLog = createLog(setting.APP_NAME, logDirectory, setting);

if(setting.disable) {
for(var prop in appLog) {
appLog[prop] = function() {};
}
}
return appLog;
};

function canWrite(owner, inGroup, mode){
return owner && (mode & 00200) || // User is owner and owner can write.
inGroup && (mode & 00020) || // User is in group and group can write.
(mode & 00002); // Anyone can write.
}

function createLog(appName, logDirectory, setting){

var appLog = {};

function getRequestId() {
return (process.domain && process.domain.id) || "";
}

function getLogDate(){
var today = new Date();

var fullYear = today.getFullYear();
var month = today.getMonth();
var day = today.getDate();
var hours = today.getHours();
var minutes = today.getMinutes();
var seconds = today.getSeconds();

var logDate = fullYear + "-" + month + "-" + day + "/" + hours + ":" + minutes + ":" + seconds;
return logDate;
}

function setMessageWithFormat(message) {
var res = getLogDate() + "\t" + message;
return res;
}

function appendToLogFile(line) {
var logFile = logDirectory + path.sep + appName + ".log";
var augmentedLine = getLogDate() + "\t" + line + os.EOL
fs.appendFile(logFile, augmentedLine, function (err) {
if(err) {
throw "couldn't write to " + logFile;
}
});
}

appLog.log = function (input) {
console.log(setMessageWithFormat(input));
appendToLogFile(input);
}

appLog.info = appLog.log;

appLog.warn = function (input) {
console.warn(setMessageWithFormat(input));
appendToLogFile(input);
}

appLog.error = function (input) {
console.error(setMessageWithFormat(input));
appendToLogFile(input);
}

return appLog;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
Loading