Skip to content

Commit

Permalink
Modify setVState method to use receiver identifier
Browse files Browse the repository at this point in the history
This commit modifies setVState method to use receiver identifier.
Before this commit, this method doesn't use receiver but uses
top-level variable 'logging'.

This commit also adds test function of setVState.
  • Loading branch information
vanou committed Feb 14, 2020
1 parent 12be8a0 commit 299c76c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
12 changes: 6 additions & 6 deletions klog.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,20 +517,20 @@ var logging loggingT
// l.mu is held.
func (l *loggingT) setVState(verbosity Level, filter []modulePat, setFilter bool) {
// Turn verbosity off so V will not fire while we are in transition.
logging.verbosity.set(0)
l.verbosity.set(0)
// Ditto for filter length.
atomic.StoreInt32(&logging.filterLength, 0)
atomic.StoreInt32(&l.filterLength, 0)

// Set the new filters and wipe the pc->Level map if the filter has changed.
if setFilter {
logging.vmodule.filter = filter
logging.vmap = make(map[uintptr]Level)
l.vmodule.filter = filter
l.vmap = make(map[uintptr]Level)
}

// Things are consistent now, so enable filtering and verbosity.
// They are enabled in order opposite to that in V.
atomic.StoreInt32(&logging.filterLength, int32(len(filter)))
logging.verbosity.set(verbosity)
atomic.StoreInt32(&l.filterLength, int32(len(filter)))
l.verbosity.set(verbosity)
}

// getBuffer returns a new, ready-to-use buffer.
Expand Down
63 changes: 63 additions & 0 deletions klog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,3 +639,66 @@ func TestInitFlags(t *testing.T) {
t.Fatal("Expected log_file_max_size to be 2048")
}
}

func createTestValueOfLoggingT() *loggingT {
l := new(loggingT)
l.toStderr = true
l.alsoToStderr = false
l.stderrThreshold = errorLog
l.verbosity = Level(0)
l.skipHeaders = false
l.skipLogHeaders = false
l.addDirHeader = false
return l
}

func createTestValueOfModulePat(p string, li bool, le Level) modulePat {
m := modulePat{}
m.pattern = p
m.literal = li
m.level = le
return m
}

func compareModuleSpec(a, b moduleSpec) bool {
if len(a.filter) != len(b.filter) {
return false
}

for i := 0; i < len(a.filter); i++ {
if a.filter[i] != b.filter[i] {
return false
}
}

return true
}

func TestSetVState(t *testing.T) {
//Target loggingT value
want := createTestValueOfLoggingT()
want.verbosity = Level(3)
want.vmodule.filter = []modulePat{
createTestValueOfModulePat("recordio", true, Level(2)),
createTestValueOfModulePat("file", true, Level(1)),
createTestValueOfModulePat("gfs*", false, Level(3)),
createTestValueOfModulePat("gopher*", false, Level(3)),
}
want.filterLength = 4

//loggingT value to which test is run
target := createTestValueOfLoggingT()

tf := []modulePat{
createTestValueOfModulePat("recordio", true, Level(2)),
createTestValueOfModulePat("file", true, Level(1)),
createTestValueOfModulePat("gfs*", false, Level(3)),
createTestValueOfModulePat("gopher*", false, Level(3)),
}

target.setVState(Level(3), tf, true)

if want.verbosity != target.verbosity || !compareModuleSpec(want.vmodule, target.vmodule) || want.filterLength != target.filterLength {
t.Errorf("setVState method doesn't configure loggingT values' verbosity, vmodule or filterLength:\nwant:\n\tverbosity:\t%v\n\tvmodule:\t%v\n\tfilterLength:\t%v\ngot:\n\tverbosity:\t%v\n\tvmodule:\t%v\n\tfilterLength:\t%v", want.verbosity, want.vmodule, want.filterLength, target.verbosity, target.vmodule, target.filterLength)
}
}

0 comments on commit 299c76c

Please sign in to comment.