-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
53 lines (45 loc) · 1.46 KB
/
Copy pathindex.js
File metadata and controls
53 lines (45 loc) · 1.46 KB
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
// Dependancies
var fs = require('fs');
var os = require('os');
// Variables
var valueSeparator = ', ';
var printConsoleLogs = true;
// Public
exports.logCharacteristicUpdateForDevice = function logCharacteristicUpdateForDevice(device, characteristic, value) {
const now = new Date();
const time = `${now.getHours()}:${now.getMinutes()}:${now.getSeconds()}:${now.getMilliseconds()}`;
const output = `time=${time}${valueSeparator}name=${device}${valueSeparator}characteristic=${characteristic}${valueSeparator}value=${value}\n`;
logOutput(output);
}
exports.setValueSeparator = function setValueSeparator(separator) {
valueSeparator = separator;
}
exports.setPrintConsoleLogs = function setPrintConsoleLogs(print) {
printConsoleLogs = print;
}
// Private
function logOutput(output) {
if (printConsoleLogs == true) {
console.log(`Logging: ${output}`);
}
const now = new Date();
const date = `${now.getDate()}-${now.getMonth()}-${now.getYear()}`;
const path = `${os.homedir()}/.homebridge/logs/${date}characteristic_updates.log`;
fs.exists(path, function(exists) {
if (exists) {
// File exists, append output.
fs.appendFile(path, output, function (error) {
if (error) {
print(console.log(error));
}
});
} else {
// File doesn't exist, create it with the output.
fs.writeFile(path, output, function (error) {
if (error) {
print(console.log(error));
}
});
}
});
}