Skip to content

Commit

Permalink
Printing debug info only if -v mentioned
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaxray committed Mar 12, 2018
1 parent 86ae9ef commit 800f0b3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
2 changes: 2 additions & 0 deletions img.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"
"strconv"
"strings"
Expand All @@ -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 {
Expand Down
15 changes: 7 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ import (
)

var size, margin string
var scaleH, scaleW bool
var scaleH, scaleW, verbose bool

const (
DefaultSize = "IMG-SIZE"
DefaultMargin = "1,1,1,1"
)

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)")
Expand All @@ -44,6 +43,10 @@ func main() {
os.Exit(0)
}

if verbose {
unicommon.SetLogger(unicommon.NewConsoleLogger(unicommon.LogLevelDebug))
}

outputPath := args[0]
inputPaths := []string{}
inputPages := [][]int{}
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
6 changes: 5 additions & 1 deletion pdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"errors"
"fmt"
"io"
"os"

"github.com/unidoc/unidoc/pdf/creator"
pdf "github.com/unidoc/unidoc/pdf/model"
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"log"
"net/http"
"os"
"reflect"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 800f0b3

Please sign in to comment.