2017-02-09 12:01:20 +01:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2017-06-26 23:46:45 +02:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2017-02-09 12:01:20 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// LogLevel describes rclone's logs. These are a subset of the syslog log levels.
|
|
|
|
type LogLevel byte
|
|
|
|
|
2017-02-10 14:28:06 +01:00
|
|
|
// Log levels. These are the syslog levels of which we only use a
|
|
|
|
// subset.
|
|
|
|
//
|
|
|
|
// LOG_EMERG system is unusable
|
|
|
|
// LOG_ALERT action must be taken immediately
|
|
|
|
// LOG_CRIT critical conditions
|
|
|
|
// LOG_ERR error conditions
|
|
|
|
// LOG_WARNING warning conditions
|
|
|
|
// LOG_NOTICE normal, but significant, condition
|
|
|
|
// LOG_INFO informational message
|
|
|
|
// LOG_DEBUG debug-level message
|
2017-02-09 12:01:20 +01:00
|
|
|
const (
|
2017-02-09 22:22:46 +01:00
|
|
|
LogLevelEmergency LogLevel = iota
|
2017-02-09 12:01:20 +01:00
|
|
|
LogLevelAlert
|
|
|
|
LogLevelCritical
|
|
|
|
LogLevelError // Error - can't be suppressed
|
|
|
|
LogLevelWarning
|
|
|
|
LogLevelNotice // Normal logging, -q suppresses
|
|
|
|
LogLevelInfo // Transfers, needs -v
|
|
|
|
LogLevelDebug // Debug level, needs -vv
|
|
|
|
)
|
|
|
|
|
2017-02-10 14:28:06 +01:00
|
|
|
var logLevelToString = []string{
|
|
|
|
LogLevelEmergency: "EMERGENCY",
|
|
|
|
LogLevelAlert: "ALERT",
|
|
|
|
LogLevelCritical: "CRITICAL",
|
|
|
|
LogLevelError: "ERROR",
|
|
|
|
LogLevelWarning: "WARNING",
|
|
|
|
LogLevelNotice: "NOTICE",
|
|
|
|
LogLevelInfo: "INFO",
|
|
|
|
LogLevelDebug: "DEBUG",
|
|
|
|
}
|
2017-02-09 12:01:20 +01:00
|
|
|
|
2017-02-10 14:28:06 +01:00
|
|
|
// String turns a LogLevel into a string
|
|
|
|
func (l LogLevel) String() string {
|
|
|
|
if l >= LogLevel(len(logLevelToString)) {
|
|
|
|
return fmt.Sprintf("LogLevel(%d)", l)
|
|
|
|
}
|
|
|
|
return logLevelToString[l]
|
|
|
|
}
|
2017-02-09 12:01:20 +01:00
|
|
|
|
2017-06-26 23:46:45 +02:00
|
|
|
// Set a LogLevel
|
|
|
|
func (l *LogLevel) Set(s string) error {
|
|
|
|
for n, name := range logLevelToString {
|
|
|
|
if s != "" && name == s {
|
|
|
|
*l = LogLevel(n)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return errors.Errorf("Unknown log level %q", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type of the value
|
|
|
|
func (l *LogLevel) Type() string {
|
|
|
|
return "string"
|
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// LogPrint sends the text to the logger of level
|
|
|
|
var LogPrint = func(level LogLevel, text string) {
|
2017-02-10 14:28:06 +01:00
|
|
|
text = fmt.Sprintf("%-6s: %s", level, text)
|
2018-09-02 19:11:09 +02:00
|
|
|
_ = log.Output(4, text)
|
2017-02-10 14:28:06 +01:00
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// LogPrintf produces a log string from the arguments passed in
|
|
|
|
func LogPrintf(level LogLevel, o interface{}, text string, args ...interface{}) {
|
2017-02-09 12:01:20 +01:00
|
|
|
out := fmt.Sprintf(text, args...)
|
2017-02-10 14:28:06 +01:00
|
|
|
if o != nil {
|
|
|
|
out = fmt.Sprintf("%v: %s", o, out)
|
2017-02-09 12:01:20 +01:00
|
|
|
}
|
2018-01-12 17:30:54 +01:00
|
|
|
LogPrint(level, out)
|
2017-02-09 12:01:20 +01:00
|
|
|
}
|
|
|
|
|
2017-06-26 23:46:45 +02:00
|
|
|
// LogLevelPrintf writes logs at the given level
|
|
|
|
func LogLevelPrintf(level LogLevel, o interface{}, text string, args ...interface{}) {
|
|
|
|
if Config.LogLevel >= level {
|
2018-01-12 17:30:54 +01:00
|
|
|
LogPrintf(level, o, text, args...)
|
2017-06-26 23:46:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 12:01:20 +01:00
|
|
|
// Errorf writes error log output for this Object or Fs. It
|
2017-02-10 14:28:06 +01:00
|
|
|
// should always be seen by the user.
|
2017-02-09 12:01:20 +01:00
|
|
|
func Errorf(o interface{}, text string, args ...interface{}) {
|
2017-02-09 22:22:46 +01:00
|
|
|
if Config.LogLevel >= LogLevelError {
|
2018-01-12 17:30:54 +01:00
|
|
|
LogPrintf(LogLevelError, o, text, args...)
|
2017-02-09 22:22:46 +01:00
|
|
|
}
|
2017-02-09 12:01:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Logf writes log output for this Object or Fs. This should be
|
2017-02-09 22:22:46 +01:00
|
|
|
// considered to be Info level logging. It is the default level. By
|
|
|
|
// default rclone should not log very much so only use this for
|
|
|
|
// important things the user should see. The user can filter these
|
|
|
|
// out with the -q flag.
|
2017-02-09 12:01:20 +01:00
|
|
|
func Logf(o interface{}, text string, args ...interface{}) {
|
2017-02-09 22:22:46 +01:00
|
|
|
if Config.LogLevel >= LogLevelNotice {
|
2018-01-12 17:30:54 +01:00
|
|
|
LogPrintf(LogLevelNotice, o, text, args...)
|
2017-02-09 12:01:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 22:22:46 +01:00
|
|
|
// Infof writes info on transfers for this Object or Fs. Use this
|
|
|
|
// level for logging transfers, deletions and things which should
|
|
|
|
// appear with the -v flag.
|
2017-02-09 12:01:20 +01:00
|
|
|
func Infof(o interface{}, text string, args ...interface{}) {
|
2017-02-09 22:22:46 +01:00
|
|
|
if Config.LogLevel >= LogLevelInfo {
|
2018-01-12 17:30:54 +01:00
|
|
|
LogPrintf(LogLevelInfo, o, text, args...)
|
2017-02-09 12:01:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 22:22:46 +01:00
|
|
|
// Debugf writes debugging output for this Object or Fs. Use this for
|
|
|
|
// debug only. The user must have to specify -vv to see this.
|
2017-02-09 12:01:20 +01:00
|
|
|
func Debugf(o interface{}, text string, args ...interface{}) {
|
2017-02-09 22:22:46 +01:00
|
|
|
if Config.LogLevel >= LogLevelDebug {
|
2018-01-12 17:30:54 +01:00
|
|
|
LogPrintf(LogLevelDebug, o, text, args...)
|
2017-05-09 12:38:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// LogDirName returns an object for the logger, logging a root
|
|
|
|
// directory which would normally be "" as the Fs
|
|
|
|
func LogDirName(f Fs, dir string) interface{} {
|
|
|
|
if dir != "" {
|
|
|
|
return dir
|
2017-02-09 12:01:20 +01:00
|
|
|
}
|
2018-01-12 17:30:54 +01:00
|
|
|
return f
|
2017-02-09 12:01:20 +01:00
|
|
|
}
|