mirror of
https://github.com/jkl1337/duplicacy.git
synced 2026-01-02 11:44:45 -06:00
Make log.Printf print to Duplicacy's logging system
This commit is contained in:
@@ -7,10 +7,12 @@ package duplicacy
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"log"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
"regexp"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -161,6 +163,32 @@ func logf(level int, logID string, format string, v ...interface{}) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set up logging for libraries that Duplicacy depends on. They can call 'log.Printf("[ID] message")'
|
||||||
|
// to produce logs in Duplicacy's format
|
||||||
|
type Logger struct {
|
||||||
|
formatRegex *regexp.Regexp
|
||||||
|
}
|
||||||
|
|
||||||
|
func (logger *Logger) Write(line []byte) (n int, err error) {
|
||||||
|
n = len(line)
|
||||||
|
for len(line) > 0 && line[len(line) - 1] == '\n' {
|
||||||
|
line = line[:len(line) - 1]
|
||||||
|
}
|
||||||
|
matched := logger.formatRegex.FindStringSubmatch(string(line))
|
||||||
|
if matched != nil {
|
||||||
|
LOG_INFO(matched[1], "%s", matched[2])
|
||||||
|
} else {
|
||||||
|
LOG_INFO("LOG_DEFAULT", "%s", line)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log.SetFlags(0)
|
||||||
|
log.SetOutput(&Logger{ formatRegex: regexp.MustCompile(`^\[(.+)\]\s*(.+)`) })
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
duplicacyExitCode = 100
|
duplicacyExitCode = 100
|
||||||
otherExitCode = 101
|
otherExitCode = 101
|
||||||
|
|||||||
Reference in New Issue
Block a user