-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmyLib.js
168 lines (157 loc) · 4.36 KB
/
myLib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var emailjs = require("emailjs");
var mailConfig = require("./etc/email.json");
var child_process = require('child_process');
function sysExec(path, args, cb) {
console.log('appExec', path, args);
let app = child_process.spawn(path, args);
let output = '';
app.on('close', (code) => {
if (code !== 0) {
console.error(`process exited with code ${code}`);
cb(code, output);
} else {
cb(null, output);
}
});
app.on('error', (err) => {
console.error(`Failed to start child process. ${err}`);
});
app.stdout.on('data', (data) => {
output += data;
});
app.stderr.on('data', (data) => {
output += data;
});
};
function mailSend(subject, body, addressTo) {
var server = emailjs.server.connect({
user: mailConfig.username,
password: mailConfig.password,
host: mailConfig.smtpHost,
ssl: true
});
// send the message and get a callback with an error or details of the message that was sent
server.send({
text: body,
from: mailConfig.username,
to: addressTo,
subject: subject
}, function(err, message) {
if (err) {
console.error('ERROR::mailSend - ' + JSON.stringify(err));
}
});
}
function httpGeneric(statusCode, message, response, label) {
if (!response) {
statusCode = statusCode + " <no-http-response>";
} else {
if (statusCode === 200) {
if (!message) {
message = '{}';
} else if (label !== false && !label) {
label = "OK";
}
response.setHeader("Content-Type", "text/json");
} else {
response.setHeader("Content-Type", "text/plain");
}
response.writeHead(statusCode);
response.end(message);
}
if (label) {
console.error(label + " - [" + statusCode + "] " + message);
}
}
function consoleLog(level, label, message, values) {
var logStamp = '>>>> ' + new Date().toLocaleString() + " -";
/*
if (!label) {
//arguments.callee.name
//arguments.callee.caller.name
label = consoleLog.caller;
}
*/
label += ":";
if (values !== null && typeof (values) === 'object') {
values = JSON.stringify(values, null, 4);
}
if (level === "error" || level === "log") {
//console[level](logStamp, level, label, message, values);
console.error(logStamp, level, label, message, values);
} else if (level === 'warning') {
//console.log(logStamp, level, label, message, values);
console.error(logStamp, level, label, message, values);
} else if (level === 'debug') {
console.error('||||', Date.now(), level, label, message, values);
} else {
console.error(logStamp, "undefined log level: " + level, label, message, values);
}
}
function jsonLog(metadata, labels, tags, values, extra) {
// temp handling of old log
if (typeof metadata === 'string') {
var temp = message;
[].unshift.call(arguments, {});
values = temp;
}
if (typeof labels === 'string') {
labels = [labels];
}
var tzoffset = (new Date()).getTimezoneOffset() * 60000; //offset in milliseconds
var timestamp = Date.now();
var localISOTime = (new Date(timestamp - tzoffset)).toISOString();
var debugEntry = JSON.stringify({
logStamp: localISOTime.slice(0, -1),
timestamp: timestamp,
metadata: metadata,
labels: labels,
tags: tags,
values: values,
extra: extra
});
//websocket.emitDebugEvent(debugEntry);
console.log(debugEntry);
if (values !== null && typeof (values) === 'object') {
values = JSON.stringify(values, null, 4);
}
if (extra !== null && typeof (extra) === 'object') {
extra = JSON.stringify(extra, null, 4);
}
var errorLabels = ["warning", "error", "panic", "alert"];
labels.forEach(function (label){
if (errorLabels.indexOf(label) !== -1) {
console.error(">>>>", debugEntry.logStamp, labels, tags, values, metadata, extra);
}
});
}
exports.checkObjectProperties = function (params, required) {
if (typeof(params) !== 'object') {
throw Error('First param must be an object');
} else if (!params) {
return false;
} else {
if (!required) {
return true;
} else if (Array.isArray(required)) {
for (var i in required) {
if (!params[required[i]]) {
return false;
}
}
return true;
} else {
throw Error('Second param must be an array');
}
}
};
exports.httpGeneric = httpGeneric;
exports.mailSend = mailSend;
exports.jsonLog = jsonLog;
exports.consoleLog = consoleLog;
exports.msecDuration = function(hms) {
// expects HH:MM:SS
var a = hms.split(':'); // split it at the colons
return ((+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2])) * 1000;
};
exports.sysExec = sysExec;