Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Change all outPutLog to slog.Error or slog.Info #628

Merged
merged 2 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions src/internal/config_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func initialConfig(dir string) (toggleDotFileBool bool, toggleFooter bool, first

toggleDotFileData, err := os.ReadFile(variable.ToggleDotFile)
if err != nil {
outPutLog("Error while reading toggleDotFile data error:", err)
slog.Error("Error while reading toggleDotFile data error:", "error", err)
}
if string(toggleDotFileData) == "true" {
toggleDotFileBool = true
Expand All @@ -61,7 +61,7 @@ func initialConfig(dir string) (toggleDotFileBool bool, toggleFooter bool, first

toggleFooterData, err := os.ReadFile(variable.ToggleFooter)
if err != nil {
outPutLog("Error while reading toggleFooter data error:", err)
slog.Error("Error while reading toggleFooter data error:", "error", err)
}
if string(toggleFooterData) == "true" {
toggleFooter = true
Expand All @@ -77,7 +77,7 @@ func initialConfig(dir string) (toggleDotFileBool bool, toggleFooter bool, first
if Config.Metadata {
et, err = exiftool.NewExiftool()
if err != nil {
outPutLog("Initial model function init exiftool error", err)
slog.Error("Error while initial model function init exiftool error", "error", err)
}
}

Expand Down Expand Up @@ -248,29 +248,29 @@ func LoadAllDefaultConfig(content embed.FS) {

temp, err := content.ReadFile(variable.EmbedHotkeysFile)
if err != nil {
outPutLog("Error reading from embed file:", err)
slog.Error("Error reading from embed file:", "error", err)
return
}
HotkeysTomlString = string(temp)

temp, err = content.ReadFile(variable.EmbedConfigFile)
if err != nil {
outPutLog("Error reading from embed file:", err)
slog.Error("Error reading from embed file:", "error", err)
return
}
ConfigTomlString = string(temp)

temp, err = content.ReadFile(variable.EmbedThemeCatppuccinFile)
if err != nil {
outPutLog("Error reading from embed file:", err)
slog.Error("Error reading from embed file:", "error", err)
return
}
DefaultThemeString = string(temp)

// Todo : We should not return here, and have a default value for this
currentThemeVersion, err := os.ReadFile(variable.ThemeFileVersion)
if err != nil && !os.IsNotExist(err) {
outPutLog("Error reading from file:", err)
slog.Error("Error reading from file:", "error", err)
return
}

Expand All @@ -279,7 +279,7 @@ func LoadAllDefaultConfig(content embed.FS) {
if os.IsNotExist(err) {
err := os.MkdirAll(variable.ThemeFolder, 0755)
if err != nil {
outPutLog("error create theme directory", err)
slog.Error("Error creating theme directory", "error", err)
return
}
} else if string(currentThemeVersion) == variable.CurrentVersion {
Expand All @@ -288,7 +288,7 @@ func LoadAllDefaultConfig(content embed.FS) {

files, err := content.ReadDir(variable.EmbedThemeDir)
if err != nil {
outPutLog("error read theme directory from embed", err)
slog.Error("Error reading theme directory from embed", "error", err)
return
}

Expand All @@ -299,31 +299,31 @@ func LoadAllDefaultConfig(content embed.FS) {
// This will not break in windows. This is a relative path for Embed FS. It uses "/" only
src, err := content.ReadFile(variable.EmbedThemeDir + "/" + file.Name())
if err != nil {
outPutLog("error read theme file from embed", err)
slog.Error("Error reading theme file from embed", "error", err)
return
}

file, err := os.Create(filepath.Join(variable.ThemeFolder, file.Name()))
if err != nil {
outPutLog("error create theme file from embed", err)
slog.Error("Error creating theme file from embed", "error", err)
return
}
defer file.Close()
_, err = file.Write(src)
if err != nil {
slog.Error("error writing theme file from embed", "error", err)
slog.Error("Error writing theme file from embed", "error", err)
return
}
}

// Prevent failure for first time app run by making sure parent directories exists
if err = os.MkdirAll(filepath.Dir(variable.ThemeFileVersion), 0755); err != nil {
slog.Error("error creating theme file parent directory", "error", err)
slog.Error("Error creating theme file parent directory", "error", err)
return
}

err = os.WriteFile(variable.ThemeFileVersion, []byte(variable.CurrentVersion), 0644)
if err != nil {
slog.Error("error writing theme file version", "error", err)
slog.Error("Error writing theme file version", "error", err)
}
}
3 changes: 2 additions & 1 deletion src/internal/file_operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -153,7 +154,7 @@ func trashMacOrLinux(src string) error {
err = trash.Trash(src)
}
if err != nil {
outPutLog("Delete single item function move file to trash can error", err)
slog.Error("Error while delete single item function move file to trash can", "error", err)
}
return err
}
Expand Down
5 changes: 3 additions & 2 deletions src/internal/file_operations_compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package internal
import (
"archive/zip"
"io"
"log/slog"
"os"
"path/filepath"

Expand All @@ -19,7 +20,7 @@ func zipSource(source, target string) error {
totalFiles, err := countFiles(source)

if err != nil {
outPutLog("Zip file count files error: ", err)
slog.Error("Error while zip file count files ", "error", err)
}

p := process{
Expand Down Expand Up @@ -108,7 +109,7 @@ func zipSource(source, target string) error {
})

if err != nil {
outPutLog("Error while zip file:", err)
slog.Error("Error while zip file", "error", err)
p.state = failure
message.processNewState = p
channel <- message
Expand Down
11 changes: 6 additions & 5 deletions src/internal/file_operations_extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/zip"
"fmt"
"io"
"log/slog"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -68,7 +69,7 @@ func extractCompressFile(src, dest string) error {
if len(channel) < 5 {
channel <- message
}
outPutLog(fmt.Sprintf("Error extracting %s: %v", src, err))
slog.Error("Error extracting", "path", src, "error", err)
return err
}

Expand All @@ -92,7 +93,7 @@ func unzip(src, dest string) error {
}
defer func() {
if err := r.Close(); err != nil {
outPutLog(fmt.Sprintf("Error closing zip reader: %v", err))
slog.Error("Error closing zip reader", "error", err)
}
}()

Expand Down Expand Up @@ -125,7 +126,7 @@ func unzip(src, dest string) error {
}
defer func() {
if err := rc.Close(); err != nil {
outPutLog(fmt.Sprintf("Error closing file reader: %v", err))
slog.Error("Error closing file reader", "error", err)
}
}()

Expand Down Expand Up @@ -161,7 +162,7 @@ func unzip(src, dest string) error {
}
defer func() {
if err := outFile.Close(); err != nil {
outPutLog(fmt.Sprintf("Error closing output file %s: %v", path, err))
slog.Error("Error closing output file", "path", path, "error", err)
}
}()

Expand Down Expand Up @@ -189,7 +190,7 @@ func unzip(src, dest string) error {
p.state = failure
message.processNewState = p
channel <- message
outPutLog(fmt.Sprintf("Error extracting %s: %v", f.Name, err))
slog.Error("Error extracting", "path", f.Name, "error", err)
p.done++
continue
}
Expand Down
23 changes: 9 additions & 14 deletions src/internal/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func returnFocusType(focusPanel focusPanelType) filePanelFocusType {
func returnDirElement(location string, displayDotFile bool, sortOptions sortOptionsModelData) (directoryElement []element) {
dirEntries, err := os.ReadDir(location)
if err != nil {
outPutLog("Return folder element function error", err)
slog.Error("Error while return folder element function", "error", err)
return directoryElement
}

Expand Down Expand Up @@ -95,11 +95,11 @@ func returnDirElement(location string, displayDotFile bool, sortOptions sortOpti
// No need of early return, we only call len() on filesI, so nil would
// just result in 0
if err != nil {
outPutLog("Error when reading directory during sort", err)
slog.Error("Error when reading directory during sort", "error", err)
}
filesJ, err := os.ReadDir(filepath.Join(location, dirEntries[j].Name()))
if err != nil {
outPutLog("Error when reading directory during sort", err)
slog.Error("Error when reading directory during sort", "error", err)
}
return len(filesI) < len(filesJ) != reversed
} else {
Expand Down Expand Up @@ -134,7 +134,7 @@ func returnDirElementBySearchString(location string, displayDotFile bool, search

items, err := os.ReadDir(location)
if err != nil {
outPutLog("Return folder element function error", err)
slog.Error("Error while return folder element function", "error", err)
return []element{}
}

Expand Down Expand Up @@ -196,11 +196,6 @@ func arrayContains(s []string, str string) bool {
return false
}

// Todo : Eventually we will replace all calls to direct slog calls
func outPutLog(values ...interface{}) {
slog.Info(fmt.Sprintln(values...))
}

// Todo : Eventually we want to remove all such usage that can result in app exiting abruptly
func LogAndExit(msg string, values ...any) {
slog.Error(msg, values...)
Expand Down Expand Up @@ -324,7 +319,7 @@ func (m *model) returnMetaData() {
}

if err != nil {
outPutLog("Return meta data function get file state error", err)
slog.Error("Error while return meta data function get file state", "error", err)
}

if fileInfo.IsDir() {
Expand All @@ -341,7 +336,7 @@ func (m *model) returnMetaData() {

checkIsSymlinked, err := os.Lstat(filePath)
if err != nil {
outPutLog("err when getting file info", err)
slog.Error("Error when getting file info", "error", err)
return
}

Expand All @@ -351,7 +346,7 @@ func (m *model) returnMetaData() {

for _, fileInfo := range fileInfos {
if fileInfo.Err != nil {
outPutLog("Return meta data function error", fileInfo, fileInfo.Err)
slog.Error("Error while return metadata function", "fileInfo", fileInfo, "error", fileInfo.Err)
continue
}

Expand All @@ -370,7 +365,7 @@ func (m *model) returnMetaData() {
// Calculate MD5 checksum
checksum, err := calculateMD5Checksum(filePath)
if err != nil {
outPutLog("Error calculating MD5 checksum", err)
slog.Error("Error calculating MD5 checksum", "error", err)
} else {
md5Data := [2]string{"MD5Checksum", checksum}
m.fileMetaData.metaData = append(m.fileMetaData.metaData, md5Data)
Expand Down Expand Up @@ -407,7 +402,7 @@ func dirSize(path string) int64 {
var size int64
err := filepath.WalkDir(path, func(_ string, entry os.DirEntry, err error) error {
if err != nil {
outPutLog("Dir size function error", err)
slog.Error("Dir size function error", "error", err)
}
if !entry.IsDir() {
info, err := entry.Info()
Expand Down
7 changes: 4 additions & 3 deletions src/internal/get_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package internal

import (
"encoding/json"
"log/slog"
"os"
"path/filepath"
"slices"
Expand Down Expand Up @@ -55,7 +56,7 @@ func getPinnedDirectories() []directory {

jsonData, err := os.ReadFile(variable.PinnedFile)
if err != nil {
outPutLog("Read superfile data error", err)
slog.Error("Error while read superfile data", "error", err)
return directories
}

Expand All @@ -74,7 +75,7 @@ func getPinnedDirectories() []directory {
}
// If the data is in neither format, log the error
} else {
outPutLog("Error parsing pinned data", err)
slog.Error("Error parsing pinned data", "error", err)
}
return directories
}
Expand All @@ -84,7 +85,7 @@ func getExternalMediaFolders() (disks []directory) {
parts, err := disk.Partitions(true)

if err != nil {
outPutLog("Error while getting external media: ", err)
slog.Error("Error while getting external media: ", "error", err)
return disks
}
for _, disk := range parts {
Expand Down
Loading
Loading