Skip to content

Commit b160f4e

Browse files
author
fahmi.irfan
committed
feature: use logrus for error & debug
1 parent 63069f0 commit b160f4e

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

grader/main.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
func init() {
1818
log.SetReportCaller(true)
19+
log.SetLevel(log.DebugLevel)
1920
}
2021

2122
func compile(filePath string) (outPath string, err error) {
@@ -24,9 +25,9 @@ func compile(filePath string) (outPath string, err error) {
2425
cmd := exec.Command("g++", args...)
2526
bt, err := cmd.CombinedOutput()
2627
if err != nil {
27-
fmt.Println("compile error: ", string(bt))
28-
return "", err
28+
log.WithField("output", string(bt)).Error(err)
2929
}
30+
3031
return
3132
}
3233

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

5455
func remove(source string) {
5556
if err := os.Remove(source); err != nil {
56-
fmt.Println("error : ", err)
57+
log.Println("error : ", err)
5758
}
5859
}
5960

6061
func grade(source, input, expected string) (result string) {
6162
outPath, err := compile(source)
6263
if err != nil {
63-
fmt.Printf("grade error : %s", err.Error())
64+
log.Error(err)
6465
return ""
6566
}
6667

6768
defer remove(outPath)
6869
out := run(outPath, input)
6970
if expected != out {
70-
fmt.Printf("debug : out=%s expected=%s\n", out, expected)
71+
log.Debugf("out=%s expected=%s\n", out, expected)
7172
result = "NAY"
7273
return
7374
}
@@ -80,7 +81,7 @@ func grade(source, input, expected string) (result string) {
8081
func findFilesInDir(dir string) (map[string]string, error) {
8182
dirs, err := ioutil.ReadDir(dir)
8283
if err != nil {
83-
fmt.Printf("findFilesInDir error: %s", err.Error())
84+
log.Error(err)
8485
return nil, err
8586
}
8687

@@ -133,23 +134,23 @@ func main() {
133134
submissionDir := path.Join(cwd, "submission")
134135
submissions, err := findFilesInDir(submissionDir)
135136
if err != nil {
136-
fmt.Printf("main error : %s", err.Error())
137+
log.Error(err)
137138
return
138139
}
139140

140141
// read input
141142
inputDir := path.Join(cwd, "input")
142143
inputs, err := findFilesInDir(inputDir)
143144
if err != nil {
144-
fmt.Printf("main error : %s", err.Error())
145+
log.Error(err)
145146
return
146147
}
147148

148149
// read output
149150
outputDir := path.Join(cwd, "output")
150151
outputs, err := findFilesInDir(outputDir)
151152
if err != nil {
152-
fmt.Printf("main error : %s", err.Error())
153+
log.Error(err)
153154
return
154155
}
155156

@@ -166,7 +167,7 @@ func main() {
166167
}
167168

168169
if len(outs) != len(ins) {
169-
fmt.Printf("error : unmatch input %d & ouput file %d\n", len(ins), len(outs))
170+
log.Error("error : unmatch input %d & ouput file %d\n", len(ins), len(outs))
170171
return
171172
}
172173

output/example_01

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
3
22
12
3-
12
3+
20

0 commit comments

Comments
 (0)