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

fix: client log is not recorded #141

Merged
merged 1 commit into from
Jul 6, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bin/

*.data
*.db
*.log

.DS_Store

Expand Down
24 changes: 7 additions & 17 deletions pkg/download/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func NewDownloader(cfg *DownloaderConfig) *Downloader {
}
}

logPanic(cfg.StorageDir)
logPanic(filepath.Join(cfg.StorageDir, "logs"))
return d
}

Expand Down Expand Up @@ -640,22 +640,12 @@ func (d *Downloader) doContinue(task *Task) (err error) {

// redirect stderr to log file, when panic happened log it
func logPanic(logDir string) {
fp := filepath.Join(logDir, "crash.log")
// if log file not exists, create it
var f *os.File
if _, err := os.Stat(fp); err != nil {
if !os.IsNotExist(err) {
return
}
f, err = os.Create(fp)
if err != nil {
return
}
} else {
f, err = os.OpenFile(fp, os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
return
}
if err := util.CreateDirIfNotExist(logDir); err != nil {
return
}
f, err := os.Create(filepath.Join(logDir, "crash.log"))
if err != nil {
return
}
paniclog.RedirectStderr(f)
}
Expand Down
17 changes: 4 additions & 13 deletions pkg/util/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package util
import (
"errors"
"fmt"
"io"
"os"
syspath "path"
"strings"
Expand Down Expand Up @@ -83,17 +82,9 @@ func GetSingleDir(paths []string) string {
return dir
}

// check directory is empty
func isEmpty(name string) (bool, error) {
f, err := os.Open(name)
if err != nil {
return false, err
}
defer f.Close()

_, err = f.Readdirnames(1) // Or f.Readdir(1)
if err == io.EOF {
return true, nil
func CreateDirIfNotExist(dir string) error {
if _, err := os.Stat(dir); os.IsNotExist(err) {
return os.MkdirAll(dir, 0o777)
}
return false, err // Either not empty or error, suits both cases
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SettingView extends GetView<SettingController> {
debounceSave({bool needRestart = false}) {
var completer = Completer<void>();
timer?.cancel();
timer = Timer(const Duration(milliseconds: 500), () {
timer = Timer(const Duration(milliseconds: 1000), () {
appController
.saveConfig()
.then(completer.complete)
Expand Down
14 changes: 9 additions & 5 deletions ui/flutter/lib/util/log_util.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart';

import 'util.dart';

final logger = Logger(
printer: PrettyPrinter(),
filter: ProductionFilter(),
printer: LogfmtPrinter(),
output: buildOutput(),
);

buildOutput() {
// if is debug mode, output to console
// if is debug mode, don't log to file
if (!kDebugMode && Util.isDesktop()) {
return FileOutput(file: File('log.txt'));
const logDirPath = 'logs';
var logDir = Directory(logDirPath);
if (!logDir.existsSync()) {
logDir.createSync();
}
return FileOutput(file: File('$logDirPath/client.log'));
}
return null;
}
2 changes: 1 addition & 1 deletion ui/flutter/lib/util/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ void showErrorMessage(msg) {
Get.snackbar(title, ((msg as dynamic).message as Result).msg!);
return;
}
Get.snackbar(title, msg);
Get.snackbar(title, msg.toString());
}

void showMessage(title, msg) {
Expand Down