aboutsummaryrefslogtreecommitdiff
path: root/cli/cmd/compile.go
diff options
context:
space:
mode:
authorRyo Nihei <nihei.dev@gmail.com>2021-04-08 00:36:22 +0900
committerRyo Nihei <nihei.dev@gmail.com>2021-04-08 00:36:22 +0900
commit91b93662e8ec8a92d763fad74da56b360bba2660 (patch)
tree231e3728a0b682d9376bbe84c7a7a6ce55ff4177 /cli/cmd/compile.go
parentPrint the result of the lex command in JSON format (diff)
downloadtre-91b93662e8ec8a92d763fad74da56b360bba2660.tar.gz
tre-91b93662e8ec8a92d763fad74da56b360bba2660.tar.xz
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.
Diffstat (limited to 'cli/cmd/compile.go')
-rw-r--r--cli/cmd/compile.go27
1 files changed, 25 insertions, 2 deletions
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
}