From 800f0b386b4f6c3dabb3b4d72983d115f1bbe75d Mon Sep 17 00:00:00 2001 From: Anis Ahmad Date: Mon, 12 Mar 2018 21:06:37 +0600 Subject: [PATCH] Printing debug info only if -v mentioned --- img.go | 2 ++ main.go | 15 +++++++-------- pdf.go | 6 +++++- util.go | 7 +++++++ 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/img.go b/img.go index 265e66e..f37237c 100644 --- a/img.go +++ b/img.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "log" "strconv" "strings" @@ -14,6 +15,7 @@ var sizeHasSet, merginHasSet = false, false // func addImageToPdf(inputPath string, outputPath string, imagePath string, pageNum int, xPos float64, yPos float64, iwidth float64) error { func addImage(filePath string, c *creator.Creator) error { + debugInfo(fmt.Sprintf("Adding image: %s", filePath)) img, err := creator.NewImageFromFile(filePath) if err != nil { diff --git a/main.go b/main.go index 5ed467d..c830896 100644 --- a/main.go +++ b/main.go @@ -13,7 +13,7 @@ import ( ) var size, margin string -var scaleH, scaleW bool +var scaleH, scaleW, verbose bool const ( DefaultSize = "IMG-SIZE" @@ -21,13 +21,12 @@ const ( ) func init() { - // Debug log level. - unicommon.SetLogger(unicommon.NewConsoleLogger(unicommon.LogLevelDebug)) flag.StringVarP(&size, "size", "s", DefaultSize, "Resize image pages to print size. One of A4, A3, Legal or Letter.") flag.StringVarP(&margin, "margin", "m", DefaultMargin, "Comma separated numbers for left, right, top, bottm side margin in inch.") flag.BoolVarP(&scaleW, "scale-width", "w", false, "Scale Image to page width. Only if --size specified.") flag.BoolVarP(&scaleH, "scale-height", "h", false, "Scale Image to page height. Only if --size specified.") + flag.BoolVarP(&verbose, "verbose", "v", false, "Display debug info.") flag.Usage = func() { fmt.Println("Requires at least 3 arguments: output_path and 2 input paths (and optional page numbers)") @@ -44,6 +43,10 @@ func main() { os.Exit(0) } + if verbose { + unicommon.SetLogger(unicommon.NewConsoleLogger(unicommon.LogLevelDebug)) + } + outputPath := args[0] inputPaths := []string{} inputPages := [][]int{} @@ -79,7 +82,7 @@ func main() { os.Exit(1) } - fmt.Printf("Complete, see output file: %s\n", outputPath) + debugInfo(fmt.Sprintf("Complete, see output file: %s", outputPath)) } func mergePdf(inputPaths []string, inputPages [][]int, outputPath string) error { @@ -105,10 +108,6 @@ func mergePdf(inputPaths []string, inputPages [][]int, outputPath string) error err = addPdfPages(f, inputPages[i], c) } else if fileType[:6] == "image/" { err = addImage(inputPath, c) - - fmt.Println("%+v", pageMargin) - fmt.Println("%+v", pageSize) - } else { err = errors.New("Unsupported type:" + inputPath) } diff --git a/pdf.go b/pdf.go index bb9db2b..8a8122e 100644 --- a/pdf.go +++ b/pdf.go @@ -2,7 +2,9 @@ package main import ( "errors" + "fmt" "io" + "os" "github.com/unidoc/unidoc/pdf/creator" pdf "github.com/unidoc/unidoc/pdf/model" @@ -33,13 +35,14 @@ func getReader(rs io.ReadSeeker) (*pdf.PdfReader, error) { return pdfReader, nil } -func addPdfPages(file io.ReadSeeker, pages []int, c *creator.Creator) error { +func addPdfPages(file *os.File, pages []int, c *creator.Creator) error { pdfReader, err := getReader(file) if err != nil { return err } if len(pages) > 0 { + debugInfo(fmt.Sprintf("Adding all pages of PDF: %s", file.Name())) for _, pageNo := range pages { if page, pageErr := pdfReader.GetPage(pageNo); pageErr != nil { return pageErr @@ -60,6 +63,7 @@ func addPdfPages(file io.ReadSeeker, pages []int, c *creator.Creator) error { return err } + debugInfo(fmt.Sprintf("Adding page %d of PDF: %s", pageNum, file.Name())) if err = c.AddPage(page); err != nil { return err } diff --git a/util.go b/util.go index bc87257..076b171 100644 --- a/util.go +++ b/util.go @@ -1,6 +1,7 @@ package main import ( + "log" "net/http" "os" "reflect" @@ -43,3 +44,9 @@ func getFileType(file *os.File) (string, error) { return http.DetectContentType(buffer), nil } } + +func debugInfo(message string) { + if verbose { + log.Println(message) + } +}