mirror of
https://github.com/Belphemur/CBZOptimizer.git
synced 2025-10-13 20:18:52 +02:00
feat: integrate zerolog for enhanced logging across multiple components
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/belphemur/CBZOptimizer/v2/pkg/converter/constant"
|
||||
converterrors "github.com/belphemur/CBZOptimizer/v2/pkg/converter/errors"
|
||||
"github.com/oliamb/cutter"
|
||||
"github.com/rs/zerolog/log"
|
||||
"golang.org/x/exp/slices"
|
||||
_ "golang.org/x/image/webp"
|
||||
)
|
||||
@@ -53,8 +54,17 @@ func (converter *Converter) PrepareConverter() error {
|
||||
}
|
||||
|
||||
func (converter *Converter) ConvertChapter(chapter *manga.Chapter, quality uint8, split bool, progress func(message string, current uint32, total uint32)) (*manga.Chapter, error) {
|
||||
log.Debug().
|
||||
Str("chapter", chapter.FilePath).
|
||||
Int("pages", len(chapter.Pages)).
|
||||
Uint8("quality", quality).
|
||||
Bool("split", split).
|
||||
Int("max_goroutines", runtime.NumCPU()).
|
||||
Msg("Starting chapter conversion")
|
||||
|
||||
err := converter.PrepareConverter()
|
||||
if err != nil {
|
||||
log.Error().Str("chapter", chapter.FilePath).Err(err).Msg("Failed to prepare converter")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -73,6 +83,12 @@ func (converter *Converter) ConvertChapter(chapter *manga.Chapter, quality uint8
|
||||
var pages []*manga.Page
|
||||
var totalPages = uint32(len(chapter.Pages))
|
||||
|
||||
log.Debug().
|
||||
Str("chapter", chapter.FilePath).
|
||||
Int("total_pages", len(chapter.Pages)).
|
||||
Int("worker_count", maxGoroutines).
|
||||
Msg("Initialized conversion worker pool")
|
||||
|
||||
// Start the worker pool
|
||||
go func() {
|
||||
for page := range pagesChan {
|
||||
@@ -165,6 +181,15 @@ func (converter *Converter) ConvertChapter(chapter *manga.Chapter, quality uint8
|
||||
var aggregatedError error = nil
|
||||
if len(errList) > 0 {
|
||||
aggregatedError = errors.Join(errList...)
|
||||
log.Debug().
|
||||
Str("chapter", chapter.FilePath).
|
||||
Int("error_count", len(errList)).
|
||||
Msg("Conversion completed with errors")
|
||||
} else {
|
||||
log.Debug().
|
||||
Str("chapter", chapter.FilePath).
|
||||
Int("pages_converted", len(pages)).
|
||||
Msg("Conversion completed successfully")
|
||||
}
|
||||
|
||||
slices.SortFunc(pages, func(a, b *manga.Page) int {
|
||||
@@ -175,7 +200,13 @@ func (converter *Converter) ConvertChapter(chapter *manga.Chapter, quality uint8
|
||||
})
|
||||
chapter.Pages = pages
|
||||
|
||||
log.Debug().
|
||||
Str("chapter", chapter.FilePath).
|
||||
Int("final_page_count", len(pages)).
|
||||
Msg("Pages sorted and chapter updated")
|
||||
|
||||
runtime.GC()
|
||||
log.Debug().Str("chapter", chapter.FilePath).Msg("Garbage collection completed")
|
||||
|
||||
return chapter, aggregatedError
|
||||
}
|
||||
@@ -183,12 +214,20 @@ func (converter *Converter) ConvertChapter(chapter *manga.Chapter, quality uint8
|
||||
func (converter *Converter) cropImage(img image.Image) ([]image.Image, error) {
|
||||
bounds := img.Bounds()
|
||||
height := bounds.Dy()
|
||||
width := bounds.Dx()
|
||||
|
||||
numParts := height / converter.cropHeight
|
||||
if height%converter.cropHeight != 0 {
|
||||
numParts++
|
||||
}
|
||||
|
||||
log.Debug().
|
||||
Int("original_width", width).
|
||||
Int("original_height", height).
|
||||
Int("crop_height", converter.cropHeight).
|
||||
Int("num_parts", numParts).
|
||||
Msg("Starting image cropping for page splitting")
|
||||
|
||||
parts := make([]image.Image, numParts)
|
||||
|
||||
for i := 0; i < numParts; i++ {
|
||||
@@ -197,6 +236,12 @@ func (converter *Converter) cropImage(img image.Image) ([]image.Image, error) {
|
||||
partHeight = height - i*converter.cropHeight
|
||||
}
|
||||
|
||||
log.Debug().
|
||||
Int("part_index", i).
|
||||
Int("part_height", partHeight).
|
||||
Int("y_offset", i*converter.cropHeight).
|
||||
Msg("Cropping image part")
|
||||
|
||||
part, err := cutter.Crop(img, cutter.Config{
|
||||
Width: bounds.Dx(),
|
||||
Height: partHeight,
|
||||
@@ -204,45 +249,119 @@ func (converter *Converter) cropImage(img image.Image) ([]image.Image, error) {
|
||||
Mode: cutter.TopLeft,
|
||||
})
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Int("part_index", i).
|
||||
Err(err).
|
||||
Msg("Failed to crop image part")
|
||||
return nil, fmt.Errorf("error cropping part %d: %v", i+1, err)
|
||||
}
|
||||
|
||||
parts[i] = part
|
||||
|
||||
log.Debug().
|
||||
Int("part_index", i).
|
||||
Int("cropped_width", part.Bounds().Dx()).
|
||||
Int("cropped_height", part.Bounds().Dy()).
|
||||
Msg("Image part cropped successfully")
|
||||
}
|
||||
|
||||
log.Debug().
|
||||
Int("total_parts", len(parts)).
|
||||
Msg("Image cropping completed")
|
||||
|
||||
return parts, nil
|
||||
}
|
||||
|
||||
func (converter *Converter) checkPageNeedsSplit(page *manga.Page, splitRequested bool) (bool, image.Image, string, error) {
|
||||
log.Debug().
|
||||
Uint16("page_index", page.Index).
|
||||
Bool("split_requested", splitRequested).
|
||||
Int("page_size", len(page.Contents.Bytes())).
|
||||
Msg("Analyzing page for splitting")
|
||||
|
||||
reader := bytes.NewBuffer(page.Contents.Bytes())
|
||||
img, format, err := image.Decode(reader)
|
||||
if err != nil {
|
||||
log.Debug().Uint16("page_index", page.Index).Err(err).Msg("Failed to decode page image")
|
||||
return false, nil, format, err
|
||||
}
|
||||
|
||||
bounds := img.Bounds()
|
||||
height := bounds.Dy()
|
||||
width := bounds.Dx()
|
||||
|
||||
log.Debug().
|
||||
Uint16("page_index", page.Index).
|
||||
Int("width", width).
|
||||
Int("height", height).
|
||||
Str("format", format).
|
||||
Int("max_height", converter.maxHeight).
|
||||
Int("webp_max_height", webpMaxHeight).
|
||||
Msg("Page dimensions analyzed")
|
||||
|
||||
if height >= webpMaxHeight && !splitRequested {
|
||||
log.Debug().
|
||||
Uint16("page_index", page.Index).
|
||||
Int("height", height).
|
||||
Int("webp_max", webpMaxHeight).
|
||||
Msg("Page too tall for WebP format, would be ignored")
|
||||
return false, img, format, converterrors.NewPageIgnored(fmt.Sprintf("page %d is too tall [max: %dpx] to be converted to webp format", page.Index, webpMaxHeight))
|
||||
}
|
||||
return height >= converter.maxHeight && splitRequested, img, format, nil
|
||||
|
||||
needsSplit := height >= converter.maxHeight && splitRequested
|
||||
log.Debug().
|
||||
Uint16("page_index", page.Index).
|
||||
Bool("needs_split", needsSplit).
|
||||
Msg("Page splitting decision made")
|
||||
|
||||
return needsSplit, img, format, nil
|
||||
}
|
||||
|
||||
func (converter *Converter) convertPage(container *manga.PageContainer, quality uint8) (*manga.PageContainer, error) {
|
||||
log.Debug().
|
||||
Uint16("page_index", container.Page.Index).
|
||||
Str("format", container.Format).
|
||||
Bool("to_be_converted", container.IsToBeConverted).
|
||||
Uint8("quality", quality).
|
||||
Msg("Converting page")
|
||||
|
||||
// Fix WebP format detection (case insensitive)
|
||||
if container.Format == "webp" || container.Format == "WEBP" {
|
||||
log.Debug().
|
||||
Uint16("page_index", container.Page.Index).
|
||||
Msg("Page already in WebP format, skipping conversion")
|
||||
container.Page.Extension = ".webp"
|
||||
return container, nil
|
||||
}
|
||||
if !container.IsToBeConverted {
|
||||
log.Debug().
|
||||
Uint16("page_index", container.Page.Index).
|
||||
Msg("Page marked as not to be converted, skipping")
|
||||
return container, nil
|
||||
}
|
||||
|
||||
log.Debug().
|
||||
Uint16("page_index", container.Page.Index).
|
||||
Uint8("quality", quality).
|
||||
Msg("Encoding page to WebP format")
|
||||
|
||||
converted, err := converter.convert(container.Image, uint(quality))
|
||||
if err != nil {
|
||||
log.Error().
|
||||
Uint16("page_index", container.Page.Index).
|
||||
Err(err).
|
||||
Msg("Failed to convert page to WebP")
|
||||
return nil, err
|
||||
}
|
||||
container.SetConverted(converted, ".webp");
|
||||
|
||||
container.SetConverted(converted, ".webp")
|
||||
|
||||
log.Debug().
|
||||
Uint16("page_index", container.Page.Index).
|
||||
Int("original_size", len(container.Page.Contents.Bytes())).
|
||||
Int("converted_size", len(converted.Bytes())).
|
||||
Msg("Page conversion completed")
|
||||
|
||||
return container, nil
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user