Skip to content

Commit

Permalink
feature: use logrus for error & debug
Browse files Browse the repository at this point in the history
  • Loading branch information
fahmi.irfan committed Jun 6, 2020
1 parent 63069f0 commit b160f4e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
21 changes: 11 additions & 10 deletions grader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

func init() {
log.SetReportCaller(true)
log.SetLevel(log.DebugLevel)
}

func compile(filePath string) (outPath string, err error) {
Expand All @@ -24,9 +25,9 @@ func compile(filePath string) (outPath string, err error) {
cmd := exec.Command("g++", args...)
bt, err := cmd.CombinedOutput()
if err != nil {
fmt.Println("compile error: ", string(bt))
return "", err
log.WithField("output", string(bt)).Error(err)
}

return
}

Expand All @@ -53,21 +54,21 @@ func run(source, input string) (out string) {

func remove(source string) {
if err := os.Remove(source); err != nil {
fmt.Println("error : ", err)
log.Println("error : ", err)
}
}

func grade(source, input, expected string) (result string) {
outPath, err := compile(source)
if err != nil {
fmt.Printf("grade error : %s", err.Error())
log.Error(err)
return ""
}

defer remove(outPath)
out := run(outPath, input)
if expected != out {
fmt.Printf("debug : out=%s expected=%s\n", out, expected)
log.Debugf("out=%s expected=%s\n", out, expected)
result = "NAY"
return
}
Expand All @@ -80,7 +81,7 @@ func grade(source, input, expected string) (result string) {
func findFilesInDir(dir string) (map[string]string, error) {
dirs, err := ioutil.ReadDir(dir)
if err != nil {
fmt.Printf("findFilesInDir error: %s", err.Error())
log.Error(err)
return nil, err
}

Expand Down Expand Up @@ -133,23 +134,23 @@ func main() {
submissionDir := path.Join(cwd, "submission")
submissions, err := findFilesInDir(submissionDir)
if err != nil {
fmt.Printf("main error : %s", err.Error())
log.Error(err)
return
}

// read input
inputDir := path.Join(cwd, "input")
inputs, err := findFilesInDir(inputDir)
if err != nil {
fmt.Printf("main error : %s", err.Error())
log.Error(err)
return
}

// read output
outputDir := path.Join(cwd, "output")
outputs, err := findFilesInDir(outputDir)
if err != nil {
fmt.Printf("main error : %s", err.Error())
log.Error(err)
return
}

Expand All @@ -166,7 +167,7 @@ func main() {
}

if len(outs) != len(ins) {
fmt.Printf("error : unmatch input %d & ouput file %d\n", len(ins), len(outs))
log.Error("error : unmatch input %d & ouput file %d\n", len(ins), len(outs))
return
}

Expand Down
2 changes: 1 addition & 1 deletion output/example_01
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
3
12
12
20

0 comments on commit b160f4e

Please sign in to comment.