Skip to content

Commit

Permalink
New feature: write log file
Browse files Browse the repository at this point in the history
  • Loading branch information
tzerk committed Dec 2, 2017
1 parent 07de9d1 commit edb13d8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ faillimit: 2
killondc: true
shutdownondc: false
killcoherentui: false
log: true

# Set the process priority. Allowed values are: idle, below normal, normal, above normal, high priority, realtime
# Also requires admin rights!
Expand Down
2 changes: 2 additions & 0 deletions lib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Config struct {
KillOnDC bool
ShutdownOnDC bool
KillCoherentUI bool
Log bool
ProcessPriority string
}

Expand Down Expand Up @@ -65,6 +66,7 @@ func Read_Settings(ex string) (config Config, err error) {
"killondc: true\r\n" +
"shutdownondc: false\r\n" +
"killcoherentui: false\r\n" +
"log: true\r\n" +
"\r\n" +
"# Set the process priority. Allowed values are: idle, below normal, normal, above normal, high priority, realtime\r\n" +
"# Also requires admin rights!\r\n" +
Expand Down
26 changes: 26 additions & 0 deletions lib/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package lib

import (
"os"
"log"
)

func logger(msg string) (err error) {

// create your file with desired read/write permissions
f, err := os.OpenFile("log.txt", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0777)
if err != nil {
log.Fatal(err)
}

// defer to close when you're done with it, not because you think it's idiomatic!
defer f.Close()

// set output of logs to f
log.SetOutput(f)

// write message
log.Println(msg)

return err
}
7 changes: 6 additions & 1 deletion lib/watchdog.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,19 @@ func Watchdog(
// If the process is running, but no longer connected we trigger the following actions
if EXIT {

// Only procede with exit routine if we reached the fail threshold
// Only proceed with exit routine if we reached the fail threshold
if PENALTY >= config.FailLimit {

// Optional: shutdown the computer if the monitored process is disconnected
if config.ShutdownOnDC {
exec.Command("cmd", "/C", "shutdown", "/s").Run()
}

// Optional: write disconnect msg to log file
if config.Log {
logger("Process (PID " + strconv.Itoa(PID) + ") disconnected\r\n")
}

// Optional: kill the monitored process if it is disconnected
// requires elevated rights --> start .exe as administrator
if config.KillOnDC && PID != 0 {
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// Variables
const VERSION = "0.1.9"
const VERSION = "0.2.0"
var errmsg string

func main() {
Expand Down Expand Up @@ -69,6 +69,7 @@ func main() {
box_settings.Append(ui.NewLabel("Kill process after disconnect: " + strconv.FormatBool(config.KillOnDC)), false)
box_settings.Append(ui.NewLabel("Shutdown PC after disconnect: " + strconv.FormatBool(config.ShutdownOnDC) ), false)
box_settings.Append(ui.NewLabel("Kill CoherenUI_Host.exe: " + strconv.FormatBool(config.KillCoherentUI)), false)
box_settings.Append(ui.NewLabel("Write log file: " + strconv.FormatBool(config.Log)), false)
box_settings.Append(ui.NewLabel("Process Priority: " + config.ProcessPriority), false)

// Append UI elements for about tab
Expand Down

0 comments on commit edb13d8

Please sign in to comment.