-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
127 lines (114 loc) · 3.56 KB
/
index.js
File metadata and controls
127 lines (114 loc) · 3.56 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
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
import kleur from "kleur";
const originalInfo = console.info;
const originalWarn = console.warn;
const originalError = console.error;
const originalLog = console.log;
function getInitiator(e) {
let initiator = "unknown place";
if (typeof e.stack === "string") {
let isFirst = true;
for (const line of e.stack.split("\n")) {
const matches = line.match(/^\s+at\s+(.*)/);
if (matches) {
if (!isFirst) {
// first line - current function
// second line - caller (what we are looking for)
if (matches[1].split("(").length === 1) {
// inside loop or something
initiator = matches[1].split("(")[0];
} else if (matches[1].split("(").length === 2) {
// not inside loop
initiator = matches[1].split("(")[1];
} else {
initiator = "something went wrong";
}
if (initiator.includes("src")) {
// if src is in path, split at src to support +page.svelte etc with same file names
initiator = `src${initiator.split("src")[1]}`;
} else {
// remove long path info, split at last /
initiator = initiator.split("/")[initiator.split("/").length - 1];
}
return initiator;
}
isFirst = false;
}
}
}
}
console.log = (...args) => {
// https://stackoverflow.com/questions/45395369/how-to-get-console-log-line-numbers-shown-in-nodejs
const e = new Error();
const initiator = getInitiator(e);
const formattedArgs = args.map((arg) => {
if (typeof arg === "string") {
return arg;
} else {
return arg;
}
});
formattedArgs.push(kleur.italic(`(${initiator}`));
if (typeof window !== "undefined") {
originalLog.apply(console, args);
} else {
originalLog.apply(console, formattedArgs);
}
};
console.info = (...args) => {
// https://stackoverflow.com/questions/45395369/how-to-get-console-log-line-numbers-shown-in-nodejs
const e = new Error();
const initiator = getInitiator(e);
const blueText = kleur.blue().bold;
const formattedArgs = args.map((arg) => {
if (typeof arg === "string") {
return blueText(arg);
} else {
return arg;
}
});
formattedArgs.push(kleur.italic(`(${initiator}`));
if (typeof window !== "undefined") {
originalInfo.apply(console, args);
} else {
originalInfo.apply(console, formattedArgs);
}
};
console.warn = (...args) => {
// https://stackoverflow.com/questions/45395369/how-to-get-console-log-line-numbers-shown-in-nodejs
const e = new Error();
const initiator = getInitiator(e);
const yellowText = kleur.yellow().bold;
const formattedArgs = args.map((arg) => {
if (typeof arg === "string") {
return yellowText(arg);
} else {
return arg;
}
});
formattedArgs.push(kleur.italic(`(${initiator}`));
if (typeof window !== "undefined") {
originalWarn.apply(console, args);
} else {
originalWarn.apply(console, formattedArgs);
}
};
console.error = (...args) => {
// https://stackoverflow.com/questions/45395369/how-to-get-console-log-line-numbers-shown-in-nodejs
const e = new Error();
const initiator = getInitiator(e);
const redText = kleur.red().bold;
const formattedArgs = args.map((arg) => {
if (typeof arg === "string") {
return redText(arg);
} else {
return arg;
}
});
formattedArgs.push(kleur.italic(`(${initiator}`));
if (typeof window !== "undefined") {
originalError.apply(console, args);
} else {
originalError.apply(console, formattedArgs);
}
};
export default console;