Skip to content

Commit

Permalink
Separate Module Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshua Thome committed Apr 3, 2018
1 parent 091c77c commit 8bb5b0d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
4 changes: 2 additions & 2 deletions main.ts
Expand Up @@ -71,7 +71,7 @@ function run(srcPath: string, dstPath: string, debug: boolean) {
}

if (mods.some(mod => !mod.result.success)) {
let msgs = compiler.logger.getLogs()
let msgs = compiler.logger.getAll()
for (let msg of msgs) console.log(msg.toString())
console.log('Compilation failed.')
process.exitCode = 1
Expand All @@ -88,7 +88,7 @@ function run(srcPath: string, dstPath: string, debug: boolean) {
fs.writeFileSync(dstPath, Buffer.from(result.buffer as ArrayBuffer))
console.log('Compilation successful.')
} else {
let msgs = compiler.logger.getLogs()
let msgs = compiler.logger.getAll()
for (let msg of msgs) console.log(msg.toString())
console.log('Compilation failed.')
process.exitCode = 1
Expand Down
33 changes: 19 additions & 14 deletions schwa/compiler.ts
Expand Up @@ -94,62 +94,67 @@ export class Compiler {
mod.result.tokens = this.lexer.lex(mod)
if (this.debug) console.timeEnd('lexer')

mod.result.msgs = this.logger.getLogs()
if (this.logger.count(LogType.Error)) return
mod.result.msgs = this.logger.get(mod)

if (this.logger.count(mod, LogType.Error)) return

if (this.debug) console.time('parser')
mod.result.ast = this.parser.parse(mod)
if (this.debug) console.timeEnd('parser')

mod.result.msgs = this.logger.getLogs()
mod.result.msgs = this.logger.get(mod)

if (!mod.result.ast || this.logger.count(LogType.Error)) return
if (!mod.result.ast || this.logger.count(mod, LogType.Error)) return

if (this.debug) console.time('validator')
this.validator.validate(mod)
if (this.debug) console.timeEnd('validator')

mod.result.msgs = this.logger.getLogs()
mod.result.msgs = this.logger.get(mod)

if (this.debug) console.time('preAnalyzer')
this.analyzer.preAnalyze(mod)
if (this.debug) console.timeEnd('preAnalyzer')

mod.result.msgs = this.logger.getLogs()
mod.result.msgs = this.logger.get(mod)
}

protected postLinkCompile(mod: Module) {
if (!mod.result.ast || this.logger.count(LogType.Error)) return
if (!mod.result.ast || this.logger.count(mod, LogType.Error)) return

if (this.debug) console.time('analyzer')
this.analyzer.analyze(mod)
if (this.debug) console.timeEnd('analyzer')

mod.result.msgs = this.logger.getLogs()
mod.result.msgs = this.logger.get(mod)

if (this.logger.count(LogType.Error)) return
if (this.logger.count(mod, LogType.Error)) return

if (this.debug) console.time('formatter')
mod.result.formatted = this.formatter.format(mod)
if (this.debug) console.timeEnd('formatter')

mod.result.msgs = this.logger.getLogs()
mod.result.msgs = this.logger.get(mod)

if (this.logger.count(LogType.Error)) return
if (this.logger.count(mod, LogType.Error)) return

if (this.debug) console.time('generator')
mod.result.buffer = this.generator.generate(mod)
if (this.debug) console.timeEnd('generator')

mod.result.msgs = this.logger.getLogs()
mod.result.msgs = this.logger.get(mod)

if (!mod.result.buffer || this.logger.count(LogType.Error)) return
if (!mod.result.buffer || this.logger.count(mod, LogType.Error)) return

mod.result.success = true
}

protected linkCompile(mod: Module, modules: Module[]) {
this.analyzer.resolveImports(mod, modules.filter(m => m != mod))
if (this.debug) console.time('linker')
this.analyzer.resolveImports(mod, modules.filter(m => this.logger.count(m, LogType.Error) == 0 && m != mod))
if (this.debug) console.timeEnd('linker')

mod.result.msgs = this.logger.get(mod)
}
}

Expand Down
2 changes: 1 addition & 1 deletion schwa/generator.ts
Expand Up @@ -56,7 +56,7 @@ export class Generator {

let writer = new Writer()
writer.write(this.getModule(mod.name))
if (this.logger.count(LogType.Error) > 0) return null
if (this.logger.count(mod, LogType.Error) > 0) return null
return writer.toArrayBuffer()
}

Expand Down
18 changes: 11 additions & 7 deletions schwa/log.ts
Expand Up @@ -19,20 +19,24 @@ export class LogMsg {
export class Logger {
private logs: LogMsg[] = []

public getLogs(): LogMsg[] {
public get(mod: Module): LogMsg[] {
return this.logs.filter(v => v.mod == mod)
}

public getAll(): LogMsg[] {
return this.logs.slice(0)
}

public clear() {
this.logs.length = 0
}

public count(type: LogType) {
let cnt = 0
for (let log of this.logs) {
if (log.type == type) cnt++
}
return cnt
public count(mod: Module, type: LogType) {
return this.logs.filter(v => v.mod == mod && v.type == type).length
}

public countAll(type: LogType) {
return this.logs.filter(v => v.type == type).length
}

public log(msg: LogMsg) {
Expand Down

0 comments on commit 8bb5b0d

Please sign in to comment.