-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
163 lines (143 loc) · 4.12 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main // Author wwalker
import (
"bufio"
"fmt"
"os"
"os/exec"
"time"
"github.com/alecthomas/kingpin"
)
type Cfg struct {
logfile *os.File
printfFormat string
prefix string
suffix string
timeFormat string
noStdout bool
timeInFilename bool
timeZoneUtc bool
appendToLog bool
startLine bool
endLine bool
}
var cmd_args *[]string
func (config *Cfg) parseArgs() {
app := kingpin.New("ilts - In Line Time Stamper", "ilts prepends messages with a time stamp and writes to stdout")
app.Flag("prefix", "turns on logging to the file prefixed with filepath").Short('p').StringVar(&config.prefix)
app.Flag("printf-format", "printf style for how to print the timestamp with the message ; defaults to \"%s - %s\n\"").Short('P').StringVar(&config.printfFormat)
app.Flag("suffix", "suffix appended to filepath").Short('s').StringVar(&config.suffix)
app.Flag("nostdout", "disable writing to stdout").Short('n').BoolVar(&config.noStdout)
app.Flag("time", "add timestamp to filepath after prefix").Short('t').BoolVar(&config.timeInFilename)
app.Flag("time-format", "add timestamp to filepath after prefix").Short('T').StringVar(&config.timeFormat)
app.Flag("utc", "set timezone for timestamp to UTC").Short('u').BoolVar(&config.timeZoneUtc)
app.Flag("append", "append to rather than truncate existin logfile").Short('a').BoolVar(&config.appendToLog)
app.Flag("start-line", "immediately write a timestamped \"Starting\" upon startup").Short('S').BoolVar(&config.startLine)
app.Flag("end-line", "write a timestamped \"Ending\" when exiting").Short('E').BoolVar(&config.endLine)
cmd_args = app.Arg("cmd", "commands").Strings()
kingpin.MustParse(app.Parse(os.Args[1:]))
config.unsupportedFlags()
if config.timeFormat == "" {
config.timeFormat = "2006-01-02_15:04:05.000000"
}
// TODO
if config.printfFormat == "" {
config.printfFormat = "%s - %s\n"
}
}
func (config *Cfg) unsupportedFlags() {
fatal := false
if config.timeZoneUtc {
fmt.Printf("--utc|-u is not currently supported")
}
if fatal {
os.Exit(1)
}
}
func die(message string) {
fmt.Printf(message + "\n")
os.Exit(1)
}
func main() {
var cfg *Cfg
cfg = &Cfg{}
cfg.parseArgs()
cfg.openLogFile()
if cfg.startLine {
cfg.printMessage("Execution begins")
}
if len(*cmd_args) == 0 {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
cfg.printMessage(line)
}
} else {
cmd := exec.Command((*cmd_args)[0], (*cmd_args)[1:]...)
stdout_pipe, err := cmd.StdoutPipe()
if err != nil {
log_fatal(err)
}
stdout := bufio.NewScanner(stdout_pipe)
// create a go routine pushing lines into a channel
stderr_pipe, err := cmd.StderrPipe()
if err != nil {
log_fatal(err)
}
stderr := bufio.NewScanner(stderr_pipe)
// create a go routine pushing lines into a channel
go func() {
for stderr.Scan() {
cfg.printMessage(stderr.Text())
}
}()
if err := cmd.Start(); err != nil {
log_fatal(err)
}
// consume from the channels and cfg.printMessage(line)
for stdout.Scan() {
cfg.printMessage(stdout.Text())
}
}
if cfg.endLine {
cfg.printMessage("Execution ends")
}
}
func (cfg *Cfg) openLogFile() {
var err error
var filepath string
now := time.Now()
if cfg.prefix != "" {
filepath = cfg.prefix
if cfg.timeInFilename {
filepath = filepath + now.Format(cfg.timeFormat)
}
if cfg.suffix != "" {
filepath = filepath + cfg.suffix
}
if cfg.appendToLog {
cfg.logfile, err = os.OpenFile(filepath, os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
die("Failed to open file: <" + filepath + "> for append")
}
} else {
cfg.logfile, err = os.OpenFile(filepath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
die("Failed to open new file: <" + filepath + ">")
}
}
}
}
func (cfg *Cfg) printMessage(message string) {
now := time.Now()
nowString := now.Format(cfg.timeFormat)
if cfg.prefix != "" {
fmt.Fprintf(cfg.logfile, cfg.printfFormat, nowString, message)
}
if !cfg.noStdout {
fmt.Printf(cfg.printfFormat, nowString, message)
}
}
func log_fatal(message error) {
fmt.Fprintln(os.Stderr, message)
os.Exit(1)
}