av/cmd/vidforward/global/logger.go

57 lines
1.8 KiB
Go
Raw Normal View History

/*
DESCRIPTION
logger.go provides a "safe" global logger by following the singleton pattern.
Usage of this should be avoided if possible, but in some instances it might be
necessary, for example implementations of interfaces where logging is required
but do not offer parameters where a logger can be passed as an argument.
AUTHORS
Saxon A. Nelson-Milton <saxon@ausocean.org>
LICENSE
Copyright (C) 2022 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
*/
package global
import "bitbucket.org/ausocean/utils/logging"
var logger *globalLogger = nil
type globalLogger struct {
logging.Logger
}
// SetLogger sets the global logger. This must be set, and only once, before
// the GetLogger function is called. If these requirements are violated panics
// will occur.
func SetLogger(l logging.Logger) {
if logger != nil {
logger.Fatal("attempting set of already instantiated global logger")
}
logger = &globalLogger{l}
}
// GetLogger returns the global logger. If this has not been set, a panic will
// occur.
func GetLogger() logging.Logger {
if logger == nil {
panic("attempted get of uninstantiated global logger")
}
// We want to return the underlying logger.
return logger.Logger
}