More betterify logging
This commit is contained in:
37
main.go
37
main.go
@@ -15,9 +15,9 @@ import (
|
||||
const deliminer = ","
|
||||
const SourceColor = Purple
|
||||
const TargetColor = Yellow
|
||||
const ErrorColor = URed
|
||||
const ErrorColor = Red
|
||||
const ImportantColor = BRed
|
||||
const DefaultColor = White
|
||||
const DefaultColor = Reset
|
||||
const PathColor = Green
|
||||
|
||||
var FileRegex, _ = regexp.Compile(`sync\.ya?ml$`)
|
||||
@@ -31,7 +31,7 @@ func main() {
|
||||
|
||||
if *debug {
|
||||
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
||||
logFile, err := os.Create("cln.log")
|
||||
logFile, err := os.Create(programName + ".log")
|
||||
if err != nil {
|
||||
LogError("Error creating log file: %v", err)
|
||||
os.Exit(1)
|
||||
@@ -94,7 +94,7 @@ func main() {
|
||||
}
|
||||
}()
|
||||
|
||||
var instructionsDone int32
|
||||
var instructionsDone int32 = 0
|
||||
var wg sync.WaitGroup
|
||||
for {
|
||||
instruction, ok := <-instructions
|
||||
@@ -108,8 +108,7 @@ func main() {
|
||||
wg.Add(1)
|
||||
err := <-status
|
||||
if err != nil {
|
||||
LogError("Failed parsing instruction %s due to %v",
|
||||
FormatSourcePath(instruction.String()), err)
|
||||
LogError("Failed processing instruction: %v", err)
|
||||
}
|
||||
atomic.AddInt32(&instructionsDone, 1)
|
||||
wg.Done()
|
||||
@@ -136,7 +135,7 @@ func ReadFromFilesRecursively(input string, output chan *LinkInstruction, status
|
||||
|
||||
workdir, _ := os.Getwd()
|
||||
input = NormalizePath(input, workdir)
|
||||
LogInfo("Reading input from files recursively starting in %s%s%s", PathColor, input, DefaultColor)
|
||||
LogInfo("Reading input from files recursively starting in %s", FormatPathValue(input))
|
||||
|
||||
files := make(chan string, 128)
|
||||
fileStatus := make(chan error)
|
||||
@@ -171,19 +170,21 @@ func ReadFromFilesRecursively(input string, output chan *LinkInstruction, status
|
||||
defer wg.Done()
|
||||
LogInfo(file)
|
||||
file = NormalizePath(file, workdir)
|
||||
LogInfo("Processing file: %s%s%s", PathColor, file, DefaultColor)
|
||||
LogInfo("Processing file: %s", FormatPathValue(file))
|
||||
|
||||
// This "has" to be done because instructions are resolved in relation to cwd
|
||||
fileDir := FileRegex.FindStringSubmatch(file)
|
||||
if fileDir == nil {
|
||||
LogError("Failed to extract directory from %s%s%s", SourceColor, file, DefaultColor)
|
||||
LogError("Failed to extract directory from %s", FormatSourcePath(file))
|
||||
return
|
||||
}
|
||||
LogInfo("Changing directory to %s%s%s (for %s%s%s)", PathColor, fileDir[1], DefaultColor, PathColor, file, DefaultColor)
|
||||
LogInfo("Changing directory to %s (for %s)",
|
||||
FormatPathValue(fileDir[1]),
|
||||
FormatPathValue(file))
|
||||
err := os.Chdir(fileDir[1])
|
||||
if err != nil {
|
||||
LogError("Failed to change directory to %s%s%s: %v",
|
||||
SourceColor, fileDir[1], DefaultColor, err)
|
||||
LogError("Failed to change directory to %s: %v",
|
||||
FormatSourcePath(fileDir[1]), err)
|
||||
return
|
||||
}
|
||||
ReadFromFile(file, output, status, false)
|
||||
@@ -200,15 +201,15 @@ func ReadFromFile(input string, output chan *LinkInstruction, status chan error,
|
||||
}
|
||||
|
||||
input = NormalizePath(input, filepath.Dir(input))
|
||||
LogInfo("Reading input from file: %s%s%s", PathColor, input, DefaultColor)
|
||||
LogInfo("Reading input from file: %s", FormatPathValue(input))
|
||||
|
||||
// Check if this is a YAML file
|
||||
if IsYAMLFile(input) {
|
||||
LogInfo("Parsing as YAML file")
|
||||
instructions, err := ParseYAMLFile(input, filepath.Dir(input))
|
||||
if err != nil {
|
||||
LogError("Failed to parse YAML file %s%s%s: %s%+v%s",
|
||||
SourceColor, input, DefaultColor, ErrorColor, err, DefaultColor)
|
||||
LogError("Failed to parse YAML file %s: %v",
|
||||
FormatSourcePath(input), err)
|
||||
status <- err
|
||||
return
|
||||
}
|
||||
@@ -231,8 +232,7 @@ func ReadFromArgs(output chan *LinkInstruction, status chan error) {
|
||||
for _, arg := range flag.Args() {
|
||||
instruction, err := ParseInstruction(arg, workdir)
|
||||
if err != nil {
|
||||
LogError("Error parsing arg: %s'%s'%s, error: %v",
|
||||
SourceColor, arg, DefaultColor, err)
|
||||
LogError("Error parsing arg '%s': %v", arg, err)
|
||||
continue
|
||||
}
|
||||
output <- &instruction
|
||||
@@ -251,8 +251,7 @@ func ReadFromStdin(output chan *LinkInstruction, status chan error) {
|
||||
line := scanner.Text()
|
||||
instruction, err := ParseInstruction(line, workdir)
|
||||
if err != nil {
|
||||
LogError("Error parsing line: %s'%s'%s, error: %v",
|
||||
SourceColor, line, DefaultColor, err)
|
||||
LogError("Error parsing line '%s': %v", line, err)
|
||||
continue
|
||||
}
|
||||
output <- &instruction
|
||||
|
Reference in New Issue
Block a user