-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.ts
45 lines (38 loc) · 1.6 KB
/
log.ts
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
import pc from 'picocolors'
import * as fs from 'fs'
import { Time, PadWithZeros } from './functions.js'
/**
* A function for logging a specific message.
*
* @param message - The message to be logged.
* @param type - The type of log.
*/
export const Log = (message: string, type: 'i' | 'e' | 's' | 'w' | 'd' = 'i') => {
const time = Time()
const YMD = `${time.year}-${PadWithZeros(time.month, 2)}-${PadWithZeros(time.date, 2)}`
const HMS = `${PadWithZeros(time.hours, 2)}:${PadWithZeros(time.minutes, 2)}:${PadWithZeros(time.seconds, 2)}`
let title = ''
if (type === 'i') title = pc.blue('QuackJS')
if (type === 'e') title = pc.red('QuackJS')
if (type === 's') title = pc.green('QuackJS')
if (type === 'w') title = pc.yellow('QuackJS')
if (type === 'd') title = pc.magenta('QuackJS')
console.log(pc.gray(`(${YMD} ${HMS}) (CODE: ${type.toUpperCase()}) `) + title + pc.gray(' » ') + pc.white(message))
if (fs.existsSync(`./logs/console/`)) fs.appendFileSync(`./logs/console/${YMD}.log`, `${`(${YMD} ${HMS})`} (CODE: ${type.toUpperCase()}) QuackJS » ${message}\n`)
}
/**
* A function for debugging Functions or Objects
*
* @param obj - The object/function to be tested.
* @param name - The name of the debug call.
*/
export const Debug = (obj: Function | Object, name: string = 'none') => {
if (typeof obj === 'function') {
const timeStart = new Date()
const returnFromObj = obj()
const timeEnd = new Date()
Log(name + ' ' + returnFromObj + ' ' + (timeEnd.getTime() - timeStart.getTime()) + 'ms', 'd')
return
}
Log(name + ' ' + obj.constructor.name + ' ' + (obj === null || obj === undefined), 'd')
}