From 15ee9a5cac82b7aeb85455340ade63a634c301f6 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Sun, 26 Oct 2014 13:15:14 +0100 Subject: [PATCH] Break out logger as a reusable component --- Godeps/Godeps.json | 4 +++ .../src/github.com/calmh/logger/LICENSE | 19 ++++++++++ .../src/github.com/calmh/logger/README.md | 14 ++++++++ .../src/github.com/calmh}/logger/logger.go | 35 +++++++++++-------- .../github.com/calmh}/logger/logger_test.go | 16 ++------- cmd/syncthing/gui.go | 2 +- cmd/syncthing/main.go | 2 +- internal/beacon/debug.go | 2 +- internal/config/config.go | 2 +- internal/discover/debug.go | 2 +- internal/events/debug.go | 2 +- internal/files/debug.go | 2 +- internal/model/debug.go | 2 +- internal/protocol/debug.go | 2 +- internal/scanner/debug.go | 2 +- internal/stats/debug.go | 2 +- internal/upgrade/debug.go | 2 +- internal/upnp/debug.go | 2 +- internal/versioner/debug.go | 2 +- 19 files changed, 74 insertions(+), 42 deletions(-) create mode 100644 Godeps/_workspace/src/github.com/calmh/logger/LICENSE create mode 100644 Godeps/_workspace/src/github.com/calmh/logger/README.md rename {internal => Godeps/_workspace/src/github.com/calmh}/logger/logger.go (73%) rename {internal => Godeps/_workspace/src/github.com/calmh}/logger/logger_test.go (63%) diff --git a/Godeps/Godeps.json b/Godeps/Godeps.json index 33f7a1ce..08db2b4e 100644 --- a/Godeps/Godeps.json +++ b/Godeps/Godeps.json @@ -38,6 +38,10 @@ "ImportPath": "github.com/bkaradzic/go-lz4", "Rev": "93a831dcee242be64a9cc9803dda84af25932de7" }, + { + "ImportPath": "github.com/calmh/logger", + "Rev": "f50d32b313bec2933a3e1049f7416a29f3413d29" + }, { "ImportPath": "github.com/calmh/osext", "Rev": "9bf61584e5f1f172e8766ddc9022d9c401faaa5e" diff --git a/Godeps/_workspace/src/github.com/calmh/logger/LICENSE b/Godeps/_workspace/src/github.com/calmh/logger/LICENSE new file mode 100644 index 00000000..fa5b4e20 --- /dev/null +++ b/Godeps/_workspace/src/github.com/calmh/logger/LICENSE @@ -0,0 +1,19 @@ +Copyright (C) 2013 Jakob Borg + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +- The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Godeps/_workspace/src/github.com/calmh/logger/README.md b/Godeps/_workspace/src/github.com/calmh/logger/README.md new file mode 100644 index 00000000..badb2027 --- /dev/null +++ b/Godeps/_workspace/src/github.com/calmh/logger/README.md @@ -0,0 +1,14 @@ +logger +====== + +A small wrapper around `log` to provide log levels. + +Documentation +------------- + +http://godoc.org/github.com/calmh/logger + +License +------- + +MIT diff --git a/internal/logger/logger.go b/Godeps/_workspace/src/github.com/calmh/logger/logger.go similarity index 73% rename from internal/logger/logger.go rename to Godeps/_workspace/src/github.com/calmh/logger/logger.go index a28b0f33..103021bf 100644 --- a/internal/logger/logger.go +++ b/Godeps/_workspace/src/github.com/calmh/logger/logger.go @@ -1,17 +1,5 @@ -// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file). -// -// This program is free software: you can redistribute it and/or modify it -// 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. -// -// This program 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 this program. If not, see . +// Copyright (C) 2014 Jakob Borg. All rights reserved. Use of this source code +// is governed by an MIT-style license that can be found in the LICENSE file. // Package logger implements a standardized logger with callback functionality package logger @@ -35,6 +23,7 @@ const ( NumLevels ) +// A MessageHandler is called with the log level and message text. type MessageHandler func(l LogLevel, msg string) type Logger struct { @@ -43,6 +32,7 @@ type Logger struct { mut sync.Mutex } +// The default logger logs to standard output with a time prefix. var DefaultLogger = New() func New() *Logger { @@ -51,16 +41,20 @@ func New() *Logger { } } +// AddHandler registers a new MessageHandler to receive messages with the +// specified log level or above. func (l *Logger) AddHandler(level LogLevel, h MessageHandler) { l.mut.Lock() defer l.mut.Unlock() l.handlers[level] = append(l.handlers[level], h) } +// See log.SetFlags func (l *Logger) SetFlags(flag int) { l.logger.SetFlags(flag) } +// See log.SetPrefix func (l *Logger) SetPrefix(prefix string) { l.logger.SetPrefix(prefix) } @@ -71,6 +65,7 @@ func (l *Logger) callHandlers(level LogLevel, s string) { } } +// Debugln logs a line with a DEBUG prefix. func (l *Logger) Debugln(vals ...interface{}) { l.mut.Lock() defer l.mut.Unlock() @@ -79,6 +74,7 @@ func (l *Logger) Debugln(vals ...interface{}) { l.callHandlers(LevelDebug, s) } +// Debugf logs a formatted line with a DEBUG prefix. func (l *Logger) Debugf(format string, vals ...interface{}) { l.mut.Lock() defer l.mut.Unlock() @@ -86,6 +82,8 @@ func (l *Logger) Debugf(format string, vals ...interface{}) { l.logger.Output(2, "DEBUG: "+s) l.callHandlers(LevelDebug, s) } + +// Infoln logs a line with an INFO prefix. func (l *Logger) Infoln(vals ...interface{}) { l.mut.Lock() defer l.mut.Unlock() @@ -94,6 +92,7 @@ func (l *Logger) Infoln(vals ...interface{}) { l.callHandlers(LevelInfo, s) } +// Infof logs a formatted line with an INFO prefix. func (l *Logger) Infof(format string, vals ...interface{}) { l.mut.Lock() defer l.mut.Unlock() @@ -102,6 +101,7 @@ func (l *Logger) Infof(format string, vals ...interface{}) { l.callHandlers(LevelInfo, s) } +// Okln logs a line with an OK prefix. func (l *Logger) Okln(vals ...interface{}) { l.mut.Lock() defer l.mut.Unlock() @@ -110,6 +110,7 @@ func (l *Logger) Okln(vals ...interface{}) { l.callHandlers(LevelOK, s) } +// Okf logs a formatted line with an OK prefix. func (l *Logger) Okf(format string, vals ...interface{}) { l.mut.Lock() defer l.mut.Unlock() @@ -118,6 +119,7 @@ func (l *Logger) Okf(format string, vals ...interface{}) { l.callHandlers(LevelOK, s) } +// Warnln logs a formatted line with a WARNING prefix. func (l *Logger) Warnln(vals ...interface{}) { l.mut.Lock() defer l.mut.Unlock() @@ -126,6 +128,7 @@ func (l *Logger) Warnln(vals ...interface{}) { l.callHandlers(LevelWarn, s) } +// Warnf logs a formatted line with a WARNING prefix. func (l *Logger) Warnf(format string, vals ...interface{}) { l.mut.Lock() defer l.mut.Unlock() @@ -134,6 +137,8 @@ func (l *Logger) Warnf(format string, vals ...interface{}) { l.callHandlers(LevelWarn, s) } +// Fatalln logs a line with a FATAL prefix and exits the process with exit +// code 1. func (l *Logger) Fatalln(vals ...interface{}) { l.mut.Lock() defer l.mut.Unlock() @@ -143,6 +148,8 @@ func (l *Logger) Fatalln(vals ...interface{}) { os.Exit(1) } +// Fatalf logs a formatted line with a FATAL prefix and exits the process with +// exit code 1. func (l *Logger) Fatalf(format string, vals ...interface{}) { l.mut.Lock() defer l.mut.Unlock() diff --git a/internal/logger/logger_test.go b/Godeps/_workspace/src/github.com/calmh/logger/logger_test.go similarity index 63% rename from internal/logger/logger_test.go rename to Godeps/_workspace/src/github.com/calmh/logger/logger_test.go index 6243fdc2..39f25ea2 100644 --- a/internal/logger/logger_test.go +++ b/Godeps/_workspace/src/github.com/calmh/logger/logger_test.go @@ -1,17 +1,5 @@ -// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file). -// -// This program is free software: you can redistribute it and/or modify it -// 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. -// -// This program 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 this program. If not, see . +// Copyright (C) 2014 Jakob Borg. All rights reserved. Use of this source code +// is governed by an MIT-style license that can be found in the LICENSE file. package logger diff --git a/cmd/syncthing/gui.go b/cmd/syncthing/gui.go index 97a65fa9..ed219db9 100644 --- a/cmd/syncthing/gui.go +++ b/cmd/syncthing/gui.go @@ -32,11 +32,11 @@ import ( "time" "code.google.com/p/go.crypto/bcrypt" + "github.com/calmh/logger" "github.com/syncthing/syncthing/internal/auto" "github.com/syncthing/syncthing/internal/config" "github.com/syncthing/syncthing/internal/discover" "github.com/syncthing/syncthing/internal/events" - "github.com/syncthing/syncthing/internal/logger" "github.com/syncthing/syncthing/internal/model" "github.com/syncthing/syncthing/internal/osutil" "github.com/syncthing/syncthing/internal/protocol" diff --git a/cmd/syncthing/main.go b/cmd/syncthing/main.go index e862b9a9..ad0f0ef5 100644 --- a/cmd/syncthing/main.go +++ b/cmd/syncthing/main.go @@ -37,12 +37,12 @@ import ( "time" "code.google.com/p/go.crypto/bcrypt" + "github.com/calmh/logger" "github.com/juju/ratelimit" "github.com/syncthing/syncthing/internal/config" "github.com/syncthing/syncthing/internal/discover" "github.com/syncthing/syncthing/internal/events" "github.com/syncthing/syncthing/internal/files" - "github.com/syncthing/syncthing/internal/logger" "github.com/syncthing/syncthing/internal/model" "github.com/syncthing/syncthing/internal/osutil" "github.com/syncthing/syncthing/internal/protocol" diff --git a/internal/beacon/debug.go b/internal/beacon/debug.go index 44d0f754..2295a2e2 100644 --- a/internal/beacon/debug.go +++ b/internal/beacon/debug.go @@ -19,7 +19,7 @@ import ( "os" "strings" - "github.com/syncthing/syncthing/internal/logger" + "github.com/calmh/logger" ) var ( diff --git a/internal/config/config.go b/internal/config/config.go index bc0ea4e7..c036995b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -27,7 +27,7 @@ import ( "strconv" "code.google.com/p/go.crypto/bcrypt" - "github.com/syncthing/syncthing/internal/logger" + "github.com/calmh/logger" "github.com/syncthing/syncthing/internal/osutil" "github.com/syncthing/syncthing/internal/protocol" ) diff --git a/internal/discover/debug.go b/internal/discover/debug.go index d6bee516..61b77984 100644 --- a/internal/discover/debug.go +++ b/internal/discover/debug.go @@ -19,7 +19,7 @@ import ( "os" "strings" - "github.com/syncthing/syncthing/internal/logger" + "github.com/calmh/logger" ) var ( diff --git a/internal/events/debug.go b/internal/events/debug.go index fc26a4c8..44103631 100644 --- a/internal/events/debug.go +++ b/internal/events/debug.go @@ -19,7 +19,7 @@ import ( "os" "strings" - "github.com/syncthing/syncthing/internal/logger" + "github.com/calmh/logger" ) var ( diff --git a/internal/files/debug.go b/internal/files/debug.go index 090eb064..73520ce4 100644 --- a/internal/files/debug.go +++ b/internal/files/debug.go @@ -19,7 +19,7 @@ import ( "os" "strings" - "github.com/syncthing/syncthing/internal/logger" + "github.com/calmh/logger" ) var ( diff --git a/internal/model/debug.go b/internal/model/debug.go index 54788aa7..533fd6ab 100644 --- a/internal/model/debug.go +++ b/internal/model/debug.go @@ -19,7 +19,7 @@ import ( "os" "strings" - "github.com/syncthing/syncthing/internal/logger" + "github.com/calmh/logger" ) var ( diff --git a/internal/protocol/debug.go b/internal/protocol/debug.go index 930aaf34..c46f4a98 100644 --- a/internal/protocol/debug.go +++ b/internal/protocol/debug.go @@ -19,7 +19,7 @@ import ( "os" "strings" - "github.com/syncthing/syncthing/internal/logger" + "github.com/calmh/logger" ) var ( diff --git a/internal/scanner/debug.go b/internal/scanner/debug.go index 89eef958..bffd48cb 100644 --- a/internal/scanner/debug.go +++ b/internal/scanner/debug.go @@ -19,7 +19,7 @@ import ( "os" "strings" - "github.com/syncthing/syncthing/internal/logger" + "github.com/calmh/logger" ) var ( diff --git a/internal/stats/debug.go b/internal/stats/debug.go index 9603c31b..7e0608ef 100755 --- a/internal/stats/debug.go +++ b/internal/stats/debug.go @@ -19,7 +19,7 @@ import ( "os" "strings" - "github.com/syncthing/syncthing/internal/logger" + "github.com/calmh/logger" ) var ( diff --git a/internal/upgrade/debug.go b/internal/upgrade/debug.go index f1bb7385..c7406c07 100644 --- a/internal/upgrade/debug.go +++ b/internal/upgrade/debug.go @@ -19,7 +19,7 @@ import ( "os" "strings" - "github.com/syncthing/syncthing/internal/logger" + "github.com/calmh/logger" ) var ( diff --git a/internal/upnp/debug.go b/internal/upnp/debug.go index b756bd74..9a339d19 100644 --- a/internal/upnp/debug.go +++ b/internal/upnp/debug.go @@ -19,7 +19,7 @@ import ( "os" "strings" - "github.com/syncthing/syncthing/internal/logger" + "github.com/calmh/logger" ) var ( diff --git a/internal/versioner/debug.go b/internal/versioner/debug.go index 6964b7ec..556094e6 100644 --- a/internal/versioner/debug.go +++ b/internal/versioner/debug.go @@ -19,7 +19,7 @@ import ( "os" "strings" - "github.com/syncthing/syncthing/internal/logger" + "github.com/calmh/logger" ) var (