Skip to content

Commit 800f0b3

Browse files
committed
Printing debug info only if -v mentioned
1 parent 86ae9ef commit 800f0b3

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

img.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"log"
56
"strconv"
67
"strings"
@@ -14,6 +15,7 @@ var sizeHasSet, merginHasSet = false, false
1415

1516
// func addImageToPdf(inputPath string, outputPath string, imagePath string, pageNum int, xPos float64, yPos float64, iwidth float64) error {
1617
func addImage(filePath string, c *creator.Creator) error {
18+
debugInfo(fmt.Sprintf("Adding image: %s", filePath))
1719

1820
img, err := creator.NewImageFromFile(filePath)
1921
if err != nil {

main.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,20 @@ import (
1313
)
1414

1515
var size, margin string
16-
var scaleH, scaleW bool
16+
var scaleH, scaleW, verbose bool
1717

1818
const (
1919
DefaultSize = "IMG-SIZE"
2020
DefaultMargin = "1,1,1,1"
2121
)
2222

2323
func init() {
24-
// Debug log level.
25-
unicommon.SetLogger(unicommon.NewConsoleLogger(unicommon.LogLevelDebug))
2624

2725
flag.StringVarP(&size, "size", "s", DefaultSize, "Resize image pages to print size. One of A4, A3, Legal or Letter.")
2826
flag.StringVarP(&margin, "margin", "m", DefaultMargin, "Comma separated numbers for left, right, top, bottm side margin in inch.")
2927
flag.BoolVarP(&scaleW, "scale-width", "w", false, "Scale Image to page width. Only if --size specified.")
3028
flag.BoolVarP(&scaleH, "scale-height", "h", false, "Scale Image to page height. Only if --size specified.")
29+
flag.BoolVarP(&verbose, "verbose", "v", false, "Display debug info.")
3130

3231
flag.Usage = func() {
3332
fmt.Println("Requires at least 3 arguments: output_path and 2 input paths (and optional page numbers)")
@@ -44,6 +43,10 @@ func main() {
4443
os.Exit(0)
4544
}
4645

46+
if verbose {
47+
unicommon.SetLogger(unicommon.NewConsoleLogger(unicommon.LogLevelDebug))
48+
}
49+
4750
outputPath := args[0]
4851
inputPaths := []string{}
4952
inputPages := [][]int{}
@@ -79,7 +82,7 @@ func main() {
7982
os.Exit(1)
8083
}
8184

82-
fmt.Printf("Complete, see output file: %s\n", outputPath)
85+
debugInfo(fmt.Sprintf("Complete, see output file: %s", outputPath))
8386
}
8487

8588
func mergePdf(inputPaths []string, inputPages [][]int, outputPath string) error {
@@ -105,10 +108,6 @@ func mergePdf(inputPaths []string, inputPages [][]int, outputPath string) error
105108
err = addPdfPages(f, inputPages[i], c)
106109
} else if fileType[:6] == "image/" {
107110
err = addImage(inputPath, c)
108-
109-
fmt.Println("%+v", pageMargin)
110-
fmt.Println("%+v", pageSize)
111-
112111
} else {
113112
err = errors.New("Unsupported type:" + inputPath)
114113
}

pdf.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package main
22

33
import (
44
"errors"
5+
"fmt"
56
"io"
7+
"os"
68

79
"github.com/unidoc/unidoc/pdf/creator"
810
pdf "github.com/unidoc/unidoc/pdf/model"
@@ -33,13 +35,14 @@ func getReader(rs io.ReadSeeker) (*pdf.PdfReader, error) {
3335
return pdfReader, nil
3436
}
3537

36-
func addPdfPages(file io.ReadSeeker, pages []int, c *creator.Creator) error {
38+
func addPdfPages(file *os.File, pages []int, c *creator.Creator) error {
3739
pdfReader, err := getReader(file)
3840
if err != nil {
3941
return err
4042
}
4143

4244
if len(pages) > 0 {
45+
debugInfo(fmt.Sprintf("Adding all pages of PDF: %s", file.Name()))
4346
for _, pageNo := range pages {
4447
if page, pageErr := pdfReader.GetPage(pageNo); pageErr != nil {
4548
return pageErr
@@ -60,6 +63,7 @@ func addPdfPages(file io.ReadSeeker, pages []int, c *creator.Creator) error {
6063
return err
6164
}
6265

66+
debugInfo(fmt.Sprintf("Adding page %d of PDF: %s", pageNum, file.Name()))
6367
if err = c.AddPage(page); err != nil {
6468
return err
6569
}

util.go

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"log"
45
"net/http"
56
"os"
67
"reflect"
@@ -43,3 +44,9 @@ func getFileType(file *os.File) (string, error) {
4344
return http.DetectContentType(buffer), nil
4445
}
4546
}
47+
48+
func debugInfo(message string) {
49+
if verbose {
50+
log.Println(message)
51+
}
52+
}

0 commit comments

Comments
 (0)