Skip to content

Commit

Permalink
Merge pull request #9158 from mvo5/debug-via-kernelenv
Browse files Browse the repository at this point in the history
logger: add support for setting snapd.debug=1 on kernel cmdline
  • Loading branch information
mvo5 authored Aug 25, 2020
2 parents 3d0ca30 + a078660 commit a79aa06
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
10 changes: 9 additions & 1 deletion logger/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,18 @@ func GetLogger() Logger {
}

func GetLoggerFlags() int {
log, ok := GetLogger().(Log)
log, ok := GetLogger().(*Log)
if !ok {
return -1
}

return log.log.Flags()
}

func MockProcCmdline(new string) (restore func()) {
old := procCmdline
procCmdline = new
return func() {
procCmdline = old
}
}
30 changes: 26 additions & 4 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"strings"
"sync"

"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/strutil"
)

// A Logger is a fairly minimal logging tool.
Expand Down Expand Up @@ -121,23 +124,29 @@ func SetLogger(l Logger) {

type Log struct {
log *log.Logger

debug bool
}

// Debug only prints if SNAPD_DEBUG is set
func (l Log) Debug(msg string) {
if osutil.GetenvBool("SNAPD_DEBUG") {
func (l *Log) Debug(msg string) {
if l.debug || osutil.GetenvBool("SNAPD_DEBUG") {
l.log.Output(3, "DEBUG: "+msg)
}
}

// Notice alerts the user about something, as well as putting it syslog
func (l Log) Notice(msg string) {
func (l *Log) Notice(msg string) {
l.log.Output(3, msg)
}

// New creates a log.Logger using the given io.Writer and flag.
func New(w io.Writer, flag int) (Logger, error) {
return Log{log: log.New(w, "", flag)}, nil
logger := &Log{
log: log.New(w, "", flag),
debug: debugEnabledOnKernelCmdline(),
}
return logger, nil
}

// SimpleSetup creates the default (console) logger
Expand All @@ -153,3 +162,16 @@ func SimpleSetup() error {
}
return err
}

var procCmdline = "/proc/cmdline"

// TODO: consider generalizing this to snapdenv and having it used by
// other places that consider SNAPD_DEBUG
func debugEnabledOnKernelCmdline() bool {
buf, err := ioutil.ReadFile(procCmdline)
if err != nil {
return false
}
l := strings.Split(string(buf), " ")
return strutil.ListContains(l, "snapd.debug=1")
}
16 changes: 16 additions & 0 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ package logger_test

import (
"bytes"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"testing"

Expand Down Expand Up @@ -122,3 +124,17 @@ func (s *LogSuite) TestWithLoggerLock(c *C) {
})
c.Check(called, Equals, true)
}

func (s *LogSuite) TestIntegrationDebugFromKernelCmdline(c *C) {
mockProcCmdline := filepath.Join(c.MkDir(), "proc-cmdline")
err := ioutil.WriteFile(mockProcCmdline, []byte("console=tty panic=-1 snapd.debug=1"), 0644)
c.Assert(err, IsNil)
restore := logger.MockProcCmdline(mockProcCmdline)
defer restore()

var buf bytes.Buffer
l, err := logger.New(&buf, logger.DefaultFlags)
c.Assert(err, IsNil)
l.Debug("xyzzy")
c.Check(buf.String(), testutil.Contains, `DEBUG: xyzzy`)
}

0 comments on commit a79aa06

Please sign in to comment.