log_collector_host.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package diagnostic
  2. import (
  3. "context"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "runtime"
  8. )
  9. const (
  10. linuxManagedLogsPath = "/var/log/cloudflared.err"
  11. darwinManagedLogsPath = "/Library/Logs/com.cloudflare.cloudflared.err.log"
  12. )
  13. type HostLogCollector struct {
  14. client HTTPClient
  15. }
  16. func NewHostLogCollector(client HTTPClient) *HostLogCollector {
  17. return &HostLogCollector{
  18. client,
  19. }
  20. }
  21. func getServiceLogPath() (string, error) {
  22. switch runtime.GOOS {
  23. case "darwin":
  24. {
  25. path := darwinManagedLogsPath
  26. if _, err := os.Stat(path); err == nil {
  27. return path, nil
  28. }
  29. userHomeDir, err := os.UserHomeDir()
  30. if err != nil {
  31. return "", fmt.Errorf("error getting user home: %w", err)
  32. }
  33. return filepath.Join(userHomeDir, darwinManagedLogsPath), nil
  34. }
  35. case "linux":
  36. {
  37. return linuxManagedLogsPath, nil
  38. }
  39. default:
  40. return "", ErrManagedLogNotFound
  41. }
  42. }
  43. func (collector *HostLogCollector) Collect(ctx context.Context) (*LogInformation, error) {
  44. logConfiguration, err := collector.client.GetLogConfiguration(ctx)
  45. if err != nil {
  46. return nil, fmt.Errorf("error getting log configuration: %w", err)
  47. }
  48. if logConfiguration.uid == 0 {
  49. path, err := getServiceLogPath()
  50. if err != nil {
  51. return nil, err
  52. }
  53. return NewLogInformation(path, false, false), nil
  54. }
  55. if logConfiguration.logFile != "" {
  56. return NewLogInformation(logConfiguration.logFile, false, false), nil
  57. } else if logConfiguration.logDirectory != "" {
  58. return NewLogInformation(logConfiguration.logDirectory, false, true), nil
  59. }
  60. return nil, ErrMustNotBeEmpty
  61. }