dedis / onet

Overlay Network for distributed protocols
GNU Lesser General Public License v3.0
51 stars 28 forks source link

unable to log.OutputToBuf early enough #588

Open tharvik opened 5 years ago

tharvik commented 5 years ago

I want to output something to stdout without having the logger writing it on. The issue is that it's already writing quite early on (via the func init construct).

package main

import (
    _ "go.dedis.ch/cothority/v3/skipchain"
    onet_log "go.dedis.ch/onet/v3/log"
)

func init() {
    onet_log.OutputToBuf() // already useless
}

func main() {
    // nothing
}

gives with DEBUG_LVL=4

4 : protocol.go:111 (v3.(*protocolStorage).Register) - Registered Broadcast to 59af9e1e-79cc-301b-b2b4-bbfe989bac36
4 : protocol.go:111 (v3.(*protocolStorage).Register) - Registered blsCoSiProtoDefault to 522a1bad-ed6f-3326-bbef-36fe2a859cf1
4 : protocol.go:111 (v3.(*protocolStorage).Register) - Registered blsSubCoSiProtoDefault to 9941c053-dc08-359f-8a68-d00fee072378
4 : protocol.go:111 (v3.(*protocolStorage).Register) - Registered scExtendRoster to 324d4101-3ab7-3672-a985-43b9ec56a82e
4 : protocol.go:111 (v3.(*protocolStorage).Register) - Registered scGetBlocks to 15533989-b358-3b71-b454-4558037237a3

A working way to do it (but ugly), is to create a dedicated pkg just containing the OutputToBuf call inside an init, then importing it before anything else.

A better way to fix it would be to avoid init everything, especially if the func called have side effects. That's the main issue IMO, that init (and its combo, global variables) is used as a way to avoid having a dedicated struct to contain the state of a given part.

ineiti commented 5 years ago

That will be difficult to achieve given the compile-time plugin structure of protocols and services.

If I understand your proposition correctly, you would like to replace the automatic registration of protocols and services with something more user-controlled.

Can you be more specific what you're actually trying to achieve?

tharvik commented 5 years ago

If I understand your proposition correctly, you would like to replace the automatic registration of protocols and services with something more user-controlled.

In the long run, that would be best; but maybe, it's reaching larger than the scope of this issue.

Can you be more specific what you're actually trying to achieve?

I'm writing a small CLI which generates a Roster configuration, but outputing this config to stdout gets it gets mangled with the logging from onet.

ineiti commented 5 years ago

So you either write it to a file or put DEBUG_LVL=0 when calling it...

tharvik commented 5 years ago

So you either write it to a file or put DEBUG_LVL=0 when calling it...

I can work around it, that's not the issue. The issue is that I don't have a normal/obvious way to control os.Stdout when using onet, it get mixed with intended output and library output. IMO, it's akin to having a library calling os.Exit when receiving a malformed message.