-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
100 lines (77 loc) · 3.39 KB
/
logger.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Implements logging functionality.
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"time"
)
var log_file, _ = os.OpenFile("./machine.log", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
var log_file_writer = bufio.NewWriter(log_file)
func PrintMessage(direction string, message Message, nodeId string) {
// fmt.Fprintf(log_file_writer, "---------\nPrinting %s message\n%s\n", direction, time.Now())
currentTime := time.Now().Format(time.TimeOnly)
switch message.Kind {
case PING:
fmt.Fprintf(log_file_writer, "[%s] [%s] PING message (%s):\n", currentTime, direction, nodeId)
var messages Messages
err := json.Unmarshal([]byte(message.Data), &messages)
if err != nil {
fmt.Fprintf(log_file_writer, "Failed to unmarshal PING submessages")
return
}
fmt.Fprintf(log_file_writer, "Submessages vvvvv\n")
for _, subMessage := range messages {
PrintMessage(direction, subMessage, nodeId)
}
fmt.Fprintf(log_file_writer, "Submessages ^^^^^\n")
case ACK:
fmt.Fprintf(log_file_writer, "[%s] [%s] ACK message (%s):\n", currentTime, direction, nodeId)
var messages Messages
err := json.Unmarshal([]byte(message.Data), &messages)
if err != nil {
fmt.Fprintf(log_file_writer, "Failed to unmarshal PING submessages")
return
}
fmt.Fprintf(log_file_writer, "Submessages vvvvv\n")
for _, subMessage := range messages {
PrintMessage(direction, subMessage, nodeId)
}
fmt.Fprintf(log_file_writer, "Submessages ^^^^^\n")
case JOIN:
fmt.Fprintf(log_file_writer, "[%s] [%s] [%s] JOIN message with %s\n", currentTime, direction, nodeId, message.Data)
case LEAVE:
fmt.Fprintf(log_file_writer, "[%s] [%s] [%s] LEAVE message with %s\n", currentTime, direction, nodeId, message.Data)
case FAIL:
fmt.Fprintf(log_file_writer, "[%s] [%s] [%s] FAIL message with %s\n", currentTime, direction, nodeId, message.Data)
case HELLO:
fmt.Fprintf(log_file_writer, "[%s] [%s] [%s] HELLO message with %s\n", currentTime, direction, nodeId, message.Data)
case CREATE:
fmt.Fprintf(log_file_writer, "[%s] [%s] [%s] CREATE message with %s\n", currentTime, direction, nodeId, message.Data)
case APPEND:
fmt.Fprintf(log_file_writer, "[%s] [%s] [%s] APPEND message with %s\n", currentTime, direction, nodeId, message.Data)
case CHECK:
fmt.Fprintf(log_file_writer, "[%s] [%s] [%s] CHECK message with %s\n", currentTime, direction, nodeId, message.Data)
case FILES:
fmt.Fprintf(log_file_writer, "[%s] [%s] [%s] FILES message with %s\n", currentTime, direction, nodeId, message.Data)
case GETFILE:
fmt.Fprintf(log_file_writer, "[%s] [%s] [%s] GETFILE message with %s\n", currentTime, direction, nodeId, message.Data)
case MERGE:
fmt.Fprintf(log_file_writer, "[%s] [%s] [%s] MERGE message with %s\n", currentTime, direction, nodeId, message.Data)
case DELETE:
fmt.Fprintf(log_file_writer, "[%s] [%s] [%s] DELETE message with %s\n", currentTime, direction, nodeId, message.Data)
default:
fmt.Fprintf(log_file_writer, "[%s] [%s] ********Trying to print unknown message type: %d**********", currentTime, direction, message.Kind)
}
fmt.Fprintf(log_file_writer, "---------\n")
log_file_writer.Flush()
}
func LogMessage(message string) {
fmt.Fprintf(log_file_writer, "[%s] %s\n", time.Now().Format(time.TimeOnly), message)
log_file_writer.Flush()
}
func LogError(message string) {
fmt.Fprintf(log_file_writer, "[%s] ERROR: %s\n", time.Now().Format(time.TimeOnly), message)
log_file_writer.Flush()
}