homegames-common 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +49 -26
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -664,42 +664,60 @@ const msgToString = (msg) => {
|
|
|
664
664
|
return typeof msg === 'object' ? JSON.stringify(msg) : msg;
|
|
665
665
|
};
|
|
666
666
|
|
|
667
|
+
let electronLogger = null;
|
|
668
|
+
|
|
669
|
+
try {
|
|
670
|
+
require.resolve('electron-log');
|
|
671
|
+
electronLogger = require('electron-log');
|
|
672
|
+
} catch (err) {
|
|
673
|
+
console.log('Logger not using electron. Logging to file.');
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
|
|
667
677
|
const log = {
|
|
668
678
|
info: (msg, explanation = null) => {
|
|
669
|
-
|
|
670
|
-
|
|
679
|
+
if (electronLogger) {
|
|
680
|
+
electronLogger.info(msgToString(msg));
|
|
681
|
+
} else {
|
|
682
|
+
const logLevel = getLogLevel();
|
|
683
|
+
const required = getLogLevel('INFO');
|
|
671
684
|
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
685
|
+
if (logLevel < required) {
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
675
688
|
|
|
676
|
-
|
|
689
|
+
const logPath = getConfigValue('LOG_PATH', 'hg_log.txt');
|
|
677
690
|
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
691
|
+
const msgString = `[HOMEGAMES-INFO][${new Date().toTimeString()}] ${msgToString(msg)}${explanation ? ':' + os.EOL + msgToString(explanation) : ''}${os.EOL}${os.EOL}`;
|
|
692
|
+
fs.appendFile(logPath, msgString, (err) => {
|
|
693
|
+
if (err) {
|
|
694
|
+
console.error('failed log');
|
|
695
|
+
console.log(err);
|
|
696
|
+
}
|
|
697
|
+
});
|
|
698
|
+
}
|
|
685
699
|
},
|
|
686
700
|
error: (msg, explanation) => {
|
|
687
|
-
|
|
688
|
-
|
|
701
|
+
if (electronLogger) {
|
|
702
|
+
electronLogger.error(msgToString(msg));
|
|
703
|
+
} else {
|
|
704
|
+
const logLevel = getLogLevel();
|
|
705
|
+
const required = getLogLevel('INFO');
|
|
689
706
|
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
707
|
+
if (logLevel < required) {
|
|
708
|
+
return;
|
|
709
|
+
}
|
|
693
710
|
|
|
694
|
-
|
|
711
|
+
const logPath = getConfigValue('LOG_PATH', 'hg_log.txt');
|
|
695
712
|
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
713
|
+
const msgString = `[HOMEGAMES-ERROR][${new Date().toTimeString()}] ${msgToString(msg)}${explanation ? ':' + os.EOL + msgToString(explanation) : ''}${os.EOL}${os.EOL}`;
|
|
714
|
+
fs.appendFile(logPath, msgString, (err) => {
|
|
715
|
+
if (err) {
|
|
716
|
+
console.error('failed log');
|
|
717
|
+
console.log(err);
|
|
718
|
+
}
|
|
719
|
+
});
|
|
720
|
+
}
|
|
703
721
|
},
|
|
704
722
|
debug: (msg, explanation) => {
|
|
705
723
|
const logLevel = getLogLevel();
|
|
@@ -724,6 +742,11 @@ const log = {
|
|
|
724
742
|
}
|
|
725
743
|
|
|
726
744
|
const getAppDataPath = () => {
|
|
745
|
+
if (!process) {
|
|
746
|
+
// this shouldnt be called if running in browser
|
|
747
|
+
return '';
|
|
748
|
+
}
|
|
749
|
+
|
|
727
750
|
switch (process.platform) {
|
|
728
751
|
case "darwin": {
|
|
729
752
|
return process.env.HOME ? path.join(process.env.HOME, "Library", "Application Support", "homegames") : __dirname;
|