Skip to content

Commit

Permalink
logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Mqxx committed Jun 21, 2024
1 parent c811cda commit ca122a8
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/logging/generator/body_part.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* This function generates the body part with indentation.
*/
export default function bodyPart(
content : string,
indentation : number = 2
): string {
return `${' '.repeat(indentation)}${content}\n`
}
3 changes: 3 additions & 0 deletions src/logging/generator/footer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function footer() : string {
return '\n'
}
5 changes: 5 additions & 0 deletions src/logging/generator/header.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import timestamp from './timestamp.ts';

export default function header(title : string) : string {
return `[${timestamp()}] ${title}:\n`
}
17 changes: 17 additions & 0 deletions src/logging/generator/timestamp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* This function generates the timestamp prefix for logging. Format: `<date>, <time>`
*/
export default function timestamp() : string {
return new Date().toLocaleDateString(
undefined,
{
day: '2-digit',
month: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
}
)
}
12 changes: 12 additions & 0 deletions src/logging/new_request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import bodyPart from './generator/body_part.ts';
import footer from './generator/footer.ts';
import header from './generator/header.ts';

export default function newRequest() : void {
console.log(
header('New Request') +
bodyPart('Type: ') +
bodyPart('Type: ') +
footer()
)
}
15 changes: 15 additions & 0 deletions src/logging/record_updated.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { DNSRecord } from '../record/dns_record.ts';
import timestamp from './generator/timestamp.ts';

export default function recordUpdated(record : DNSRecord) : void {
console.group(`[${timestamp()}] Record Updated`)
console.info(`Type: \t${record.type}`)
console.info(`Content: \t${record.content}`)
console.info(`Name: \t${record.name}`)
record.proxied !== undefined && console.info(`Proxied: \t${record.proxied ? 'yes' : 'no'}`)
record.comment !== undefined && console.info(`Comment: \t"${record.comment}"`)
record.ttl !== undefined && console.info(`TTL: \t${record.ttl === 0 ? 'auto' : record.ttl}`)
console.groupEnd()
}

recordUpdated({type: 'A', content: '1.1.1.1', name: 'example.com'})

0 comments on commit ca122a8

Please sign in to comment.