Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fb3489c
commit b31c4cf
Showing
8 changed files
with
277 additions
and
90 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package clog | ||
|
||
import ( | ||
"log" | ||
"os" | ||
) | ||
|
||
// FileLog logs messages to a file | ||
type FileLog struct { | ||
quiet bool | ||
verbose bool | ||
file *os.File | ||
} | ||
|
||
// NewFileLog creates a new file logger | ||
// Remember to Close() the logger at the end | ||
func NewFileLog(filename string) (*FileLog, error) { | ||
file, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644) | ||
if err != nil { | ||
return nil, err | ||
} | ||
logger := &FileLog{ | ||
file: file, | ||
} | ||
// also redirect the standard logger to the file | ||
SetOutput(file) | ||
return logger, nil | ||
} | ||
|
||
// Close the logfile when no longer needed | ||
// please note this method reinstate the NULL logger as the default logger | ||
func (l *FileLog) Close() { | ||
if l.file != nil { | ||
l.file.Sync() | ||
l.file.Close() | ||
l.file = nil | ||
} | ||
// make sure any other call to the logger won't panic | ||
defaultLog = &NullLog{} | ||
// and reset the standard logger | ||
SetOutput(os.Stdout) | ||
} | ||
|
||
// Quiet will only display warnings and errors | ||
func (l *FileLog) Quiet() { | ||
l.quiet = true | ||
l.verbose = false | ||
} | ||
|
||
// Verbose will display debugging information | ||
func (l *FileLog) Verbose() { | ||
l.verbose = true | ||
l.quiet = false | ||
} | ||
|
||
// Debug sends debugging information | ||
func (l *FileLog) Debug(v ...interface{}) { | ||
if !l.verbose { | ||
return | ||
} | ||
l.message(DebugLevel, v...) | ||
} | ||
|
||
// Debugf sends debugging information | ||
func (l *FileLog) Debugf(format string, v ...interface{}) { | ||
if !l.verbose { | ||
return | ||
} | ||
l.messagef(DebugLevel, format, v...) | ||
} | ||
|
||
// Info logs some noticeable information | ||
func (l *FileLog) Info(v ...interface{}) { | ||
if l.quiet { | ||
return | ||
} | ||
l.message(InfoLevel, v...) | ||
} | ||
|
||
// Infof logs some noticeable information | ||
func (l *FileLog) Infof(format string, v ...interface{}) { | ||
if l.quiet { | ||
return | ||
} | ||
l.messagef(InfoLevel, format, v...) | ||
} | ||
|
||
// Warning send some important message to the console | ||
func (l *FileLog) Warning(v ...interface{}) { | ||
l.message(WarningLevel, v...) | ||
} | ||
|
||
// Warningf send some important message to the console | ||
func (l *FileLog) Warningf(format string, v ...interface{}) { | ||
l.messagef(WarningLevel, format, v...) | ||
} | ||
|
||
// Error sends error information to the console | ||
func (l *FileLog) Error(v ...interface{}) { | ||
l.message(ErrorLevel, v...) | ||
} | ||
|
||
// Errorf sends error information to the console | ||
func (l *FileLog) Errorf(format string, v ...interface{}) { | ||
l.messagef(ErrorLevel, format, v...) | ||
} | ||
|
||
func (l *FileLog) message(level int, v ...interface{}) { | ||
v = append([]interface{}{getLevelName(level)}, v...) | ||
log.Println(v...) | ||
} | ||
|
||
func (l *FileLog) messagef(level int, format string, v ...interface{}) { | ||
log.Printf(getLevelName(level)+" "+format+"\n", v...) | ||
} | ||
|
||
// Verify interface | ||
var ( | ||
_ Log = &FileLog{} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/creativeprojects/resticprofile/clog" | ||
"github.com/creativeprojects/resticprofile/remote" | ||
) | ||
|
||
func setupRemoteLogger(flags commandLineFlags) { | ||
client := remote.NewClient(flags.parentPort) | ||
logger := clog.NewRemoteLog(client) | ||
logger.SetPrefix("elevated user: ") | ||
clog.SetDefaultLogger(logger) | ||
} | ||
|
||
func setupFileLogger(flags commandLineFlags) (*clog.FileLog, error) { | ||
logger, err := clog.NewFileLog(flags.logFile) | ||
if err != nil { | ||
return nil, err | ||
} | ||
clog.SetDefaultLogger(logger) | ||
setupVerbosity(flags, logger) | ||
return logger, nil | ||
} | ||
|
||
func setupConsoleLogger(flags commandLineFlags) { | ||
logger := clog.NewConsoleLog() | ||
if flags.theme != "" { | ||
logger.SetTheme(flags.theme) | ||
} | ||
if flags.noAnsi { | ||
logger.Colorize(false) | ||
} | ||
clog.SetDefaultLogger(logger) | ||
setupVerbosity(flags, logger) | ||
} | ||
|
||
func setupVerbosity(flags commandLineFlags, logger clog.Verbosity) { | ||
if flags.quiet && flags.verbose { | ||
coin := "" | ||
if randomBool() { | ||
coin = "verbose" | ||
flags.quiet = false | ||
} else { | ||
coin = "quiet" | ||
flags.verbose = false | ||
} | ||
clog.Warningf("you specified -quiet (-q) and -verbose (-v) at the same time. So let's flip a coin! and selection is ... %s.", coin) | ||
} | ||
if flags.quiet { | ||
logger.Quiet() | ||
} | ||
if flags.verbose { | ||
logger.Verbose() | ||
} | ||
} |
Oops, something went wrong.