Skip to content

Commit 59c5a66

Browse files
author
benma's agent
committed
backend: include rotated logs in export
1 parent 823bfdc commit 59c5a66

File tree

4 files changed

+108
-15
lines changed

4 files changed

+108
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Add a dropdown on the "Receiver address" input in the send screen to select an account
55
- Add feedback link to guide and about settings
66
- Move active currencies to top of currency dropdown
7+
- Export logs now includes rotated log file (log.txt.1) when present
78
- Android: fix connectivity misdetection when switching between WIFI and cellular network.
89
- Android: dropped support for Android versions lower than 7.
910
- Sort backups from newest to oldest in manage backups

backend/backend.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package backend
1818
import (
1919
"encoding/hex"
2020
"fmt"
21-
"io"
2221
"net/http"
2322
"net/url"
2423
"os"
@@ -1102,28 +1101,20 @@ func (backend *Backend) ExportLogs() error {
11021101
}
11031102
backend.log.Infof("Export logs to %s.", path)
11041103

1105-
file, err := os.Create(path)
1104+
exportFile, err := os.Create(path)
11061105
if err != nil {
11071106
backend.log.WithError(err).Error("error creating new log file")
11081107
return err
11091108
}
11101109
logFilePath := filepath.Join(utilConfig.AppDir(), "log.txt")
11111110

1112-
existingLogFile, err := os.Open(logFilePath)
1113-
if err != nil {
1114-
backend.log.WithError(err).Error("error opening existing log file")
1111+
if err := logging.WriteCombinedLog(exportFile, logFilePath); err != nil {
1112+
backend.log.WithError(err).Error("error copying existing logs to new file")
1113+
_ = exportFile.Close()
11151114
return err
11161115
}
1117-
1118-
defer func() {
1119-
if err := existingLogFile.Close(); err != nil {
1120-
backend.log.WithError(err).Error("error closing existing log file")
1121-
}
1122-
}()
1123-
1124-
_, err = io.Copy(file, existingLogFile)
1125-
if err != nil {
1126-
backend.log.WithError(err).Error("error copying existing log to new file")
1116+
if err := exportFile.Close(); err != nil {
1117+
backend.log.WithError(err).Error("error closing new log file")
11271118
return err
11281119
}
11291120
backend.log.Infof("Exported logs copied to %s.", path)

util/logging/export.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package logging
4+
5+
import (
6+
"errors"
7+
"fmt"
8+
"io"
9+
"os"
10+
"path/filepath"
11+
)
12+
13+
// WriteCombinedLog writes the current log file and, if it exists, the rotated-out log file
14+
// (with ".1" appended) into dst, in chronological order (rotated file first).
15+
func WriteCombinedLog(dst io.Writer, logFilePath string) error {
16+
paths := []string{}
17+
18+
rotatedLogFilePath := logFilePath + rotatedSuffix
19+
if _, err := os.Stat(rotatedLogFilePath); err == nil {
20+
paths = append(paths, rotatedLogFilePath)
21+
} else if err != nil && !errors.Is(err, os.ErrNotExist) {
22+
return err
23+
}
24+
25+
if _, err := os.Stat(logFilePath); err == nil {
26+
paths = append(paths, logFilePath)
27+
} else if err != nil && !errors.Is(err, os.ErrNotExist) {
28+
return err
29+
}
30+
31+
if len(paths) == 0 {
32+
return fmt.Errorf("no log files found at %s", logFilePath)
33+
}
34+
35+
for idx, path := range paths {
36+
if idx > 0 {
37+
if _, err := io.WriteString(dst, "\n\n"); err != nil {
38+
return err
39+
}
40+
}
41+
if _, err := io.WriteString(dst, fmt.Sprintf("----- %s -----\n", filepath.Base(path))); err != nil {
42+
return err
43+
}
44+
file, err := os.Open(path)
45+
if err != nil {
46+
return err
47+
}
48+
_, copyErr := io.Copy(dst, file)
49+
closeErr := file.Close()
50+
if copyErr != nil {
51+
return copyErr
52+
}
53+
if closeErr != nil {
54+
return closeErr
55+
}
56+
}
57+
return nil
58+
}

util/logging/export_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
3+
package logging
4+
5+
import (
6+
"bytes"
7+
"os"
8+
"path/filepath"
9+
"testing"
10+
11+
"github.com/stretchr/testify/require"
12+
)
13+
14+
func TestWriteCombinedLog(t *testing.T) {
15+
tempdir := t.TempDir()
16+
17+
logfile := filepath.Join(tempdir, "log.txt")
18+
require.NoError(t, os.WriteFile(logfile, []byte("new\n"), 0600))
19+
require.NoError(t, os.WriteFile(logfile+".1", []byte("old\n"), 0600))
20+
21+
var buf bytes.Buffer
22+
require.NoError(t, WriteCombinedLog(&buf, logfile))
23+
require.Equal(t, "----- log.txt.1 -----\nold\n\n\n----- log.txt -----\nnew\n", buf.String())
24+
}
25+
26+
func TestWriteCombinedLogNoRotatedFile(t *testing.T) {
27+
tempdir := t.TempDir()
28+
29+
logfile := filepath.Join(tempdir, "log.txt")
30+
require.NoError(t, os.WriteFile(logfile, []byte("new\n"), 0600))
31+
32+
var buf bytes.Buffer
33+
require.NoError(t, WriteCombinedLog(&buf, logfile))
34+
require.Equal(t, "----- log.txt -----\nnew\n", buf.String())
35+
}
36+
37+
func TestWriteCombinedLogNoFiles(t *testing.T) {
38+
tempdir := t.TempDir()
39+
logfile := filepath.Join(tempdir, "log.txt")
40+
41+
var buf bytes.Buffer
42+
require.Error(t, WriteCombinedLog(&buf, logfile))
43+
}

0 commit comments

Comments
 (0)