Skip to content
Snippets Groups Projects
Commit 2f658496 authored by Benjamin Wenger's avatar Benjamin Wenger
Browse files

Merge branch 'logWriter' into 'release'

Log writer

See merge request !581
parents 82c07316 f4d650d3
No related branches found
No related tags found
No related merge requests found
......@@ -96,3 +96,17 @@ func newRoundListUnregister(rounds []id.Round, ec []*dataStructures.EventCallbac
type ClientError interface {
Report(source, message, trace string)
}
type LogWriter interface{
Log(string)
}
type writerAdapter struct{
lw LogWriter
}
func (wa *writerAdapter)Write(p []byte) (n int, err error){
wa.lw.Log(string(p))
return len(p), nil
}
......@@ -19,18 +19,17 @@ import (
"gitlab.com/elixxir/comms/mixmessages"
"gitlab.com/elixxir/primitives/states"
"gitlab.com/xx_network/primitives/id"
"sync/atomic"
"sync"
"time"
)
var extantClient *uint32
var extantClient bool
var loginMux sync.Mutex
// sets the log level
func init() {
jww.SetLogThreshold(jww.LevelInfo)
jww.SetStdoutThreshold(jww.LevelInfo)
extant := uint32(0)
extantClient = &extant
}
// BindingsClient wraps the api.Client, implementing additional functions
......@@ -81,11 +80,14 @@ func NewPrecannedClient(precannedID int, network, storageDir string, password []
// Login does not block on network connection, and instead loads and
// starts subprocesses to perform network operations.
func Login(storageDir string, password []byte, parameters string) (*Client, error) {
// check if a client is already logged in, refuse to login if one is
if atomic.CompareAndSwapUint32(extantClient,0,1){
return nil, errors.New("Cannot login when a session already exists")
}
loginMux.Lock()
defer loginMux.Unlock()
if extantClient{
return nil, errors.New("cannot login when another session " +
"already exists")
}
// check if a client is already logged in, refuse to login if one is
p, err := params.GetNetworkParameters(parameters)
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to login: %+v", err))
......@@ -95,6 +97,7 @@ func Login(storageDir string, password []byte, parameters string) (*Client, erro
if err != nil {
return nil, errors.New(fmt.Sprintf("Failed to login: %+v", err))
}
extantClient=true
return &Client{api: *client}, nil
}
......@@ -137,6 +140,11 @@ func LogLevel(level int) error {
return nil
}
//RegisterLogWriter registers a callback on which logs are written.
func RegisterLogWriter(writer LogWriter){
jww.SetLogOutput(&writerAdapter{lw:writer})
}
//Unmarshals a marshaled contact object, returns an error if it fails
func UnmarshalContact(b []byte) (*Contact, error) {
c, err := contact.Unmarshal(b)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment