Skip to content

Commit

Permalink
top: fix wrong assembling of path to logfile
Browse files Browse the repository at this point in the history
  • Loading branch information
lesovsky committed Feb 11, 2019
1 parent 9b148b4 commit 2e1d14a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
// ProgramVersion is the version of this program
ProgramVersion = "0.6"
// ProgramRelease is release number of this program
ProgramRelease = "0"
ProgramRelease = "1"
// ProgramIssuesUrl is the public URL for posting issues, bug reports and asking questions
ProgramIssuesUrl = "https://github.com/lesovsky/pgcenter/issues"
)
Expand Down
4 changes: 3 additions & 1 deletion doc/Changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pgcenter (0.6.0) unstable; urgency=low
pgcenter (0.6.1) unstable; urgency=low
* top: fix srong assebling of path to logfile (issue #55)

pgcenter (0.6.0) unstable; urgency=low
* added goreleser support
* top: fix wrong handling of group cancel/terminate mask
* implemented GoReport recommendations
Expand Down
2 changes: 1 addition & 1 deletion top/auxinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func showAux(auxtype auxType) func(g *gocui.Gui, _ *gocui.View) error {
}

pgLog.Size = 0
pgLog.Path, _ = readLogPath()
pgLog.Path = readLogPath()

// Check the logfile isn't an empty
if info, err := os.Stat(pgLog.Path); err == nil && info.Size() == 0 {
Expand Down
11 changes: 2 additions & 9 deletions top/logtail.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,8 @@ func (l *postgresLogfile) ReOpen() error {
return err
}

if l.Path, err = readLogPath(); err != nil {
return err
}

if err = l.Open(); err != nil {
return err
}

return nil
l.Path = readLogPath()
return l.Open()
}

// Read methos reads logfile until required number of newlines aren't collected
Expand Down
55 changes: 30 additions & 25 deletions top/pglog.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ func showPgLog(g *gocui.Gui, _ *gocui.View) error {
return nil
}

var currentLogfile, error = readLogPath()
var currentLogfile = readLogPath()
if currentLogfile == "" {
printCmdline(g, "Can't determine log file: %s", error)
printCmdline(g, "Can't assemble absolute path to log file")
return nil
}

Expand All @@ -49,32 +49,37 @@ func showPgLog(g *gocui.Gui, _ *gocui.View) error {
}

// Get an absolute path of current Postgres log.
func readLogPath() (string, error) {
func readLogPath() string {
var logfileRealpath, pgDatadir string

// An easiest way to get logfile is using pg_current_logfile() function, but it's available since PG 10.
conn.QueryRow(stat.PgGetCurrentLogfileQuery).Scan(&logfileRealpath)

if logfileRealpath != "" {
// Even pg_current_logfile() might return relative path
if logfileRealpath[0] != byte('/') {
if !strings.HasPrefix(logfileRealpath, "/") {
conn.QueryRow(stat.PgGetSingleSettingQuery, "data_directory").Scan(&pgDatadir)
logfileRealpath = pgDatadir + "/" + logfileRealpath
}
return logfileRealpath, nil
return logfileRealpath
}

// if we're here, it means we are connected to Postgres that has no pg_current_logfile() function (9.6 and older).
// if we're here, it means we are connected to old Postgres that has no pg_current_logfile() function (9.6 and older).
// Anyway, after September 2021 years, all Postgres 9.x will become EOL and code below could be deleted.
var pgLogdir, pgLogfile, logfileFallback string
return lookupPostgresLogfile()
}

// lookupPostgresLogfiles tries to assemble in a hard way an absolute path to Postgres logfile
func lookupPostgresLogfile() (absLogfilePath string) {
var pgDatadir, pgLogdir, pgLogfile, pgLogfileFallback string
conn.QueryRow(stat.PgGetSingleSettingQuery, "data_directory").Scan(&pgDatadir)
conn.QueryRow(stat.PgGetSingleSettingQuery, "log_directory").Scan(&pgLogdir)
conn.QueryRow(stat.PgGetSingleSettingQuery, "log_filename").Scan(&pgLogfile)

if pgLogdir[0] == byte('/') {
logfileRealpath = pgLogdir + "/" + pgLogfile // absolute path
if strings.HasPrefix(pgLogdir, "/") {
absLogfilePath = pgLogdir + "/" + pgLogfile // absolute path
} else {
logfileRealpath = pgDatadir + "/" + pgLogdir + "/" + pgLogfile // relative to DATADIR path
absLogfilePath = pgDatadir + "/" + pgLogdir + "/" + pgLogfile // relative to DATADIR path
}

// If log_filename GUC contains %H%M%S part (Ubuntu-style default), it has to be replaced to timestamp of Postgres startup time.
Expand All @@ -83,29 +88,29 @@ func readLogPath() (string, error) {
// different variations of that: %H_%M_%S, %H-%M-%S or similar, and code below will not work.
// Also things becomes a bit tricky if logfile rotated through pg_rotate_logfile(), hence instead of Postgres startup time
// the time when rotation occurred will be use. This use case is not covered here.
if strings.Contains(logfileRealpath, "%H%M%S") {
if strings.Contains(absLogfilePath, "%H%M%S") {
var pgStartTime string
conn.QueryRow(stat.PgPostmasterStartTimeQuery).Scan(&pgStartTime)
// rotated logfile, fallback to it in case when above isn't exist
logfileFallback = strings.Replace(logfileRealpath, "%H%M%S", "000000", 1)
pgLogfileFallback = strings.Replace(absLogfilePath, "%H%M%S", "000000", 1)
// logfile created today
logfileRealpath = strings.Replace(logfileRealpath, "%H%M%S", pgStartTime, 1)
} else {
return "", fmt.Errorf("can't parse log_filename format")
absLogfilePath = strings.Replace(absLogfilePath, "%H%M%S", pgStartTime, 1)
}

var pgLogTz string = "timezone"
conn.QueryRow(stat.PgGetSingleSettingQuery, pgLogTz).Scan(&pgLogTz)
if strings.Contains(absLogfilePath, "%") {
var pgLogTz = "timezone"
conn.QueryRow(stat.PgGetSingleSettingQuery, pgLogTz).Scan(&pgLogTz)

t := time.Now()
tz, _ := time.LoadLocation(pgLogTz)
t = t.In(tz)
logfileRealpath = strftime.Format(logfileRealpath, t)
t := time.Now()
tz, _ := time.LoadLocation(pgLogTz)
t = t.In(tz)
absLogfilePath = strftime.Format(absLogfilePath, t)

// check the logfile exists, if not -- use fallback name.
if _, err := os.Stat(logfileRealpath); err != nil {
logfileRealpath = strftime.Format(logfileFallback, t)
// check the logfile exists, if not -- use fallback name.
if _, err := os.Stat(absLogfilePath); err != nil {
absLogfilePath = strftime.Format(pgLogfileFallback, t)
}
}

return logfileRealpath, nil
return absLogfilePath
}

0 comments on commit 2e1d14a

Please sign in to comment.