]> Cypherpunks.ru repositories - nncp.git/blob - src/cypherpunks.ru/nncp/cmd/nncp-log/main.go
Forbid any later GNU GPL versions autousage
[nncp.git] / src / cypherpunks.ru / nncp / cmd / nncp-log / main.go
1 /*
2 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
3 Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
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, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Read NNCP logs.
19 package main
20
21 import (
22         "bufio"
23         "flag"
24         "fmt"
25         "log"
26         "os"
27
28         "cypherpunks.ru/nncp"
29 )
30
31 func usage() {
32         fmt.Fprintf(os.Stderr, nncp.UsageHeader())
33         fmt.Fprintf(os.Stderr, "nncp-log -- read logs\n\n")
34         fmt.Fprintf(os.Stderr, "Usage: %s [options]\nOptions:\n", os.Args[0])
35         flag.PrintDefaults()
36 }
37
38 func main() {
39         var (
40                 cfgPath  = flag.String("cfg", nncp.DefaultCfgPath, "Path to configuration file")
41                 logPath  = flag.String("log", "", "Override path to logfile")
42                 debug    = flag.Bool("debug", false, "Print debug messages")
43                 version  = flag.Bool("version", false, "Print version information")
44                 warranty = flag.Bool("warranty", false, "Print warranty information")
45         )
46         flag.Usage = usage
47         flag.Parse()
48         if *warranty {
49                 fmt.Println(nncp.Warranty)
50                 return
51         }
52         if *version {
53                 fmt.Println(nncp.VersionGet())
54                 return
55         }
56
57         ctx, err := nncp.CtxFromCmdline(*cfgPath, "", *logPath, false, *debug)
58         if err != nil {
59                 log.Fatalln("Error during initialization:", err)
60         }
61
62         fd, err := os.Open(ctx.LogPath)
63         if err != nil {
64                 log.Fatalln("Can not open log:", err)
65         }
66         scanner := bufio.NewScanner(fd)
67         for scanner.Scan() {
68                 t := scanner.Text()
69                 if *debug {
70                         fmt.Println(t)
71                 }
72                 fmt.Println(ctx.Humanize(t))
73         }
74         if err = scanner.Err(); err != nil {
75                 log.Fatalln("Can not read log:", err)
76         }
77 }