blob: c8a84af40768a068dbf61e7d474c4f837d3732d3 [file] [log] [blame]
Cosmos Nicolaou0e4e3922015-06-10 16:30:09 -07001// 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.
8package logger
9
10import (
11 "v.io/x/lib/vlog"
12
Cosmos Nicolaou9691e5d2015-06-17 12:24:35 -070013 "v.io/v23/context"
Cosmos Nicolaou0e4e3922015-06-10 16:30:09 -070014 "v.io/v23/logging"
15)
16
17// Global returns the global logger.
18func Global() logging.Logger {
19 return vlog.Log
20}
21
22// NewLogger creates a new logger with the supplied name.
23func NewLogger(name string) logging.Logger {
24 return vlog.NewLogger(name)
25}
26
27// ManageLog defines the methods for managing and configuring a logger.
28type 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
48type dummy struct{}
49
50func (*dummy) LogDir() string { return "" }
51func (*dummy) Stats() (Info, Error struct{ Lines, Bytes int64 }) {
52 return struct{ Lines, Bytes int64 }{0, 0}, struct{ Lines, Bytes int64 }{0, 0}
53}
54func (*dummy) ConfigureFromFlags() error { return nil }
55func (*dummy) ExplicitlySetFlags() map[string]string { return nil }
56
Cosmos Nicolaou9691e5d2015-06-17 12:24:35 -070057// Manager determines if the supplied logging.Logger implements ManageLog and if so
Cosmos Nicolaou0e4e3922015-06-10 16:30:09 -070058// 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.
61func Manager(logger logging.Logger) ManageLog {
62 if vl, ok := logger.(ManageLog); ok {
63 return vl
64 }
Cosmos Nicolaou9691e5d2015-06-17 12:24:35 -070065 // 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 Nicolaou0e4e3922015-06-10 16:30:09 -070073 return &dummy{}
74}
Cosmos Nicolaoue9c622d2015-07-10 11:09:42 -070075
76// IsAlreadyConfiguredError returns true if the err parameter indicates
77// the the logger has already been configured.
78func IsAlreadyConfiguredError(err error) bool {
79 return err == vlog.ErrConfigured
80}