searchlog.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "io/fs"
  6. "log"
  7. "os"
  8. "runtime"
  9. "strings"
  10. "time"
  11. )
  12. // findPath Find the path in which the log files are stored.
  13. func findPath() (logPath string) {
  14. var err error
  15. var homedir string
  16. var Path string
  17. var Paths []string
  18. var PathsLinux []string
  19. var PathsMac []string
  20. var PathsWindows []string
  21. // Determine the homedir of the user.
  22. homedir, err = os.UserHomeDir()
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. // Define Paths to check out
  27. PathsLinux = append(PathsLinux, "/Documents/EVE/logs/")
  28. PathsLinux = append(PathsLinux, "/.local/share/Steam/steamapps/compatdata/8500/pfx/drive_c/users/steamuser/My Documents/EVE/logs/Chatlogs/")
  29. PathsMac = append(PathsMac, "/Documents/EVE/logs/")
  30. PathsMac = append(PathsMac, "/Library/Application Support/EVE Online/p_drive/User/My Documents/EVE/")
  31. PathsWindows = append(PathsWindows, "C:\\Users\\YourUserName\\Documents\\EVE\\logs")
  32. PathsWindows = append(PathsWindows, "MyDocuments -> EVE -> Logs")
  33. switch runtime.GOOS {
  34. case "linux":
  35. Paths = PathsLinux
  36. case "darwin":
  37. Paths = PathsMac
  38. case "windows":
  39. Paths = PathsWindows
  40. }
  41. for _, Path = range Paths {
  42. if _, err := os.Stat(homedir + Path); !os.IsNotExist(err) {
  43. logPath = homedir + Path
  44. }
  45. }
  46. return
  47. }
  48. // OrchestrateIntelMonitoring routine to periodically check the logpath for the intel channels.
  49. func OrchestrateIntelMonitoring(logPath string, intelChannel string) {
  50. // Declare variables
  51. var files []fs.FileInfo
  52. var lastName string
  53. var tempName string
  54. // Enter a for-loop that periodically checks.
  55. for {
  56. files = fetchFileListing(logPath)
  57. tempName = findLatestLog(logPath, intelChannel, files)
  58. if !strings.EqualFold(lastName, tempName) {
  59. lastName = tempName
  60. fmt.Println("Found a new LogFile! Its " + lastName)
  61. }
  62. // Sleep a minute before we check again.
  63. time.Sleep(time.Second * 60)
  64. }
  65. }
  66. // fetchFileListing As it says on the tin.
  67. func fetchFileListing(logPath string) (files []fs.FileInfo) {
  68. // Declare Variables
  69. var err error
  70. var folder *os.File
  71. // Open the folder we need to open.
  72. folder, err = os.Open(logPath)
  73. if err != nil {
  74. log.Fatal(err)
  75. }
  76. // Read the files UwU
  77. files, err = folder.Readdir(-1)
  78. folder.Close()
  79. if err != nil {
  80. log.Fatal(err)
  81. }
  82. // Off you go! \o/
  83. return
  84. }
  85. // findLatestLog Find the last log-file based on the Internet
  86. func findLatestLog(logPath string, intelChannel string, files []fs.FileInfo) (lastName string) {
  87. // Declare Variables
  88. var lastTime time.Time
  89. var tempTime time.Time
  90. for _, file := range files {
  91. file, err := os.Stat(logPath + file.Name())
  92. if err != nil {
  93. log.Fatal(err)
  94. }
  95. if strings.EqualFold(intelChannel, file.Name()[:len(intelChannel)]) {
  96. tempTime = file.ModTime()
  97. if tempTime.After(lastTime) {
  98. lastTime = tempTime
  99. lastName = logPath + file.Name()
  100. }
  101. }
  102. }
  103. // And there you are (or not)
  104. return
  105. }
  106. // main Something Importang. Dunno
  107. func main() {
  108. // Declare variables
  109. var logPath string
  110. var IntelChannelsSplit []string
  111. var IntelChannel string
  112. // Handle Parameters
  113. var intelChannels = flag.String("i", "Etherium Intel,Bean-Intel", "A commaseparated list of intel channels.")
  114. flag.Parse()
  115. // Split the CSV string into different parts
  116. IntelChannelsSplit = strings.Split(*intelChannels, ",")
  117. // Get the path we need to
  118. logPath = findPath()
  119. for _, IntelChannel = range IntelChannelsSplit {
  120. IntelChannel = strings.ReplaceAll(IntelChannel, " ", "_")
  121. go OrchestrateIntelMonitoring(logPath, IntelChannel)
  122. }
  123. for {
  124. time.Sleep(time.Second * 3600)
  125. fmt.Println("Hi. This is your hourly reminder that this program is still runnig and hasn't crashed!")
  126. }
  127. }