Skip to content

Commit 6d36c80

Browse files
committed
Add logoptions struct for configurable options for individual log statements
1 parent 5f32981 commit 6d36c80

File tree

5 files changed

+76
-72
lines changed

5 files changed

+76
-72
lines changed

example/ClientServer/ClientServer.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,19 @@ func client(listen, send string) {
2929
Logger := govec.InitGoVector("client", "clientlogfile", govec.GetDefaultConfig())
3030
// sending UDP packet to specified address and port
3131
conn := setupConnection(SERVERPORT, CLIENTPORT)
32+
opts := govec.GetDefaultLogOptions()
3233

3334
for i := 0; i < MESSAGES; i++ {
3435
outgoingMessage := i
35-
outBuf := Logger.PrepareSend("Sending message to server", outgoingMessage)
36+
outBuf := Logger.PrepareSend("Sending message to server", outgoingMessage, opts)
3637
_, errWrite := conn.Write(outBuf)
3738
printErr(errWrite)
3839

3940
var inBuf [512]byte
4041
var incommingMessage int
4142
n, errRead := conn.Read(inBuf[0:])
4243
printErr(errRead)
43-
Logger.UnpackReceive("Received Message from server", inBuf[0:n], &incommingMessage)
44+
Logger.UnpackReceive("Received Message from server", inBuf[0:n], &incommingMessage, opts)
4445
fmt.Printf("GOT BACK : %d\n", incommingMessage)
4546
time.Sleep(1)
4647
}
@@ -62,11 +63,12 @@ func server(listen string) {
6263
n = 1
6364
nMinTwo = 1
6465
nMinTwo = 1
66+
opts := govec.GetDefaultLogOptions()
6567

6668
for i := 0; i < MESSAGES; i++ {
6769
_, addr, err := conn.ReadFrom(buf[0:])
6870
var incommingMessage int
69-
Logger.UnpackReceive("Received Message From Client", buf[0:], &incommingMessage)
71+
Logger.UnpackReceive("Received Message From Client", buf[0:], &incommingMessage, opts)
7072
fmt.Printf("Received %d\n", incommingMessage)
7173
printErr(err)
7274

@@ -86,7 +88,7 @@ func server(listen string) {
8688
break
8789
}
8890

89-
outBuf := Logger.PrepareSend("Replying to client", n)
91+
outBuf := Logger.PrepareSend("Replying to client", n, opts)
9092

9193
conn.WriteTo(outBuf, addr)
9294
time.Sleep(1)

govec/example_test.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ func ExampleGoLog_basic() {
1111
//Initialize logger with default configuration. This can be done in
1212
//a var block to ensure that GoVector is initialized at boot time.
1313
Logger := govec.InitGoVector("MyProcess", "LogFile", govec.GetDefaultConfig())
14+
opts := govec.GetDefaultLogOptions()
1415

1516
//An example message
1617
messagepayload := []byte("samplepayload")
1718

1819
//Prior to sending a message, call PrepareSend on the payload to
1920
//encode the payload and append this processes vector clock to the
2021
//payload
21-
encodedVCpayload := Logger.PrepareSend("Sending Message", messagepayload)
22+
encodedVCpayload := Logger.PrepareSend("Sending Message", messagepayload, opts)
2223

2324
//encodedVCpayload is ready to be written to the network
2425
//ex) conn.Write(encodedVCpayload)
@@ -34,12 +35,12 @@ func ExampleGoLog_basic() {
3435

3536
//Call UnpackReceive on received messages to update local vector
3637
//clock values, and decode the original message.
37-
Logger.UnpackReceive("Received Message from server", encodedVCpayload, &incommingMessage)
38+
Logger.UnpackReceive("Received Message from server", encodedVCpayload, &incommingMessage, opts)
3839
fmt.Printf("Received Message: %s\n", incommingMessage)
3940

4041
//Important local events can be timestamped with vector clocks
4142
//using LogLocalEvent, which also increments the local clock.
42-
Logger.LogLocalEvent("Example Complete")
43+
Logger.LogLocalEvent("Example Complete", opts)
4344

4445
// Output: Received Message: samplepayload
4546
}
@@ -55,12 +56,13 @@ func ExampleGoLog_priority() {
5556
config.PrintOnScreen = true
5657
//Initialize GoVector
5758
Logger := govec.InitGoVector("MyProcess", "PrioritisedLogFile", config)
59+
opts := govec.GetDefaultLogOptions()
5860

59-
Logger.LogLocalEventWithPriority("Debug Priority Event", govec.DEBUG)
60-
Logger.LogLocalEventWithPriority("Info Priority Event", govec.INFO)
61-
Logger.LogLocalEventWithPriority("Warning Priority Event", govec.WARNING)
62-
Logger.LogLocalEventWithPriority("Error Priority Event", govec.ERROR)
63-
Logger.LogLocalEventWithPriority("Fatal Priority Event", govec.FATAL)
61+
Logger.LogLocalEvent("Debug Priority Event", opts.SetPriority(govec.DEBUG))
62+
Logger.LogLocalEvent("Info Priority Event", opts.SetPriority(govec.INFO))
63+
Logger.LogLocalEvent("Warning Priority Event", opts.SetPriority(govec.WARNING))
64+
Logger.LogLocalEvent("Error Priority Event", opts.SetPriority(govec.ERROR))
65+
Logger.LogLocalEvent("Fatal Priority Event", opts.SetPriority(govec.FATAL))
6466

6567
//BUG Output contains timestamps so it cant be tested with *******
6668
//comments
@@ -79,20 +81,21 @@ func ExampleGoLog_tSVizCompatable() {
7981
config.UseTimestamps = true
8082
//Initalize GoVector
8183
Logger := govec.InitGoVector("MyProcess", "LogFile", config)
84+
opts := govec.GetDefaultLogOptions()
8285

8386
//In Sending Process
8487

8588
//Prepare a Message
8689
messagepayload := []byte("samplepayload")
87-
finalsend := Logger.PrepareSend("Sending Message", messagepayload)
90+
finalsend := Logger.PrepareSend("Sending Message", messagepayload, opts)
8891
//In Receiving Process
8992

9093
//receive message
9194
var incommingMessage []byte
92-
Logger.UnpackReceive("Received Message from server", finalsend, &incommingMessage)
95+
Logger.UnpackReceive("Received Message from server", finalsend, &incommingMessage, opts)
9396
fmt.Printf("Received Message: %s\n", incommingMessage)
9497
//Can be called at any point
95-
Logger.LogLocalEvent("Example Complete")
98+
Logger.LogLocalEvent("Example Complete", opts)
9699

97100
// Output: Received Message: samplepayload
98101
}

govec/govec.go

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,28 @@ func GetDefaultConfig() GoLogConfig {
8989
return config
9090
}
9191

92+
//GoLogOptions provides logging parameters for individual logging statements
93+
type GoLogOptions struct {
94+
// The Log priority for this event
95+
Priority LogPriority
96+
}
97+
98+
//GetDefaultLogOptions returns the default GoLogOptions with default values
99+
//for the fields
100+
func GetDefaultLogOptions() GoLogOptions {
101+
o := GoLogOptions{Priority: INFO}
102+
return o
103+
}
104+
105+
//SetPriority returns a new GoLogOptions object with its priority field
106+
//set to Priority. Follows the builder pattern.
107+
// Priority : (GoLogPriority) The Priority that the new GoLogOptions object must have
108+
func (o *GoLogOptions) SetPriority(Priority LogPriority) GoLogOptions {
109+
opts := *o
110+
opts.Priority = Priority
111+
return opts
112+
}
113+
92114
//VClockPayload is the data structure that is actually end on the wire
93115
type VClockPayload struct {
94116
Pid string
@@ -450,32 +472,26 @@ func (gv *GoLog) tickClock() {
450472
gv.currentVC.Tick(gv.pid)
451473
}
452474

453-
//LogLocalEvent Increments current vector timestamp and logs it into
454-
//Log File. * LogMessage (string) : Message to be logged
455-
func (gv *GoLog) LogLocalEvent(Message string) (logSuccess bool) {
456-
return gv.LogLocalEventWithPriority(Message, gv.priority)
457-
}
458-
459475
//LogLocalEventWithPriority implements LogLocalEvent with priority
460476
//levels. If the priority of the logger is lower than or equal to the
461477
//priority of this event then the current vector timestamp is
462478
//incremented and the message is logged it into the Log File. A color
463479
//coded string is also printed on the console.
464480
//* LogMessage (string) : Message to be logged
465481
//* Priority (LogPriority) : Priority at which the message is to be logged
466-
func (gv *GoLog) LogLocalEventWithPriority(LogMessage string, Priority LogPriority) (logSuccess bool) {
482+
func (gv *GoLog) LogLocalEvent(LogMessage string, opts GoLogOptions) (logSuccess bool) {
467483
logSuccess = true
468484
gv.mutex.Lock()
469-
if Priority >= gv.priority {
470-
prefix := prefixLookup[Priority]
485+
if opts.Priority >= gv.priority {
486+
prefix := prefixLookup[opts.Priority]
471487
gv.tickClock()
472-
logSuccess = gv.logWriteWrapper(prefix+LogMessage, "Something went Wrong, Could not Log LocalEvent!", Priority)
488+
logSuccess = gv.logWriteWrapper(prefix+LogMessage, "Something went Wrong, Could not Log LocalEvent!", opts.Priority)
473489
}
474490
gv.mutex.Unlock()
475491
return
476492
}
477493

478-
//PrepareSendWithPriority is meant to be used immediately before sending.
494+
//PrepareSend is meant to be used immediately before sending.
479495
//LogMessage will be logged along with the time of the send
480496
//buf is encode-able data (structure or basic type)
481497
//Returned is an encoded byte array with logging information.
@@ -484,14 +500,14 @@ func (gv *GoLog) LogLocalEventWithPriority(LogMessage string, Priority LogPriori
484500
//it should Update the Vector Clock for its own process, package with
485501
//the clock using gob support and return the new byte array that should
486502
//be sent onwards using the Send Command
487-
func (gv *GoLog) PrepareSendWithPriority(mesg string, buf interface{}, Priority LogPriority) (encodedBytes []byte) {
503+
func (gv *GoLog) PrepareSend(mesg string, buf interface{}, opts GoLogOptions) (encodedBytes []byte) {
488504

489505
//Converting Vector Clock from Bytes and Updating the gv clock
490506
gv.mutex.Lock()
491-
if Priority >= gv.priority {
507+
if opts.Priority >= gv.priority {
492508
gv.tickClock()
493509

494-
gv.logWriteWrapper(mesg, "Something went wrong, could not log prepare send", Priority)
510+
gv.logWriteWrapper(mesg, "Something went wrong, could not log prepare send", opts.Priority)
495511

496512
d := VClockPayload{Pid: gv.pid, VcMap: gv.currentVC.GetMap(), Payload: buf}
497513

@@ -508,19 +524,6 @@ func (gv *GoLog) PrepareSendWithPriority(mesg string, buf interface{}, Priority
508524
return
509525
}
510526

511-
//PrepareSend is meant to be used immediately before sending.
512-
//LogMessage will be logged along with the time of the send
513-
//buf is encode-able data (structure or basic type)
514-
//Returned is an encoded byte array with logging information.
515-
//
516-
//This function is meant to be called before sending a packet. Usually,
517-
//it should Update the Vector Clock for its own process, package with
518-
//the clock using gob support and return the new byte array that should
519-
//be sent onwards using the Send Command
520-
func (gv *GoLog) PrepareSend(mesg string, buf interface{}) []byte {
521-
return gv.PrepareSendWithPriority(mesg, buf, gv.priority)
522-
}
523-
524527
func (gv *GoLog) mergeIncomingClock(mesg string, e VClockPayload, Priority LogPriority) {
525528

526529
// First, tick the local clock
@@ -530,19 +533,19 @@ func (gv *GoLog) mergeIncomingClock(mesg string, e VClockPayload, Priority LogPr
530533
gv.logWriteWrapper(mesg, "Something went Wrong, Could not Log!", Priority)
531534
}
532535

533-
//UnpackReceiveWithPriority is used to unmarshall network data into local structures.
536+
//UnpackReceive is used to unmarshall network data into local structures.
534537
//LogMessage will be logged along with the vector time the receive happened
535538
//buf is the network data, previously packaged by PrepareSend unpack is
536539
//a pointer to a structure, the same as was packed by PrepareSend
537540
//
538541
//This function is meant to be called immediately after receiving
539542
//a packet. It unpacks the data by the program, the vector clock. It
540543
//updates vector clock and logs it. and returns the user data
541-
func (gv *GoLog) UnpackReceiveWithPriority(mesg string, buf []byte, unpack interface{}, Priority LogPriority) {
544+
func (gv *GoLog) UnpackReceive(mesg string, buf []byte, unpack interface{}, opts GoLogOptions) {
542545

543546
gv.mutex.Lock()
544547

545-
if Priority >= gv.priority {
548+
if opts.Priority >= gv.priority {
546549
e := VClockPayload{}
547550
e.Payload = unpack
548551

@@ -553,22 +556,9 @@ func (gv *GoLog) UnpackReceiveWithPriority(mesg string, buf []byte, unpack inter
553556
}
554557

555558
// Increment and merge the incoming clock
556-
gv.mergeIncomingClock(mesg, e, Priority)
559+
gv.mergeIncomingClock(mesg, e, opts.Priority)
557560
}
558561
gv.mutex.Unlock()
559562

560563
}
561564

562-
/*
563-
UnpackReceive is used to unmarshall network data into local structures.
564-
LogMessage will be logged along with the vector time the receive happened
565-
buf is the network data, previously packaged by PrepareSend unpack is
566-
a pointer to a structure, the same as was packed by PrepareSend
567-
568-
This function is meant to be called immediately after receiving
569-
a packet. It unpacks the data by the program, the vector clock. It
570-
updates vector clock and logs it. and returns the user data
571-
*/
572-
func (gv *GoLog) UnpackReceive(mesg string, buf []byte, unpack interface{}) {
573-
gv.UnpackReceiveWithPriority(mesg, buf, unpack, gv.priority)
574-
}

govec/govec_test.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ func TestBasicInit(t *testing.T) {
2626
func TestLogLocal(t *testing.T) {
2727

2828
gv := InitGoVector(TestPID, "TestLogFile", GetDefaultConfig())
29-
gv.LogLocalEvent("TestMessage1")
29+
opts := GetDefaultLogOptions()
30+
gv.LogLocalEvent("TestMessage1", opts)
3031

3132
vc := gv.GetCurrentVC()
3233
n, _ := vc.FindTicks(TestPID)
@@ -38,15 +39,16 @@ func TestLogLocal(t *testing.T) {
3839
func TestSendAndUnpackInt(t *testing.T) {
3940

4041
gv := InitGoVector(TestPID, "TestLogFile", GetDefaultConfig())
41-
packed := gv.PrepareSend("TestMessage1", 1337)
42+
opts := GetDefaultLogOptions()
43+
packed := gv.PrepareSend("TestMessage1", 1337, opts)
4244

4345
vc := gv.GetCurrentVC()
4446
n, _ := vc.FindTicks(TestPID)
4547

4648
AssertEquals(t, uint64(2), n, "PrepareSend: Clock value incremented")
4749

4850
var response int
49-
gv.UnpackReceive("TestMessage2", packed, &response)
51+
gv.UnpackReceive("TestMessage2", packed, &response, opts)
5052

5153
vc = gv.GetCurrentVC()
5254
n, _ = vc.FindTicks(TestPID)
@@ -59,15 +61,16 @@ func TestSendAndUnpackInt(t *testing.T) {
5961
func TestSendAndUnpackStrings(t *testing.T) {
6062

6163
gv := InitGoVector(TestPID, "TestLogFile", GetDefaultConfig())
62-
packed := gv.PrepareSend("TestMessage1", "DistClocks!")
64+
opts := GetDefaultLogOptions()
65+
packed := gv.PrepareSend("TestMessage1", "DistClocks!", opts)
6366

6467
vc := gv.GetCurrentVC()
6568
n, _ := vc.FindTicks(TestPID)
6669

6770
AssertEquals(t, uint64(2), n, "PrepareSend: Clock value incremented. ")
6871

6972
var response string
70-
gv.UnpackReceive("TestMessage2", packed, &response)
73+
gv.UnpackReceive("TestMessage2", packed, &response, opts)
7174

7275
vc = gv.GetCurrentVC()
7376
n, _ = vc.FindTicks(TestPID)
@@ -80,29 +83,31 @@ func TestSendAndUnpackStrings(t *testing.T) {
8083
func BenchmarkPrepare(b *testing.B) {
8184

8285
gv := InitGoVector(TestPID, "TestLogFile", GetDefaultConfig())
86+
opts := GetDefaultLogOptions()
8387

8488
var packed []byte
8589

8690
for i := 0; i < b.N; i++ {
87-
packed = gv.PrepareSend("TestMessage1", 1337)
91+
packed = gv.PrepareSend("TestMessage1", 1337, opts)
8892
}
8993

9094
var response int
91-
gv.UnpackReceive("TestMessage2", packed, &response)
95+
gv.UnpackReceive("TestMessage2", packed, &response, opts)
9296

9397
}
9498

9599
func BenchmarkUnpack(b *testing.B) {
96100

97101
gv := InitGoVector(TestPID, "TestLogFile", GetDefaultConfig())
102+
opts := GetDefaultLogOptions()
98103

99104
var packed []byte
100-
packed = gv.PrepareSend("TestMessage1", 1337)
105+
packed = gv.PrepareSend("TestMessage1", 1337, opts)
101106

102107
var response int
103108

104109
for i := 0; i < b.N; i++ {
105-
gv.UnpackReceive("TestMessage2", packed, &response)
110+
gv.UnpackReceive("TestMessage2", packed, &response, opts)
106111
}
107112

108113
}

govec/vrpc/vrpc.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ func (c *RPCClientCodec) WriteRequest(req *rpc.Request, param interface{}) (err
7878
if err = c.Enc.Encode(req); err != nil {
7979
return
8080
}
81-
buf := c.Logger.PrepareSend("Making RPC call", param)
81+
opts := govec.GetDefaultLogOptions()
82+
buf := c.Logger.PrepareSend("Making RPC call", param, opts)
8283
if err = c.Enc.Encode(buf); err != nil {
8384
return
8485
}
@@ -98,7 +99,8 @@ func (c *RPCClientCodec) ReadResponseBody(body interface{}) (err error) {
9899
if err = c.Dec.Decode(&buf); err != nil {
99100
return
100101
}
101-
c.Logger.UnpackReceive("Received RPC Call response from server", buf, body)
102+
opts := govec.GetDefaultLogOptions()
103+
c.Logger.UnpackReceive("Received RPC Call response from server", buf, body, opts)
102104
return nil
103105
}
104106

@@ -131,15 +133,17 @@ func (c *RPCServerCodec) ReadRequestBody(body interface{}) (err error) {
131133
if err = c.Dec.Decode(&buf); err != nil {
132134
return
133135
}
134-
c.Logger.UnpackReceive("Received RPC request", buf, body)
136+
opts := govec.GetDefaultLogOptions()
137+
c.Logger.UnpackReceive("Received RPC request", buf, body, opts)
135138
return nil
136139
}
137140

138141
//WriteResponse sends an rpc response, and it's associated result back
139142
//to the client
140143
func (c *RPCServerCodec) WriteResponse(r *rpc.Response, body interface{}) (err error) {
141144
Encode(c, r)
142-
buf := c.Logger.PrepareSend("Sending response to RPC request", body)
145+
opts := govec.GetDefaultLogOptions()
146+
buf := c.Logger.PrepareSend("Sending response to RPC request", body, opts)
143147
Encode(c, buf)
144148
return c.EncBuf.Flush()
145149
}

0 commit comments

Comments
 (0)