This repository has been archived by the owner on Jul 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
177 lines (154 loc) · 4.2 KB
/
main.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"bufio"
"flag"
"io"
"log"
"net"
"net/http"
"net/rpc"
"os"
"strconv"
"sync"
"time"
)
type simplogConfig struct {
logfile string
isListening bool
listenHost string
listenPort int
isSending bool
sendHost string
sendPort int
addTimestamp bool
nodeName string
period time.Duration
debug bool
}
type SendArgs struct {
Content string
}
var logChan chan string
type RpcEndpoint int
var rpcClient *rpc.Client
var wg sync.WaitGroup
var config simplogConfig
func main() {
set_flags()
debuglog("Config: %#v", config)
logChan = make(chan string, 10000)
go startLogWriter(logChan, &wg)
startListener()
startSender()
processStdin(logChan, &wg)
debuglog("Blocked on group waiting")
wg.Wait()
}
func debuglog(message string, args ...interface{}) {
if config.debug {
log.Printf(message+"\n", args...)
}
}
func startSender() {
if !config.isSending {
return
}
debuglog("Starting RPC client")
var err error
rpcClient, err = rpc.DialHTTP("tcp", config.sendHost+":"+strconv.Itoa(config.sendPort))
if err != nil {
log.Fatal("dialing:", err)
}
}
func startListener() {
if !config.isListening {
return
}
debuglog("Starting RPC server")
rpcEndpoint := new(RpcEndpoint)
rpc.Register(rpcEndpoint)
rpc.HandleHTTP()
l, err := net.Listen("tcp", config.listenHost+":"+strconv.Itoa(config.listenPort))
if err != nil {
log.Fatal("listen error:", err)
}
go http.Serve(l, nil)
wg.Add(1)
}
func (t *RpcEndpoint) Send(args *SendArgs, reply *int) error {
debuglog("Received new RPC Send call with args: %#v\n Sending new content to the log writer", args)
wg.Add(1)
logChan <- makeLogString(args.Content)
*reply = 0
return nil
}
func set_flags() {
flag.BoolVar(&config.isListening, "listen", false, "Description")
flag.StringVar(&config.listenHost, "listen-host", "localhost", "Description")
flag.IntVar(&config.listenPort, "listen-port", 22016, "Description")
flag.BoolVar(&config.isSending, "send", false, "Description")
flag.StringVar(&config.sendHost, "send-host", "localhost", "Description")
flag.IntVar(&config.sendPort, "send-port", 22016, "Description")
flag.BoolVar(&config.addTimestamp, "timestamp", true, "Description")
flag.StringVar(&config.nodeName, "name", "", "Description")
flag.StringVar(&config.logfile, "logfile", "./simplog.log", "Description")
flag.DurationVar(&config.period, "period", time.Duration(24*365*42)*time.Hour, "Description 1d 1w")
flag.BoolVar(&config.debug, "debug", false, "Set the flag to enable internal logging")
flag.Parse()
}
func startLogWriter(records chan string, wg *sync.WaitGroup) {
debuglog("Open logfile: %#v", config.logfile)
logFile, err := os.OpenFile(config.logfile, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0664)
if err != nil {
panic("Can't open the log file \"" + config.logfile + "\". " + err.Error())
}
for {
debuglog("Wait for new content for writing to logfile")
logRecord := <-records
debuglog("Received new content for writing to logfile: %#v", logRecord)
if config.isSending {
args := &SendArgs{logRecord}
var reply int
debuglog("Start RPC call with args: %#v", args)
err := rpcClient.Call("RpcEndpoint.Send", args, &reply)
if err != nil {
log.Fatal("Send log record error: ", err)
}
debuglog("RPC call done successfuly", args)
wg.Done()
continue
}
debuglog("Write new content to the logFile")
_, err := logFile.WriteString(logRecord)
if err != nil {
panic("Can't write to the log file. " + err.Error())
}
wg.Done()
}
}
func processStdin(logChan chan string, wg *sync.WaitGroup) {
debuglog("Start processing stdin")
inReader := bufio.NewReader(os.Stdin)
for {
line, err := inReader.ReadString('\n')
debuglog("Received new line from stdin: %#v", line)
if err == io.EOF {
debuglog("stdin EOF")
break
}
debuglog("Sending the new line to the log writer")
wg.Add(1)
logChan <- makeLogString(line)
}
}
func makeLogString(text string) string {
logString := text
if config.nodeName != "" {
logString = config.nodeName + ": " + logString
}
if config.addTimestamp && !config.isSending {
logString = time.Now().UTC().String() + " - " + logString
}
debuglog("Making the log string: from %#v to %#v", text, logString)
return logString
}