-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adis Durakovic
committed
Mar 13, 2024
1 parent
6b8ec56
commit 1e99a33
Showing
6 changed files
with
179 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package internal | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"github.com/adrg/xdg" | ||
"github.com/charmbracelet/log" | ||
) | ||
|
||
func getPath() string { | ||
return filepath.Join(xdg.CacheHome, "resticity") | ||
} | ||
|
||
func getFile(t string) string { | ||
d := time.Now().Format("2006-01-02") | ||
f := filepath.Join(getPath(), t+"_"+d+".log") | ||
return f | ||
} | ||
|
||
func appendToFile(f string, m ChanMsg) { | ||
|
||
if _, err := os.Stat(f); os.IsNotExist(err) { | ||
_, err := os.Create(f) | ||
if err != nil { | ||
log.Error("filelogger: create "+f, "error", err) | ||
return | ||
} | ||
} | ||
|
||
d, err := json.Marshal(m) | ||
if err != nil { | ||
log.Error("filelogger: marshal "+f, "error", err) | ||
return | ||
} | ||
|
||
if err := WriteFile(f, []byte(d)); err != nil { | ||
log.Error("filelogger: write "+f, "error", err) | ||
} | ||
|
||
} | ||
|
||
func WriteFile(name string, data []byte) error { | ||
f, err := os.OpenFile(name, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer f.Close() | ||
|
||
if _, err = f.WriteString(string(data) + "\n"); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func NewFileLogger(outputChan *chan ChanMsg, errorChan *chan ChanMsg) { | ||
if _, err := os.Stat(getPath()); os.IsNotExist(err) { | ||
os.Mkdir(getPath(), 0755) | ||
} | ||
log.Info("filelogger", "path", getPath()) | ||
for { | ||
select { | ||
case o := <-*outputChan: | ||
if os.Getenv("LOG_TO_FILE") == "true" { | ||
f := getFile("logs") | ||
appendToFile(f, o) | ||
} | ||
break | ||
case e := <-*errorChan: | ||
f := getFile("error") | ||
appendToFile(f, e) | ||
break | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.