diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index abdc80a8..e161acc3 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -19,5 +19,5 @@ Please fill out the template below to report a bug. **Logs**: diff --git a/TROUBLESHOOTING.md b/TROUBLESHOOTING.md index bac5adae..1aa3878a 100644 --- a/TROUBLESHOOTING.md +++ b/TROUBLESHOOTING.md @@ -4,7 +4,7 @@ First, read [How to debug WakaTime plugins][faq debug plugins]. Set `debug=true` in your `~/.wakatime.cfg` file to enable verbose logging. -The common wakatime-cli program logs to your user `$HOME` directory `~/.wakatime.log`. +The common wakatime-cli program logs to your user `$HOME` directory `~/.wakatime/wakatime.log`. Each plugin also has its own log file: diff --git a/USAGE.md b/USAGE.md index f4bea2f2..287b62cd 100644 --- a/USAGE.md +++ b/USAGE.md @@ -81,7 +81,7 @@ some/submodule/name = new project name | ssl_certs_file | Path to a CA certs file. By default, uses bundled Letsencrypt CA cert along with system ca certs. | _filepath_ | | | timeout | Connection timeout in seconds when communicating with the api. | _int_ | `120` | | hostname | Optional name of local machine. By default, auto-detects the local machine’s hostname. | _string_ | | -| log_file | Optional log file path. | _filepath_ | `~/.wakatime.log` | +| log_file | Optional log file path. | _filepath_ | `~/.wakatime/wakatime.log` | ### Project Map Section diff --git a/cmd/logfile/logfile.go b/cmd/logfile/logfile.go index bff35e5f..a049a5b2 100644 --- a/cmd/logfile/logfile.go +++ b/cmd/logfile/logfile.go @@ -11,7 +11,10 @@ import ( "github.com/spf13/viper" ) -const defaultFile = ".wakatime.log" +const ( + defaultFile = "wakatime.log" + defaultFolder = ".wakatime" +) // Params contains log file parameters. type Params struct { @@ -49,12 +52,17 @@ func LoadParams(v *viper.Viper) (Params, error) { return params, nil } - home, err := ini.WakaHomeDir() + home, hometype, err := ini.WakaHomeDir() if err != nil { return Params{}, fmt.Errorf("failed getting user's home directory: %s", err) } - params.File = filepath.Join(home, defaultFile) + switch hometype { + case ini.WakaHomeTypeEnvVar: + params.File = filepath.Join(home, defaultFile) + default: + params.File = filepath.Join(home, defaultFolder, defaultFile) + } return params, nil } diff --git a/cmd/logfile/logfile_test.go b/cmd/logfile/logfile_test.go index 50812532..12e2c8bf 100644 --- a/cmd/logfile/logfile_test.go +++ b/cmd/logfile/logfile_test.go @@ -13,14 +13,14 @@ import ( ) func TestLoadParams(t *testing.T) { - tmpFile, err := os.CreateTemp(t.TempDir(), "wakatime.log") + tmpFile, err := os.CreateTemp(t.TempDir(), "") require.NoError(t, err) defer tmpFile.Close() dir, _ := filepath.Split(tmpFile.Name()) - logFile, err := os.Create(filepath.Join(dir, ".wakatime.log")) + logFile, err := os.Create(filepath.Join(dir, "wakatime.log")) require.NoError(t, err) defer logFile.Close() @@ -41,14 +41,14 @@ func TestLoadParams(t *testing.T) { "log file and verbose set": { ViperDebug: true, Expected: logfile.Params{ - File: filepath.Join(home, ".wakatime.log"), + File: filepath.Join(home, ".wakatime", "wakatime.log"), Verbose: true, }, }, "log file and verbose from config": { ViperDebugConfig: true, Expected: logfile.Params{ - File: filepath.Join(home, ".wakatime.log"), + File: filepath.Join(home, ".wakatime", "wakatime.log"), Verbose: true, }, }, @@ -76,18 +76,18 @@ func TestLoadParams(t *testing.T) { "log file from WAKATIME_HOME": { EnvVar: dir, Expected: logfile.Params{ - File: filepath.Join(dir, ".wakatime.log"), + File: filepath.Join(dir, "wakatime.log"), }, }, "log file from home dir": { Expected: logfile.Params{ - File: filepath.Join(home, ".wakatime.log"), + File: filepath.Join(home, ".wakatime", "wakatime.log"), }, }, "log to stdout": { ViperToStdout: true, Expected: logfile.Params{ - File: filepath.Join(home, ".wakatime.log"), + File: filepath.Join(home, ".wakatime", "wakatime.log"), ToStdout: true, }, }, diff --git a/cmd/root.go b/cmd/root.go index 57d8eab8..c540a29a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -154,8 +154,8 @@ func setFlags(cmd *cobra.Command, v *viper.Viper) { " remote file, this local file will be used for stats and just"+ " the value of --entity is sent with the heartbeat.", ) - flags.String("log-file", "", "Optional log file. Defaults to '~/.wakatime.log'.") - flags.String("logfile", "", "(deprecated) Optional log file. Defaults to '~/.wakatime.log'.") + flags.String("log-file", "", "Optional log file. Defaults to '~/.wakatime/wakatime.log'.") + flags.String("logfile", "", "(deprecated) Optional log file. Defaults to '~/.wakatime/wakatime.log'.") flags.Bool("log-to-stdout", false, "If enabled, logs will go to stdout. Will overwrite logfile configs.") flags.Bool( "no-ssl-verify", diff --git a/cmd/run.go b/cmd/run.go index 1d9cf774..18daa4dd 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -268,6 +268,19 @@ func runCmd(v *viper.Viper, verbose bool, sendDiagsOnErrors bool, cmd cmdFn) int resetLogs() + // if s, ok := err.(string); ok { + // // if panicked with "invalid freelist page" error, then try to delete db file. + // if strings.Contains(s, "invalid freelist page") { + // log.Warnf("invalid freelist page error. Trying to delete corrupted db file.") + // if err := os.Remove(offline.QueueFilepath()); err != nil { + // log.Warnf("failed to delete db file: %s", err) + // } else { + // log.Warnf("db file deleted. Restarting...") + // os.Exit(exitcode.Restart) + // } + // } + // } + diags := diagnostics{ OriginalError: err, Panicked: true, diff --git a/pkg/ini/ini.go b/pkg/ini/ini.go index e53d9aa4..4dcc638f 100644 --- a/pkg/ini/ini.go +++ b/pkg/ini/ini.go @@ -19,6 +19,14 @@ import ( "gopkg.in/ini.v1" ) +type WakaHomeType int + +const ( + WakaHomeTypeUnknown WakaHomeType = iota + WakaHomeTypeEnvVar + WakaHomeTypeOSDir +) + const ( // defaultFile is the name of the default wakatime config file. defaultFile = ".wakatime.cfg" @@ -137,7 +145,7 @@ func FilePath(v *viper.Viper) (string, error) { return p, nil } - home, err := WakaHomeDir() + home, _, err := WakaHomeDir() if err != nil { return "", fmt.Errorf("failed getting user's home directory: %s", err) } @@ -172,7 +180,7 @@ func InternalFilePath(v *viper.Viper) (string, error) { return p, nil } - home, err := WakaHomeDir() + home, _, err := WakaHomeDir() if err != nil { return "", fmt.Errorf("failed getting user's home directory: %s", err) } @@ -181,12 +189,12 @@ func InternalFilePath(v *viper.Viper) (string, error) { } // WakaHomeDir returns the current user's home directory. -func WakaHomeDir() (string, error) { +func WakaHomeDir() (string, WakaHomeType, error) { home, exists := os.LookupEnv("WAKATIME_HOME") if exists && home != "" { home, err := homedir.Expand(home) if err == nil { - return home, nil + return home, WakaHomeTypeEnvVar, nil } log.Warnf("failed to expand WAKATIME_HOME filepath: %s", err) @@ -198,7 +206,7 @@ func WakaHomeDir() (string, error) { } if home != "" { - return home, nil + return home, WakaHomeTypeOSDir, nil } u, err := user.LookupId(strconv.Itoa(os.Getuid())) @@ -207,10 +215,10 @@ func WakaHomeDir() (string, error) { } if u.HomeDir != "" { - return u.HomeDir, nil + return u.HomeDir, WakaHomeTypeOSDir, nil } - return "", fmt.Errorf("could not determine wakatime home dir") + return "", WakaHomeTypeUnknown, fmt.Errorf("could not determine wakatime home dir") } // mutexClock is used to implement mutex.Clock interface. diff --git a/pkg/offline/offline.go b/pkg/offline/offline.go index 855b7c60..cef87f0c 100644 --- a/pkg/offline/offline.go +++ b/pkg/offline/offline.go @@ -89,7 +89,7 @@ func WithQueue(filepath string) heartbeat.HandleOption { // the user's $HOME folder cannot be detected, it defaults to the // current directory. func QueueFilepath() (string, error) { - home, err := ini.WakaHomeDir() + home, _, err := ini.WakaHomeDir() if err != nil { return dbFilename, fmt.Errorf("failed getting user's home directory, defaulting to current directory: %s", err) }