diff --git a/klog.go b/klog.go index 1bd1c2e98..c2b244efb 100644 --- a/klog.go +++ b/klog.go @@ -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. diff --git a/klog_test.go b/klog_test.go index fc1fdfd8a..00d39c217 100644 --- a/klog_test.go +++ b/klog_test.go @@ -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) + } +}