Skip to content

Commit

Permalink
Migrate cabbie logging from eventlog to github.com/google/deck.
Browse files Browse the repository at this point in the history
Highlights:
- Format strings are now supported directly without fmt.Sprintf().
- Custom EventIDs are preserved.
- The deck package can now be used to log in sub-packages with no additional setup required.
PiperOrigin-RevId: 492447724
  • Loading branch information
ItsMattL authored and copybara-github committed Dec 2, 2022
1 parent 1b05a63 commit ccdc28e
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 163 deletions.
118 changes: 60 additions & 58 deletions cabbie.go

Large diffs are not rendered by default.

19 changes: 0 additions & 19 deletions cabbie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,6 @@ const (
testPath = `SOFTWARE\Bar`
)

type testCabbieLog struct {
}

func (f *testCabbieLog) Info(id uint32, msg string) error {
return nil
}
func (f *testCabbieLog) Error(id uint32, msg string) error {
return nil
}
func (f *testCabbieLog) Warning(id uint32, msg string) error {
return nil
}
func (f *testCabbieLog) Close() error {
return nil
}

func createTestKeys() error {
k, _, err := registry.CreateKey(registry.LOCAL_MACHINE, testPath, registry.ALL_ACCESS)
if err != nil {
Expand All @@ -55,7 +39,6 @@ func cleanupTestKey() error {

func TestRegLoadKeyMissing(t *testing.T) {
// Setup
elog = new(testCabbieLog)
expected := newSettings()
testconfig := newSettings()
// End Setup
Expand All @@ -69,7 +52,6 @@ func TestRegLoadKeyMissing(t *testing.T) {

func TestRegLoadKeyEmpty(t *testing.T) {
// Setup
elog = new(testCabbieLog)
if err := createTestKeys(); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -101,7 +83,6 @@ func TestRegLoadRequiredCategories(t *testing.T) {
k.Close()
defer cleanupTestKey()

elog = new(testCabbieLog)
expected := newSettings()
expected.RequiredCategories = rc
testconfig := newSettings()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
module github.com/google/cabbie

go 1.16
go 1.18

require (
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-ole/go-ole v1.2.5
github.com/google/aukera v0.0.0-20201117230544-d145c8357fea
github.com/google/deck v0.0.0-20221201002015-b833469335f8
github.com/google/glazier v0.0.0-20210617205946-bf91b619f5d4
github.com/google/go-cmp v0.5.4
github.com/google/logger v1.1.0 // indirect
github.com/google/subcommands v1.2.0
github.com/iamacarpet/go-win64api v0.0.0-20210311141720-fe38760bed28 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
Expand Down
15 changes: 8 additions & 7 deletions hide.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -24,6 +24,7 @@ import (
"github.com/google/cabbie/search"
"github.com/google/cabbie/session"
"github.com/google/cabbie/updatecollection"
"github.com/google/deck"
"github.com/google/subcommands"
)

Expand All @@ -44,7 +45,7 @@ func (c *hideCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&c.unhide, "unhide", false, "mark a hidden update as visible.")
}

func (c hideCmd) Execute(_ context.Context, flags *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
func (c hideCmd) Execute(_ context.Context, flags *flag.FlagSet, _ ...any) subcommands.ExitStatus {
kbs := NewKBSet(c.kbs)

if kbs.Size() < 1 {
Expand All @@ -55,7 +56,7 @@ func (c hideCmd) Execute(_ context.Context, flags *flag.FlagSet, _ ...interface{
if c.unhide {
if err := unhide(kbs); err != nil {
fmt.Println(err)
elog.Error(cablib.EvtErrUnhide, fmt.Sprintf("Error unhiding an update: %v", err))
deck.ErrorfA("Error unhiding an update: %v", err).With(eventID(cablib.EvtErrUnhide)).Go()
}
return subcommands.ExitSuccess
}
Expand Down Expand Up @@ -94,9 +95,9 @@ func unhide(kbs KBSet) error {

for _, u := range uc.Updates {
if kbs.Search(u.KBArticleIDs) {
elog.Info(cablib.EvtUnhide, fmt.Sprintf("Unhiding update:\n%s", u.Title))
deck.InfofA("Unhiding update:\n%s", u.Title).With(eventID(cablib.EvtUnhide)).Go()
if err := u.UnHide(); err != nil {
elog.Error(cablib.EvtErrUnhide, fmt.Sprintf("Failed to unhide update %s:\n %s", u.Title, err))
deck.ErrorfA("Failed to unhide update %s:\n %s", u.Title, err).With(eventID(cablib.EvtErrUnhide)).Go()
}
}
}
Expand All @@ -114,9 +115,9 @@ func hide(kbs KBSet) error {

for _, u := range uc.Updates {
if kbs.Search(u.KBArticleIDs) {
elog.Info(cablib.EvtHide, fmt.Sprintf("Hiding update:\n%s", u.Title))
deck.InfofA("Hiding update:\n%s", u.Title).With(eventID(cablib.EvtHide)).Go()
if err := u.Hide(); err != nil {
elog.Error(cablib.EvtErrHide, fmt.Sprintf("Failed to hide update %s:\n %s", u.Title, err))
deck.ErrorfA("Failed to hide update %s:\n %s", u.Title, err).With(eventID(cablib.EvtErrHide)).Go()
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions history.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -24,6 +24,7 @@ import (
"github.com/google/cabbie/search"
"github.com/google/cabbie/session"
"github.com/google/cabbie/updatehistory"
"github.com/google/deck"
"github.com/google/subcommands"
)

Expand All @@ -38,11 +39,11 @@ func (historyCmd) Usage() string {
}
func (c *historyCmd) SetFlags(f *flag.FlagSet) {}

func (c *historyCmd) Execute(_ context.Context, flags *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
func (c *historyCmd) Execute(_ context.Context, flags *flag.FlagSet, _ ...any) subcommands.ExitStatus {
h, err := history()
if err != nil {
fmt.Printf("Failed to get update history: %s", err)
elog.Error(cablib.EvtErrHistory, fmt.Sprintf("Failed to get Update history: %s", err))
deck.ErrorfA("Failed to get Update history: %s", err).With(eventID(cablib.EvtErrHistory)).Go()
return subcommands.ExitFailure
}
defer h.Close()
Expand All @@ -67,6 +68,6 @@ func history() (*updatehistory.History, error) {
}
defer searcher.Close()

elog.Info(cablib.EvtHistory, "Collecting installed updates...")
deck.InfoA("Collecting installed updates...").With(eventID(cablib.EvtHistory)).Go()
return updatehistory.Get(searcher)
}
Loading

0 comments on commit ccdc28e

Please sign in to comment.