From 0858a596835fd3768f989e1690d20cf40fc1b76d Mon Sep 17 00:00:00 2001 From: kpym Date: Fri, 20 Aug 2021 09:05:27 +0200 Subject: [PATCH] simplifies the log module --- README.md | 21 +++++++++++++++++++- app/app.go | 8 ++++---- log/log.go | 57 ++++++++++++++---------------------------------------- main.go | 8 ++++---- 4 files changed, 43 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 045dc08..a96c86d 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,26 @@ Examples: > lol main.tex personal.sty images/img*.pdf > cat main.tex | lol -c lualatex -o out.pdf ``` +## Configuration + +As `lol` use [viper](https://github.com/spf13/viper) the parameters can be provided not only by flags but also be read from config file (`lol.yaml`, `lol.toml`, `lol.json`...) or/and from environment variables (starting with `LOL_`). + +### Using config file + +You can provide all default values for flags in a config `lol` file in the current folder. +For example if your project needs `xelatex` and use `imgs/logo.png` you can save the following `lol.yaml` in the current folder +```yaml +Compiler: xelatex +Patterns: + - imgs/logo.png +``` + +### Using environment variables + +If you wan to provide global default values you can set an environment variable. +For example if you want by default to use `ytotech` service you can set `LOL_SERVICE=ytotech`. + ## License -[MIT](LICENSE) for this code _(but all used libraries may have different licence)_. +[MIT](LICENSE) for this code _(but all used libraries may have different licences)_. diff --git a/app/app.go b/app/app.go index 31ae813..6f73fd0 100644 --- a/app/app.go +++ b/app/app.go @@ -153,7 +153,7 @@ func GetParameters(params *builder.Parameters) error { fi, err := os.Stdin.Stat() if err == nil { params.PipedMain = ((fi.Mode() & os.ModeCharDevice) == 0) && (fi.Mode()&os.ModeNamedPipe != 0) - params.Log.Debugf("Piped input: %v, Stdin mode: %v.\n", params.PipedMain, fi.Mode()) + params.Log.Debug("Piped input: %v, Stdin mode: %v.", params.PipedMain, fi.Mode()) } // Get the patterns params.Patterns = append(pflag.Args(), params.Patterns...) @@ -199,7 +199,7 @@ func GetFiles(params builder.Parameters) (builder.Files, error) { params.Log.Debug("Read the main file from stdin.") filedata, err = io.ReadAll(os.Stdin) } else { - params.Log.Debugf("Read the main file from %s.\n", params.Main) + params.Log.Debug("Read the main file from %s.", params.Main) filedata, err = os.ReadFile(params.Main) } files[params.Main] = filedata @@ -227,9 +227,9 @@ func GetFiles(params builder.Parameters) (builder.Files, error) { filedata, err = os.ReadFile(fname) if err == nil { files[uname] = filedata - params.Log.Debugf("File %s (%d bytes) added to the list.", uname, len(filedata)) + params.Log.Debug("File %s (%d bytes) added to the list.", uname, len(filedata)) } else { - params.Log.Debugf("Probleam reading support file (we skip it): %s.", fname) + params.Log.Debug("Probleam reading support file (we skip it): %s.", fname) } } } diff --git a/log/log.go b/log/log.go index 7e063ca..17abadf 100644 --- a/log/log.go +++ b/log/log.go @@ -21,12 +21,9 @@ const ( // Logger is a very basic log interface. type Logger interface { - Error(msg string) - Errorf(msg string, a ...interface{}) - Info(msg string) - Infof(msg string, a ...interface{}) - Debug(msg string) - Debugf(msg string, a ...interface{}) + Error(msg string, a ...interface{}) + Info(msg string, a ...interface{}) + Debug(msg string, a ...interface{}) } // log type variable satisfy Logger interface. @@ -101,50 +98,26 @@ var ( debugcolor = color.New(color.FgCyan, color.Bold).SprintFunc() ) -// Error method for Logger interface. -func (l *log) Error(msg string) { - if l.level > ErrorLevel { - return - } - fmt.Fprintln(l.out, errcolor("ERROR:"), msgcolor(msg)) -} - -// Errorf method for Logger interface. -func (l *log) Errorf(msg string, a ...interface{}) { - if l.level > ErrorLevel { +// printLog prints to l.out if level is high enough +// level and tag specify the type (INFO, ERROR, DEBUG). +func printLog(l *log, level Level, tag, msg string, a ...interface{}) { + if l.level > level { return } - fmt.Fprintf(l.out, errcolor("ERROR:")+" "+msgcolor(msg), a...) + fmt.Fprintln(l.out, tag, fmt.Sprintf(msgcolor(msg), a...)) } -// Info method for Logger interface. -func (l *log) Info(msg string) { - if l.level > InfoLevel { - return - } - fmt.Fprintln(l.out, infocolor("INFO:"), msgcolor(msg)) +// Error method for Logger interface. +func (l *log) Error(msg string, a ...interface{}) { + printLog(l, ErrorLevel, errcolor("ERROR:"), msg, a...) } // Infof method for Logger interface. -func (l *log) Infof(msg string, a ...interface{}) { - if l.level > InfoLevel { - return - } - fmt.Fprintf(l.out, infocolor("INFO:")+" "+msgcolor(msg), a...) +func (l *log) Info(msg string, a ...interface{}) { + printLog(l, InfoLevel, infocolor("INFO:"), msg, a...) } // Debug method for Logger interface. -func (l *log) Debug(msg string) { - if l.level > DebugLevel { - return - } - fmt.Fprintln(l.out, debugcolor("DEBUG:"), msgcolor(msg)) -} - -// Debugf method for Logger interface. -func (l *log) Debugf(msg string, a ...interface{}) { - if l.level > DebugLevel { - return - } - fmt.Fprintf(l.out, debugcolor("DEBUG:")+" "+msgcolor(msg), a...) +func (l *log) Debug(msg string, a ...interface{}) { + printLog(l, DebugLevel, debugcolor("DEBUG:"), msg, a...) } diff --git a/main.go b/main.go index 3a8ef18..ac07b98 100644 --- a/main.go +++ b/main.go @@ -45,19 +45,19 @@ func main() { compiler = laton.NewBuilder() } req := builder.Request{Parameters: params, Files: files} - params.Log.Infof("Send request with the following parameters:\n%s\n", req.String()) + params.Log.Info("Send request with the following parameters:\n%s", req.String()) sendtime := time.Now() pdf, err := compiler.BuildPDF(req) - params.Log.Infof("Answer received in %1.1f seconds.\n", time.Since(sendtime).Seconds()) + params.Log.Info("Answer received in %1.1f seconds.", time.Since(sendtime).Seconds()) check(params.Log, err) // write the pdf if params.Output != "" { - params.Log.Infof("Write %s.\n", params.Output) + params.Log.Info("Write %s.", params.Output) err = os.WriteFile(params.Output, pdf, 0644) check(params.Log, err) } else { - params.Log.Infof("Write to stdout.\n") + params.Log.Info("Write to stdout.") _, err = os.Stdout.Write(pdf) check(params.Log, err) }