Skip to content

Commit

Permalink
Support enabling debug-mode via the config file too
Browse files Browse the repository at this point in the history
Now you can have termshark's deb web server start using --debug or by
setting main.debug = true in the config file. The command-line setting
takes precedence, if provided.
  • Loading branch information
gcla committed Oct 30, 2020
1 parent 308da69 commit b8ee6ac
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
10 changes: 5 additions & 5 deletions cli/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ type Termshark struct {
CaptureFilter string `short:"f" description:"Apply capture filter." value-name:"<capture filter>"`
TimestampFormat string `short:"t" description:"Set the format of the packet timestamp printed in summary lines." choice:"a" choice:"ad" choice:"adoy" choice:"d" choice:"dd" choice:"e" choice:"r" choice:"u" choice:"ud" choice:"udoy" value-name:"<timestamp format>"`
PlatformSwitches
PassThru string `long:"pass-thru" default:"auto" optional:"true" optional-value:"true" choice:"auto" choice:"true" choice:"false" description:"Run tshark instead (auto => if stdout is not a tty)."`
LogTty bool `long:"log-tty" optional:"true" optional-value:"true" choice:"true" choice:"false" description:"Log to the terminal."`
Debug string `long:"debug" default:"false" hidden:"true" optional:"true" optional-value:"true" choice:"true" choice:"false" description:"Enable termshark debugging. See https://termshark.io/userguide."`
Help bool `long:"help" short:"h" optional:"true" optional-value:"true" description:"Show this help message."`
Version []bool `long:"version" short:"v" optional:"true" optional-value:"true" description:"Show version information."`
PassThru string `long:"pass-thru" default:"auto" optional:"true" optional-value:"true" choice:"auto" choice:"true" choice:"false" description:"Run tshark instead (auto => if stdout is not a tty)."`
LogTty bool `long:"log-tty" optional:"true" optional-value:"true" choice:"true" choice:"false" description:"Log to the terminal."`
Debug TriState `long:"debug" default:"unset" hidden:"true" optional:"true" optional-value:"true" description:"Enable termshark debugging. See https://termshark.io/userguide."`
Help bool `long:"help" short:"h" optional:"true" optional-value:"true" description:"Show this help message."`
Version []bool `long:"version" short:"v" optional:"true" optional-value:"true" description:"Show version information."`

Args struct {
FilterOrPcap string `value-name:"<filter-or-file>" description:"Filter (capture for iface, display for pcap), or pcap to read."`
Expand Down
45 changes: 45 additions & 0 deletions cli/tristate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2019-2020 Graham Clark. All rights reserved. Use of this source
// code is governed by the MIT license that can be found in the LICENSE
// file.
//

package cli

//======================================================================

type TriState struct {
Set bool
Val bool
}

func (b *TriState) UnmarshalFlag(value string) error {
switch value {
case "true", "TRUE", "t", "T", "1", "y", "Y", "yes", "Yes", "YES":
b.Set = true
b.Val = true
case "false", "FALSE", "f", "F", "0", "n", "N", "no", "No", "NO":
b.Set = true
b.Val = false
default:
b.Set = false
}
return nil
}

func (b TriState) MarshalFlag() string {
if b.Set {
if b.Val {
return "true"
} else {
return "false"
}
} else {
return "unset"
}
}

//======================================================================
// Local Variables:
// mode: Go
// fill-column: 78
// End:
12 changes: 9 additions & 3 deletions cmd/termshark/termshark.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func cmain() int {
// terminal emumlator supports 256 colors.
termVar := termshark.ConfString("main.term", "")
if termVar != "" {
log.Infof("Configuration file overrides TERM setting, using TERM=%s", termVar)
os.Setenv("TERM", termVar)
}

Expand Down Expand Up @@ -468,7 +469,12 @@ func cmain() int {
log.SetOutput(logfd)
}

if cli.FlagIsTrue(opts.Debug) {
debug := false
if (opts.Debug.Set && opts.Debug.Val == true) || (!opts.Debug.Set && termshark.ConfBool("main.debug", false)) {
debug = true
}

if debug {
for _, addr := range termshark.LocalIPs() {
log.Infof("Starting debug web server at http://%s:6060/debug/pprof/", addr)
}
Expand Down Expand Up @@ -1134,14 +1140,14 @@ Loop:
uiSuspended = false
}
} else if system.IsSigUSR1(sig) {
if cli.FlagIsTrue(opts.Debug) {
if debug {
termshark.ProfileCPUFor(20)
} else {
log.Infof("SIGUSR1 ignored by termshark - see the --debug flag")
}

} else if system.IsSigUSR2(sig) {
if cli.FlagIsTrue(opts.Debug) {
if debug {
termshark.ProfileHeap()
} else {
log.Infof("SIGUSR2 ignored by termshark - see the --debug flag")
Expand Down

0 comments on commit b8ee6ac

Please sign in to comment.