Skip to content

Commit

Permalink
Move log file to .wakatime folder
Browse files Browse the repository at this point in the history
  • Loading branch information
gandarez committed Apr 12, 2023
1 parent 9ca1a18 commit ac3b25a
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ Please fill out the template below to report a bug.
**Logs**:

<!--
Paste related logs from your ~/.wakatime.log file.
Paste related logs from your ~/.wakatime/wakatime.log file.
-->
2 changes: 1 addition & 1 deletion TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
2 changes: 1 addition & 1 deletion USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 11 additions & 3 deletions cmd/logfile/logfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
14 changes: 7 additions & 7 deletions cmd/logfile/logfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
},
},
Expand Down Expand Up @@ -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,
},
},
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
22 changes: 15 additions & 7 deletions pkg/ini/ini.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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()))
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pkg/offline/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit ac3b25a

Please sign in to comment.