Skip to content

Commit

Permalink
implementing silenced log #40 #43
Browse files Browse the repository at this point in the history
  • Loading branch information
Ca5e committed Mar 9, 2024
1 parent e3a3f1b commit 02beb3d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 25 deletions.
26 changes: 13 additions & 13 deletions hrgt/src/Resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,25 +167,25 @@ export class Resolver {
for (const converter of converters) {
try {
const hrgEntry = converter.convert(profile)
if (hrgEntry == converter.getBlank()) {
throw new Error(
`Conversion of entry '${entry.termid}' using ${converter.name} did not fill in any expression`
)
} else {
replacement += hrgEntry
}
replacement += hrgEntry
} catch (err) {
log.warn(`\t\t${err.message}`)
log.settings.minLevel = undefined // reset the log level set by {{log level="silent"}}
try {
const error = Converter.instances.find((i) => i.n === -1)
profile.err.message = err.message
if (error) {
replacement = error.convert(profile)
}
} catch (err) {
log.warn(`\t\t${err.message}`)
}
}
log.settings.minLevel = undefined // reset the log level set by {{log level="silent"}}
}
}
} else {
// If the MRG instance is not found, create the replacement using the error converter
const error = Converter.instances.find((i) => i.n === -1)
profile.err.message = `Something went wrong while retrieving the MRG file '${mrgref.hrg}'`
if (error) {
replacement = error.convert(profile)
}
report.onNotExistError(new Error(`Something went wrong while retrieving the MRG file '${mrgref.hrg}'`))
}

// Only execute the replacement steps if the 'replacement' string is not empty
Expand Down
5 changes: 2 additions & 3 deletions mrgt/src/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ export class Generator {
for (const instance of TuCBuilder.instances) {
if (instance.tuc.cText) {
log.info(`\x1b[1;37mProcessing synonymOf entries...\x1b[0m`)
// temporarily set the log level to 5 to suppress the output of the MRG files
log.settings.minLevel = 5
log.settings.minLevel = 5 // temporarily set the log level to 5 to suppress the output of the MRG files
this.handleSynonymOf(instance)
}
}
log.settings.minLevel = 1
log.settings.minLevel = undefined // reset the log level
}
}

Expand Down
18 changes: 14 additions & 4 deletions trrt/src/Resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,13 @@ export class Resolver {
} catch (err) {
error = err // store the error so it can be thrown after the replacement steps with converter[error]
profile.err.cause = err.message
const converter = Converter.instances.find((i) => i.n === -1) // get the Converter instance where n = -1 (converter[error] option)
if (converter) {
replacement = converter.convert(profile)
try {
const converter = Converter.instances.find((i) => i.n === -1) // get the Converter instance where n = -1 (converter[error] option)
if (converter) {
replacement = converter.convert(profile)
}
} catch (err) {
error.message = `${error.message}, and: ${err.message}`
}
}

Expand Down Expand Up @@ -193,7 +197,13 @@ export class Resolver {
if (interpretation !== reference) {
reference = `${reference}' > '${interpretation}`
}
const message = `Term ref '${match[0]}' > '${reference}', ${err}`

let message = `Term ref '${match[0]}' > '${reference}', ${err}`
// If the log level is set to 5 by {{log level="silent"}}, do not log the message
if (log.settings.minLevel === 5) {
message = undefined
log.settings.minLevel = undefined // reset the log level
}
report.errors.push({
type: "TERM HELP",
line,
Expand Down
7 changes: 7 additions & 0 deletions utils/src/classes/Handlebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@ import { ifValue } from "../helpers/ifValue.js"
import { localize } from "../helpers/localize.js"
import { noRefs } from "../helpers/noRefs.js"
import { regularize } from "../helpers/regularize.js"
import { log } from "../helpers/log.js"

Handlebars.registerHelper("capFirst", capFirst)
Handlebars.registerHelper("ifValue", ifValue)
Handlebars.registerHelper("localize", localize)
Handlebars.registerHelper("noRefs", noRefs)
Handlebars.registerHelper("regularize", regularize)
Handlebars.registerHelper("log", log)

// Handlebars.log = function (level: number, ...message: string[]) {
// console.log(`Level: ${level}.`)
// console.log(`Message: ${message}`)
// }

export { Handlebars }

Expand Down
2 changes: 1 addition & 1 deletion utils/src/classes/MRG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export function getEntry(entries: Entry[], origin: string, term: string, type: s
if (matches.length === 1) {
return matches[0]
} else if (matches.length === 0) {
throw new Error(`could not be matched with an MRG entry in '${origin}`)
throw new Error(`could not be matched with an MRG entry in '${origin}'`)
} else if (matches.length > 1) {
const matchingTermIds = matches.map((entry) => entry.termid).join("', '")
throw new Error(`has multiple matching MRG entries in '${origin}'. Matching termids: '${matchingTermIds}'`)
Expand Down
11 changes: 7 additions & 4 deletions utils/src/classes/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,20 @@ class Report {

console.log(`\x1B[0m`)
console.log(` Resolution Report:`)
console.log(`\t\x1b[0mFiles modified: ${this.files.length}`)
console.log(`\t\x1b[0mTerms converted: ${this.terms.length}`)
console.log(`\t\x1b[0mTerm errors: ${termErrors.length}`)
console.log(`\t\x1b[0mMain errors: ${mainErrors.length}`)
console.log(`\t\x1b[0mFiles modified: ${this.files.length}`)
console.log(`\t\x1b[0mTerms converted: ${this.terms.length}`)
console.log(`\t\x1b[0mTerm errors \x1b[3m(silenced)\x1b[0m: ${termErrors.length}`)
console.log(`\t\x1b[0mMain error occurences: ${mainErrors.length}`)

if (termErrors.length > 0) {
console.log(`\n \x1b[1;37mTerm Errors:\x1b[0m`)

const groupedTermErrors = new Map<string, Array<TermError>>()

for (const err of termErrors) {
if (!err.cause) {
continue
}
const key = err.cause.toString()

if (groupedTermErrors.has(key)) {
Expand Down
31 changes: 31 additions & 0 deletions utils/src/helpers/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { type HelperOptions } from "handlebars"
import { log as logger } from "../classes/Report.js"

export function log(...args: unknown[]) {
const options: Partial<HelperOptions> = args[args.length - 1]
args.pop()
let level = undefined
if (options.hash.level != null) {
level = options.hash.level
}
logger.settings.minLevel = undefined // reset the log level

if (level === "silent") {
// temporarily up the log level to suppress the output of conversion related warnings
logger.settings.minLevel = 5
} else if (level === "silly") {
logger.silly(...args)
} else if (level === "trace") {
logger.trace(...args)
} else if (level === "debug") {
logger.debug(...args)
} else if (level === "info") {
logger.info(...args)
} else if (level === "error") {
logger.error(...args)
} else if (level === "fatal") {
logger.fatal(...args)
} else {
logger.warn(...args)
}
}

0 comments on commit 02beb3d

Please sign in to comment.