Skip to content

Commit

Permalink
[all] rename vars watchloglevel -> level; fix in unwatch command.
Browse files Browse the repository at this point in the history
  • Loading branch information
EskoDijk committed Sep 24, 2023
1 parent 0502bd2 commit 3e5bfd5
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 43 deletions.
16 changes: 8 additions & 8 deletions cli/CmdRunner.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,19 +730,19 @@ func (rt *CmdRunner) executeRadioModel(cc *CommandContext, cmd *RadioModelCmd) {

func (rt *CmdRunner) executeLogLevel(cc *CommandContext, cmd *LogLevelCmd) {
if cmd.Level == "" {
cc.outputf("%v\n", logger.GetWatchLogLevelString(rt.sim.GetLogLevel()))
cc.outputf("%v\n", logger.GetLevelString(rt.sim.GetLogLevel()))
} else {
rt.sim.SetLogLevel(logger.ParseWatchLogLevel(cmd.Level))
rt.sim.SetLogLevel(logger.ParseLevelString(cmd.Level))
}
}

func (rt *CmdRunner) executeWatch(cc *CommandContext, cmd *WatchCmd) {
rt.postAsyncWait(cc, func(sim *simulation.Simulation) {
watchLogLevelStr := ""
var watchLogLevel = logger.DefaultLevel
levelStr := ""
var level = logger.DefaultLevel
if len(cmd.Level) > 0 {
watchLogLevelStr = cmd.Level
watchLogLevel = logger.ParseWatchLogLevel(watchLogLevelStr)
levelStr = cmd.Level
level = logger.ParseLevelString(levelStr)
}
nodesToWatch := cmd.Nodes

Expand Down Expand Up @@ -774,7 +774,7 @@ func (rt *CmdRunner) executeWatch(cc *CommandContext, cmd *WatchCmd) {
// Do nothing here. Will iterate over nodes below.
} else if len(cmd.Nodes) == 0 && len(cmd.All) == 0 && len(cmd.Default) == 0 && len(cmd.Level) > 0 {
// variant: 'watch <level>'
// Do nothing here. <level> was processed above as 'watchLogLevel'.
// Do nothing here. <level> was processed above already.
} else {
cc.errorf("watch: unsupported combination of command options")
return
Expand All @@ -786,7 +786,7 @@ func (rt *CmdRunner) executeWatch(cc *CommandContext, cmd *WatchCmd) {
cc.errorf("node %d not found", sel.Id)
continue
}
sim.Dispatcher().WatchNode(node.Id, watchLogLevel)
sim.Dispatcher().WatchNode(node.Id, level)
}
})
}
Expand Down
31 changes: 15 additions & 16 deletions dispatcher/Node.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ type Node struct {
joinerState OtJoinerState
joinerSession *joinerSession
joinResults []*JoinResult
watchLogLevel logger.Level
logger *logger.NodeLogger
}

Expand All @@ -101,24 +100,24 @@ func newNode(d *Dispatcher, nodeid NodeId, cfg *NodeConfig) *Node {
}

nc := &Node{
D: d,
Id: nodeid,
CurTime: d.CurTime,
CreateTime: d.CurTime,
X: cfg.X,
Y: cfg.Y,
ExtAddr: InvalidExtAddr,
Rloc16: threadconst.InvalidRloc16,
Role: OtDeviceRoleDisabled,
conn: nil, // connection will be set when first event is received from node.
err: nil, // keep track of connection errors.
radioNode: radiomodel.NewRadioNode(nodeid, radioCfg),
joinerState: OtJoinerStateIdle,
watchLogLevel: logger.ErrorLevel,
logger: logger.GetNodeLogger(d.cfg.SimulationId, cfg),
D: d,
Id: nodeid,
CurTime: d.CurTime,
CreateTime: d.CurTime,
X: cfg.X,
Y: cfg.Y,
ExtAddr: InvalidExtAddr,
Rloc16: threadconst.InvalidRloc16,
Role: OtDeviceRoleDisabled,
conn: nil, // connection will be set when first event is received from node.
err: nil, // keep track of connection errors.
radioNode: radiomodel.NewRadioNode(nodeid, radioCfg),
joinerState: OtJoinerStateIdle,
logger: logger.GetNodeLogger(d.cfg.SimulationId, cfg),
}

nc.failureCtrl = newFailureCtrl(nc, NonFailTime)
nc.logger.CurrentLevel = logger.ErrorLevel
return nc
}

Expand Down
17 changes: 4 additions & 13 deletions dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ func (d *Dispatcher) AddNode(nodeid NodeId, cfg *NodeConfig) *Node {
d.setAlive(nodeid)

if d.cfg.DefaultWatchOn {
d.WatchNode(nodeid, logger.ParseWatchLogLevel(d.cfg.DefaultWatchLevel))
d.WatchNode(nodeid, logger.ParseLevelString(d.cfg.DefaultWatchLevel))
}
return node
}
Expand Down Expand Up @@ -1233,31 +1233,22 @@ loop:
}
}

func (d *Dispatcher) WatchNode(nodeid NodeId, watchLogLevel logger.Level) {
func (d *Dispatcher) WatchNode(nodeid NodeId, watchLevel logger.Level) {
d.watchingNodes[nodeid] = struct{}{}
node := d.nodes[nodeid]
if node != nil {
node.watchLogLevel = watchLogLevel
node.logger.CurrentLevel = watchLogLevel
node.logger.CurrentLevel = watchLevel
}
}

func (d *Dispatcher) UnwatchNode(nodeid NodeId) {
node := d.nodes[nodeid]
if node != nil {
node.watchLogLevel = logger.ErrorLevel
node.logger.CurrentLevel = logger.ErrorLevel
}
delete(d.watchingNodes, nodeid)
}

func (d *Dispatcher) GetWatchLevel(nodeid NodeId) logger.Level {
node := d.nodes[nodeid]
if node != nil {
return node.watchLogLevel
}
return logger.ErrorLevel
}

func (d *Dispatcher) GetWatchingNodes() []NodeId {
watchingNodeIds := make([]NodeId, len(d.watchingNodes))
j := 0
Expand Down
2 changes: 1 addition & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func SetLevel(lv Level) {

// SetLevelFromString sets the log level based on a string identifier of the level.
func SetLevelFromString(level string) Level {
SetLevel(ParseWatchLogLevel(level))
SetLevel(ParseLevelString(level))
return currentLevel
}

Expand Down
30 changes: 28 additions & 2 deletions logger/parse.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
// Copyright (c) 2022-2023, The OTNS Authors.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holder nor the
// names of its contributors may be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

package logger

const (
Expand All @@ -6,7 +32,7 @@ const (
DefaultLevelString = "default"
)

func ParseWatchLogLevel(level string) Level {
func ParseLevelString(level string) Level {
switch level {
case "micro":
return MicroLevel
Expand All @@ -31,7 +57,7 @@ func ParseWatchLogLevel(level string) Level {
}
}

func GetWatchLogLevelString(level Level) string {
func GetLevelString(level Level) string {
switch level {
case MicroLevel:
return "micro"
Expand Down
4 changes: 2 additions & 2 deletions otns_main/otns_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,15 @@ func createSimulation(ctx *progctx.ProgCtx) *simulation.Simulation {
return nil
}
}
simcfg.LogLevel = logger.ParseWatchLogLevel(args.LogLevel)
simcfg.LogLevel = logger.ParseLevelString(args.LogLevel)

dispatcherCfg := dispatcher.DefaultConfig()
dispatcherCfg.SimulationId = simcfg.Id
if !args.NoPcap {
dispatcherCfg.PcapChannels[simcfg.Channel] = struct{}{}
}
dispatcherCfg.DefaultWatchLevel = args.WatchLevel
dispatcherCfg.DefaultWatchOn = logger.ParseWatchLogLevel(args.WatchLevel) != logger.OffLevel
dispatcherCfg.DefaultWatchOn = logger.ParseLevelString(args.WatchLevel) != logger.OffLevel

sim, err := simulation.NewSimulation(ctx, simcfg, dispatcherCfg)
logger.FatalIfError(err)
Expand Down
2 changes: 1 addition & 1 deletion simulation/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,7 +707,7 @@ loop:
lineTrim := strings.TrimSpace(line)
isLogLine, otLevelChar := otoutfilter.DetectLogLine(line)
if isLogLine {
lev := logger.ParseWatchLogLevel(otLevelChar)
lev := logger.ParseLevelString(otLevelChar)
node.Logger.Log(lev, lineTrim)
} else if idxNewLine == -1 { // if no newline, get more items until a line can be formed.
deadline = time.After(dispatcher.DefaultReadTimeout)
Expand Down

0 comments on commit 3e5bfd5

Please sign in to comment.