From 91b93662e8ec8a92d763fad74da56b360bba2660 Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Thu, 8 Apr 2021 00:36:22 +0900 Subject: Add logging to compile command compile command writes logs out to the maleeni-compile.log file. When you use compiler.Compile(), you can choose whether the lexer writes logs or not. --- cli/cmd/compile.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'cli/cmd/compile.go') diff --git a/cli/cmd/compile.go b/cli/cmd/compile.go index 23aa120..7815129 100644 --- a/cli/cmd/compile.go +++ b/cli/cmd/compile.go @@ -3,8 +3,10 @@ package cmd import ( "encoding/json" "fmt" + "io" "io/ioutil" "os" + "time" "github.com/nihei9/maleeni/compiler" "github.com/nihei9/maleeni/spec" @@ -22,7 +24,7 @@ func init() { rootCmd.AddCommand(cmd) } -func runCompile(cmd *cobra.Command, args []string) error { +func runCompile(cmd *cobra.Command, args []string) (retErr error) { var lspec *spec.LexSpec { data, err := ioutil.ReadAll(os.Stdin) @@ -35,7 +37,28 @@ func runCompile(cmd *cobra.Command, args []string) error { return err } } - clspec, err := compiler.Compile(lspec) + var w io.Writer + { + f, err := os.OpenFile("maleeni-compile.log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return err + } + defer f.Close() + w = f + } + fmt.Fprintf(w, `maleeni compile starts. +Date time: %v +--- +`, time.Now().Format(time.RFC3339)) + defer func() { + fmt.Fprintf(w, "---\n") + if retErr != nil { + fmt.Fprintf(w, "maleeni compile failed: %v\n", retErr) + } else { + fmt.Fprintf(w, "maleeni compile succeeded.\n") + } + }() + clspec, err := compiler.Compile(lspec, compiler.EnableLogging(w)) if err != nil { return err } -- cgit v1.2.3