forked from bpicode/fritzctl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_logs.go
51 lines (43 loc) · 1.04 KB
/
list_logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cmd
import (
"fmt"
"github.com/bpicode/fritzctl/assert"
"github.com/bpicode/fritzctl/fritz"
"github.com/bpicode/fritzctl/logger"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
var blue = color.New(color.Bold, color.FgBlue)
var listLogsCmd = &cobra.Command{
Use: "logs",
Short: "List recent FRITZ!BOX logs",
Long: "List the log statements/events from the FRITZ!Box. Logs may be subject to log rotation by the FRITZ!Box.",
Example: "fritzctl list logs",
RunE: listLogs,
}
func init() {
listCmd.AddCommand(listLogsCmd)
}
func listLogs(cmd *cobra.Command, args []string) error {
c := clientLogin()
f := fritz.Internal(c)
logs, err := f.ListLogs()
assert.NoError(err, "cannot obtain logs:", err)
logger.Success("Obtained log messages:")
printLogs(logs)
return nil
}
func printLogs(logs *fritz.MessageLog) {
for _, m := range logs.Messages {
printLog(&m)
}
}
func printLog(m *fritz.Message) {
text := (*m)[0]
if len(text) >= 17 {
blue.Print(text[:17])
fmt.Println(text[17:])
} else {
fmt.Println(text)
}
}