Improve logging

This commit is contained in:
Andrea Fazzi 2021-12-10 12:06:04 +01:00
commit d333a21175
7 changed files with 24 additions and 20 deletions

View file

@ -1,4 +1,4 @@
package youtube
package downloader
import (
"time"

View file

@ -7,9 +7,9 @@ import (
"os/exec"
"path/filepath"
"git.andreafazzi.eu/andrea/youtube-dl-service/downloader"
"git.andreafazzi.eu/andrea/youtube-dl-service/logger"
"git.andreafazzi.eu/andrea/youtube-dl-service/task"
"git.andreafazzi.eu/andrea/youtube-dl-service/youtube"
lib "github.com/kkdai/youtube/v2"
)
@ -22,17 +22,17 @@ func NewYoutubeDlDownloader(url string, commandName string) *YoutubeDlDownloader
return &YoutubeDlDownloader{url, commandName}
}
func (d *YoutubeDlDownloader) GetVideo() (*youtube.Video, error) {
func (d *YoutubeDlDownloader) GetVideo() (*downloader.Video, error) {
return d.getVideoJson(d.Url)
}
func (d *YoutubeDlDownloader) StartDownload(video *youtube.Video, tasks task.Tasks) {
func (d *YoutubeDlDownloader) StartDownload(video *downloader.Video, tasks task.Tasks) {
tasks[video.ID] = &task.Task{
ID: video.ID,
Status: task.StatusDownloading,
}
logger.Info("Download of video ID %s STARTED.", video.ID)
logger.Info(fmt.Sprintf("Download of video ID %s STARTED.", video.ID))
cmd := exec.CommandContext(context.Background(), d.CommandName, "-o", filepath.Join("data/", video.ID+filepath.Ext(video.Filename)), "--newline", d.Url)
stdout, err := cmd.StdoutPipe()
if err != nil {
@ -62,13 +62,13 @@ func (d *YoutubeDlDownloader) ExtractVideoID() (string, error) {
return lib.ExtractVideoID(d.Url)
}
func (d *YoutubeDlDownloader) getVideoJson(url string) (*youtube.Video, error) {
func (d *YoutubeDlDownloader) getVideoJson(url string) (*downloader.Video, error) {
cmd := exec.CommandContext(context.Background(), d.CommandName, "-j", url)
output, err := cmd.CombinedOutput()
if err != nil {
return nil, fmt.Errorf("Command terminated with %w: %s", err, output)
}
result := new(youtube.Video)
result := new(downloader.Video)
err = json.Unmarshal(output, &result)
if err != nil {
return nil, err

View file

@ -3,19 +3,22 @@ package logger
import "log"
const (
Disabled = 0
Disabled = iota
InfoLevel
WarningLevel
DebugLevel
)
var loggerLevel int
var loggerLevel int = InfoLevel
func SetLevel(level int) {
loggerLevel = level
}
func Info(format string, v ...interface{}) {
func Info(v ...interface{}) {
if loggerLevel == Disabled {
return
}
if loggerLevel == InfoLevel {
log.Println(v...)
}

View file

@ -7,9 +7,9 @@ import (
"path/filepath"
"strconv"
"git.andreafazzi.eu/andrea/youtube-dl-service/downloader"
youtube_dl "git.andreafazzi.eu/andrea/youtube-dl-service/downloader/youtube-dl"
"git.andreafazzi.eu/andrea/youtube-dl-service/task"
"git.andreafazzi.eu/andrea/youtube-dl-service/youtube"
youtube_dl "git.andreafazzi.eu/andrea/youtube-dl-service/youtube/youtube-dl"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
@ -21,7 +21,7 @@ func init() {
tasks = make(task.Tasks, 0)
}
func download(c *gin.Context, downloader youtube.Downloader) error {
func download(c *gin.Context, downloader downloader.Downloader) error {
video, err := downloader.GetVideo()
if err != nil {
return err

View file

@ -8,9 +8,9 @@ import (
"strings"
"testing"
"git.andreafazzi.eu/andrea/youtube-dl-service/downloader"
"git.andreafazzi.eu/andrea/youtube-dl-service/logger"
"git.andreafazzi.eu/andrea/youtube-dl-service/task"
"git.andreafazzi.eu/andrea/youtube-dl-service/youtube"
"github.com/gin-gonic/gin"
"github.com/remogatto/prettytest"
)
@ -21,16 +21,17 @@ type testSuite struct {
}
func TestRunner(t *testing.T) {
gin.SetMode(gin.ReleaseMode)
logger.SetLevel(logger.Disabled)
prettytest.Run(
t,
new(testSuite),
)
}
func (t *testSuite) BeforeAll() {
gin.SetMode(gin.ReleaseMode)
logger.SetLevel(logger.Disabled)
}
// Test the creation of a new task. A new task is created with a POST
// request to the endpoint.
func (t *testSuite) TestCreateTask() {
@ -73,7 +74,7 @@ func (t *testSuite) TestGetTask() {
t.Equal("AVIBLFl28vo", task.ID)
}
func postTask(ytUrl string) (*youtube.Video, error) {
func postTask(ytUrl string) (*downloader.Video, error) {
// Set form values
form := url.Values{}
form.Add("url", ytUrl)
@ -96,7 +97,7 @@ func postTask(ytUrl string) (*youtube.Video, error) {
createTask(c)
// Read the response and assert it.
video := new(youtube.Video)
video := new(downloader.Video)
if err := json.Unmarshal(rr.Body.Bytes(), &video); err != nil {
return nil, err