Skip to content

Commit

Permalink
simplifies the log module
Browse files Browse the repository at this point in the history
  • Loading branch information
kpym committed Aug 20, 2021
1 parent 44b82bb commit 0858a59
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 51 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)_.
8 changes: 4 additions & 4 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
Expand Down
57 changes: 15 additions & 42 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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...)
}
8 changes: 4 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 0858a59

Please sign in to comment.