| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package main | 5 package main |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "errors" | 8 "errors" |
| 9 "flag" | 9 "flag" |
| 10 "fmt" | 10 "fmt" |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 multiflag.HelpOption(&a.outputConfig.MultiFlag), | 96 multiflag.HelpOption(&a.outputConfig.MultiFlag), |
| 97 } | 97 } |
| 98 | 98 |
| 99 // Add registered conditional (build tag) options. | 99 // Add registered conditional (build tag) options. |
| 100 for _, f := range getOutputFactories() { | 100 for _, f := range getOutputFactories() { |
| 101 a.outputConfig.AddFactory(f) | 101 a.outputConfig.AddFactory(f) |
| 102 } | 102 } |
| 103 | 103 |
| 104 a.maxBufferAge = clockflag.Duration(butler.DefaultMaxBufferAge) | 104 a.maxBufferAge = clockflag.Duration(butler.DefaultMaxBufferAge) |
| 105 | 105 |
| 106 fs.Var(&a.butler.Project, "project", |
| 107 "The log prefix's project name (required).") |
| 106 fs.Var(&a.butler.Prefix, "prefix", | 108 fs.Var(&a.butler.Prefix, "prefix", |
| 107 "Prefix to apply to all stream names.") | 109 "Prefix to apply to all stream names.") |
| 108 fs.Var(&a.outputConfig, "output", | 110 fs.Var(&a.outputConfig, "output", |
| 109 "The output name and configuration. Specify 'help' for more info
rmation.") | 111 "The output name and configuration. Specify 'help' for more info
rmation.") |
| 110 fs.StringVar(&a.cpuProfile, | 112 fs.StringVar(&a.cpuProfile, |
| 111 "cpuprofile", "", "If specified, enables CPU profiling and profi
les to the specified path.") | 113 "cpuprofile", "", "If specified, enables CPU profiling and profi
les to the specified path.") |
| 112 fs.IntVar(&a.butler.OutputWorkers, "output-workers", butler.DefaultOutpu
tWorkers, | 114 fs.IntVar(&a.butler.OutputWorkers, "output-workers", butler.DefaultOutpu
tWorkers, |
| 113 "The maximum number of parallel output dispatches.") | 115 "The maximum number of parallel output dispatches.") |
| 114 fs.Var(&a.maxBufferAge, "output-max-buffer-age", | 116 fs.Var(&a.maxBufferAge, "output-max-buffer-age", |
| 115 "Send buffered messages if they've been held for longer than thi
s period.") | 117 "Send buffered messages if they've been held for longer than thi
s period.") |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 164 | 166 |
| 165 return output, nil | 167 return output, nil |
| 166 } | 168 } |
| 167 | 169 |
| 168 // An execution harness that adds application-level management to a Butler run. | 170 // An execution harness that adds application-level management to a Butler run. |
| 169 func (a *application) Main(runFunc func(b *butler.Butler) error) error { | 171 func (a *application) Main(runFunc func(b *butler.Butler) error) error { |
| 170 // Enable CPU profiling if specified | 172 // Enable CPU profiling if specified |
| 171 if a.cpuProfile != "" { | 173 if a.cpuProfile != "" { |
| 172 f, err := os.Create(a.cpuProfile) | 174 f, err := os.Create(a.cpuProfile) |
| 173 if err != nil { | 175 if err != nil { |
| 174 » » » return fmt.Errorf("Failed to create CPU profile output:
%v", err) | 176 » » » return fmt.Errorf("failed to create CPU profile output:
%v", err) |
| 175 } | 177 } |
| 176 pprof.StartCPUProfile(f) | 178 pprof.StartCPUProfile(f) |
| 177 defer pprof.StopCPUProfile() | 179 defer pprof.StopCPUProfile() |
| 178 } | 180 } |
| 179 | 181 |
| 182 // Generate a prefix secret for this Butler session. |
| 183 var err error |
| 184 if a.butler.Secret, err = types.NewPrefixSecret(); err != nil { |
| 185 return fmt.Errorf("failed to generate prefix secret: %s", err) |
| 186 } |
| 187 |
| 180 // Instantiate our Butler. | 188 // Instantiate our Butler. |
| 181 a.butler.MaxBufferAge = time.Duration(a.maxBufferAge) | 189 a.butler.MaxBufferAge = time.Duration(a.maxBufferAge) |
| 182 a.butler.BufferLogs = !a.noBufferLogs | 190 a.butler.BufferLogs = !a.noBufferLogs |
| 183 a.butler.Output = a.output | 191 a.butler.Output = a.output |
| 184 a.butler.TeeStdout = os.Stdout | 192 a.butler.TeeStdout = os.Stdout |
| 185 a.butler.TeeStderr = os.Stderr | 193 a.butler.TeeStderr = os.Stderr |
| 186 if err := a.butler.Validate(); err != nil { | 194 if err := a.butler.Validate(); err != nil { |
| 187 return err | 195 return err |
| 188 } | 196 } |
| 189 | 197 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 a.authFlags.Register(flags, authOptions) | 281 a.authFlags.Register(flags, authOptions) |
| 274 | 282 |
| 275 // Parse the top-level flag set. | 283 // Parse the top-level flag set. |
| 276 if err := flags.Parse(argv); err != nil { | 284 if err := flags.Parse(argv); err != nil { |
| 277 log.WithError(err).Errorf(a, "Failed to parse command-line.") | 285 log.WithError(err).Errorf(a, "Failed to parse command-line.") |
| 278 return configErrorReturnCode | 286 return configErrorReturnCode |
| 279 } | 287 } |
| 280 | 288 |
| 281 a.Context = logConfig.Set(a.Context) | 289 a.Context = logConfig.Set(a.Context) |
| 282 | 290 |
| 291 // TODO(dnj): Force all invocations to supply a Project. |
| 292 if a.butler.Project != "" { |
| 293 if err := a.butler.Project.Validate(); err != nil { |
| 294 log.WithError(err).Errorf(a, "Invalid project (-project)
.") |
| 295 return configErrorReturnCode |
| 296 } |
| 297 } |
| 298 |
| 283 // Validate our Prefix; generate a user prefix if one was not supplied. | 299 // Validate our Prefix; generate a user prefix if one was not supplied. |
| 284 prefix := a.butler.Prefix | 300 prefix := a.butler.Prefix |
| 285 if prefix == "" { | 301 if prefix == "" { |
| 286 // Auto-generate a prefix. | 302 // Auto-generate a prefix. |
| 287 prefix, err := generateRandomUserPrefix(a) | 303 prefix, err := generateRandomUserPrefix(a) |
| 288 if err != nil { | 304 if err != nil { |
| 289 log.WithError(err).Errorf(a, "Failed to generate user pr
efix.") | 305 log.WithError(err).Errorf(a, "Failed to generate user pr
efix.") |
| 290 return configErrorReturnCode | 306 return configErrorReturnCode |
| 291 } | 307 } |
| 292 a.butler.Prefix = prefix | 308 a.butler.Prefix = prefix |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 | 371 |
| 356 paniccatcher.Do(func() { | 372 paniccatcher.Do(func() { |
| 357 rc = mainImpl(ctx, os.Args[1:]) | 373 rc = mainImpl(ctx, os.Args[1:]) |
| 358 }, func(p *paniccatcher.Panic) { | 374 }, func(p *paniccatcher.Panic) { |
| 359 log.Fields{ | 375 log.Fields{ |
| 360 "panic.error": p.Reason, | 376 "panic.error": p.Reason, |
| 361 }.Errorf(ctx, "Panic caught in main:\n%s", p.Stack) | 377 }.Errorf(ctx, "Panic caught in main:\n%s", p.Stack) |
| 362 rc = runtimeErrorReturnCode | 378 rc = runtimeErrorReturnCode |
| 363 }) | 379 }) |
| 364 } | 380 } |
| OLD | NEW |