Add a ContextOption type to support functional options in NewContext.
Then, add a variadic ContextOption parameter to NewContext, which allows
callers to specify 0 or more configuration options.
For now, just add the WithLogLevel option so that callers can set the
log level of the Context's xentoollog_logger. Future configuration
options can be created by adding an appropriate field to the
contextOptions struct and creating a With<OptionName> function to return
a ContextOption
Signed-off-by: Nick Rosbrook <rosbrookn@ainfosec.com>
---
tools/golang/xenlight/xenlight.go | 44 +++++++++++++++++++++++++++++--
1 file changed, 42 insertions(+), 2 deletions(-)
diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go
index f68d7b6e97..65f93abe32 100644
--- a/tools/golang/xenlight/xenlight.go
+++ b/tools/golang/xenlight/xenlight.go
@@ -136,7 +136,7 @@ func sigchldHandler(ctx *Context) {
}
// NewContext returns a new Context.
-func NewContext() (ctx *Context, err error) {
+func NewContext(opts ...ContextOption) (ctx *Context, err error) {
ctx = &Context{}
defer func() {
@@ -146,8 +146,19 @@ func NewContext() (ctx *Context, err error) {
}
}()
+ // Set the default context options. These fields may
+ // be modified by the provided opts.
+ copts := &contextOptions{
+ logLevel: LogLevelError,
+ }
+
+ for _, opt := range opts {
+ opt.apply(copts)
+ }
+
// Create a logger
- ctx.logger = C.xtl_createlogger_stdiostream(C.stderr, C.XTL_ERROR, 0)
+ ctx.logger = C.xtl_createlogger_stdiostream(C.stderr,
+ C.xentoollog_level(copts.logLevel), 0)
// Allocate a context
ret := C.libxl_ctx_alloc(&ctx.ctx, C.LIBXL_VERSION, 0,
@@ -201,6 +212,35 @@ func (ctx *Context) Close() error {
return nil
}
+type contextOptions struct {
+ logLevel LogLevel
+}
+
+// ContextOption is used to configure options for a Context.
+type ContextOption interface {
+ apply(*contextOptions)
+}
+
+type funcContextOption struct {
+ f func(*contextOptions)
+}
+
+func (fco *funcContextOption) apply(c *contextOptions) {
+ fco.f(c)
+}
+
+func newFuncContextOption(f func(*contextOptions)) *funcContextOption {
+ return &funcContextOption{f}
+}
+
+// WithLogLevel sets the log level for a Context's logger. The default level is
+// LogLevelError.
+func WithLogLevel(level LogLevel) ContextOption {
+ return newFuncContextOption(func(co *contextOptions) {
+ co.logLevel = level
+ })
+}
+
// LogLevel represents an xentoollog_level, and can be used to configre the log
// level of a Context's logger.
type LogLevel int
--
2.17.1
Then, add a variadic ContextOption parameter to NewContext, which allows
callers to specify 0 or more configuration options.
For now, just add the WithLogLevel option so that callers can set the
log level of the Context's xentoollog_logger. Future configuration
options can be created by adding an appropriate field to the
contextOptions struct and creating a With<OptionName> function to return
a ContextOption
Signed-off-by: Nick Rosbrook <rosbrookn@ainfosec.com>
---
tools/golang/xenlight/xenlight.go | 44 +++++++++++++++++++++++++++++--
1 file changed, 42 insertions(+), 2 deletions(-)
diff --git a/tools/golang/xenlight/xenlight.go b/tools/golang/xenlight/xenlight.go
index f68d7b6e97..65f93abe32 100644
--- a/tools/golang/xenlight/xenlight.go
+++ b/tools/golang/xenlight/xenlight.go
@@ -136,7 +136,7 @@ func sigchldHandler(ctx *Context) {
}
// NewContext returns a new Context.
-func NewContext() (ctx *Context, err error) {
+func NewContext(opts ...ContextOption) (ctx *Context, err error) {
ctx = &Context{}
defer func() {
@@ -146,8 +146,19 @@ func NewContext() (ctx *Context, err error) {
}
}()
+ // Set the default context options. These fields may
+ // be modified by the provided opts.
+ copts := &contextOptions{
+ logLevel: LogLevelError,
+ }
+
+ for _, opt := range opts {
+ opt.apply(copts)
+ }
+
// Create a logger
- ctx.logger = C.xtl_createlogger_stdiostream(C.stderr, C.XTL_ERROR, 0)
+ ctx.logger = C.xtl_createlogger_stdiostream(C.stderr,
+ C.xentoollog_level(copts.logLevel), 0)
// Allocate a context
ret := C.libxl_ctx_alloc(&ctx.ctx, C.LIBXL_VERSION, 0,
@@ -201,6 +212,35 @@ func (ctx *Context) Close() error {
return nil
}
+type contextOptions struct {
+ logLevel LogLevel
+}
+
+// ContextOption is used to configure options for a Context.
+type ContextOption interface {
+ apply(*contextOptions)
+}
+
+type funcContextOption struct {
+ f func(*contextOptions)
+}
+
+func (fco *funcContextOption) apply(c *contextOptions) {
+ fco.f(c)
+}
+
+func newFuncContextOption(f func(*contextOptions)) *funcContextOption {
+ return &funcContextOption{f}
+}
+
+// WithLogLevel sets the log level for a Context's logger. The default level is
+// LogLevelError.
+func WithLogLevel(level LogLevel) ContextOption {
+ return newFuncContextOption(func(co *contextOptions) {
+ co.logLevel = level
+ })
+}
+
// LogLevel represents an xentoollog_level, and can be used to configre the log
// level of a Context's logger.
type LogLevel int
--
2.17.1