main.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2018, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package main
  20. import (
  21. "flag"
  22. "fmt"
  23. "os"
  24. "strings"
  25. "github.com/Psiphon-Labs/psiphon-tunnel-core/Server/logging/analysis"
  26. )
  27. type stringListFlag []string
  28. func (list *stringListFlag) String() string {
  29. return strings.Join(*list, ", ")
  30. }
  31. func (list *stringListFlag) Set(flagValue string) error {
  32. *list = append(*list, flagValue)
  33. return nil
  34. }
  35. func main() {
  36. var logFileList stringListFlag
  37. var printMessages bool
  38. var printMetrics bool
  39. var printUnknowns bool
  40. var printStructure bool
  41. var printExample bool
  42. flag.Var(
  43. &logFileList,
  44. "file",
  45. "file to analyze; flag may be repeated to analyze multiple files")
  46. flag.BoolVar(
  47. &printMessages,
  48. "messages",
  49. false,
  50. "display message type logs")
  51. flag.BoolVar(
  52. &printMetrics,
  53. "metrics",
  54. false,
  55. "display metric type logs")
  56. flag.BoolVar(
  57. &printUnknowns,
  58. "unknown",
  59. false,
  60. "display logs of an unknown type")
  61. flag.BoolVar(
  62. &printStructure,
  63. "structure",
  64. false,
  65. "print each log model with its key graph structure")
  66. flag.BoolVar(
  67. &printExample,
  68. "example",
  69. false,
  70. "print each log model with an example")
  71. flag.Usage = func() {
  72. fmt.Fprintf(os.Stderr,
  73. "Usage:\n\n"+
  74. "%s <flags>\n"+
  75. os.Args[0], os.Args[0]+"\n\n")
  76. fmt.Printf("\n")
  77. flag.PrintDefaults()
  78. }
  79. flag.Parse()
  80. if len(logFileList) < 1 {
  81. flag.Usage()
  82. os.Exit(1)
  83. }
  84. logFileStats, err := analysis.NewLogStatsFromFiles(logFileList)
  85. if err != nil {
  86. fmt.Printf("Error while parsing log files: %s\n", err)
  87. os.Exit(1)
  88. }
  89. logFileStats.Print(printMessages, printMetrics, printUnknowns, printStructure, printExample)
  90. fmt.Printf("Found %d messages, %d metrics and %d unknown logs with a total of %d distinct types of logs\n",
  91. logFileStats.MessageLogModels.Count,
  92. logFileStats.MetricsLogModels.Count,
  93. logFileStats.UnknownLogModels.Count,
  94. logFileStats.NumDistinctLogs())
  95. }