Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | // Package logger provides access to an implementation of v23/logger.Logging |
| 6 | // for use within a runtime implementation. There is pre-created "global" logger |
| 7 | // and the ability to create new loggers. |
| 8 | package logger |
| 9 | |
| 10 | import ( |
| 11 | "v.io/x/lib/vlog" |
| 12 | |
Cosmos Nicolaou | 9691e5d | 2015-06-17 12:24:35 -0700 | [diff] [blame] | 13 | "v.io/v23/context" |
Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 14 | "v.io/v23/logging" |
| 15 | ) |
| 16 | |
| 17 | // Global returns the global logger. |
| 18 | func Global() logging.Logger { |
| 19 | return vlog.Log |
| 20 | } |
| 21 | |
| 22 | // NewLogger creates a new logger with the supplied name. |
| 23 | func NewLogger(name string) logging.Logger { |
| 24 | return vlog.NewLogger(name) |
| 25 | } |
| 26 | |
| 27 | // ManageLog defines the methods for managing and configuring a logger. |
| 28 | type ManageLog interface { |
| 29 | |
| 30 | // LogDir returns the directory where the log files are written. |
| 31 | LogDir() string |
| 32 | |
| 33 | // Stats returns stats on how many lines/bytes haven been written to |
| 34 | // this set of logs. |
| 35 | Stats() (Info, Error struct{ Lines, Bytes int64 }) |
| 36 | |
| 37 | // ConfigureLoggerFromFlags will configure the supplied logger using |
| 38 | // command line flags. |
| 39 | ConfigureFromFlags() error |
| 40 | |
| 41 | // ExplicitlySetFlags returns a map of the logging command line flags and their |
| 42 | // values formatted as strings. Only the flags that were explicitly set are |
| 43 | // returned. This is intended for use when an application needs to know what |
| 44 | // value the flags were set to, for example when creating subprocesses. |
| 45 | ExplicitlySetFlags() map[string]string |
| 46 | } |
| 47 | |
| 48 | type dummy struct{} |
| 49 | |
| 50 | func (*dummy) LogDir() string { return "" } |
| 51 | func (*dummy) Stats() (Info, Error struct{ Lines, Bytes int64 }) { |
| 52 | return struct{ Lines, Bytes int64 }{0, 0}, struct{ Lines, Bytes int64 }{0, 0} |
| 53 | } |
| 54 | func (*dummy) ConfigureFromFlags() error { return nil } |
| 55 | func (*dummy) ExplicitlySetFlags() map[string]string { return nil } |
| 56 | |
Cosmos Nicolaou | 9691e5d | 2015-06-17 12:24:35 -0700 | [diff] [blame] | 57 | // Manager determines if the supplied logging.Logger implements ManageLog and if so |
Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 58 | // returns an instance of it. If it doesn't implement ManageLog then Manager |
| 59 | // will return a dummy implementation that is essentially a no-op. It is |
| 60 | // always safe to use it as: logger.Manager(logger.Global()).LogDir() for example. |
| 61 | func Manager(logger logging.Logger) ManageLog { |
| 62 | if vl, ok := logger.(ManageLog); ok { |
| 63 | return vl |
| 64 | } |
Cosmos Nicolaou | 9691e5d | 2015-06-17 12:24:35 -0700 | [diff] [blame] | 65 | // If the logger is a context.T then ask it for its implementation |
| 66 | // of logging.Logger and look for ManageLog being implemented by it, |
| 67 | // since context.T can never implement ManageLog itself. |
| 68 | if ctx, ok := logger.(*context.T); ok { |
| 69 | if l, ok := ctx.LoggerImplementation().(logging.Logger); ok { |
| 70 | return Manager(l) |
| 71 | } |
| 72 | } |
Cosmos Nicolaou | 0e4e392 | 2015-06-10 16:30:09 -0700 | [diff] [blame] | 73 | return &dummy{} |
| 74 | } |
Cosmos Nicolaou | e9c622d | 2015-07-10 11:09:42 -0700 | [diff] [blame] | 75 | |
| 76 | // IsAlreadyConfiguredError returns true if the err parameter indicates |
| 77 | // the the logger has already been configured. |
| 78 | func IsAlreadyConfiguredError(err error) bool { |
| 79 | return err == vlog.ErrConfigured |
| 80 | } |