| 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 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 multiflag.HelpOption(&a.outputConfig.MultiFlag), | 98 multiflag.HelpOption(&a.outputConfig.MultiFlag), |
| 99 } | 99 } |
| 100 | 100 |
| 101 // Add registered conditional (build tag) options. | 101 // Add registered conditional (build tag) options. |
| 102 for _, f := range getOutputFactories() { | 102 for _, f := range getOutputFactories() { |
| 103 a.outputConfig.AddFactory(f) | 103 a.outputConfig.AddFactory(f) |
| 104 } | 104 } |
| 105 | 105 |
| 106 a.maxBufferAge = clockflag.Duration(butler.DefaultMaxBufferAge) | 106 a.maxBufferAge = clockflag.Duration(butler.DefaultMaxBufferAge) |
| 107 | 107 |
| 108 fs.Var(&a.butler.Project, "project", |
| 109 "The log prefix's project name (required).") |
| 108 fs.Var(&a.butler.Prefix, "prefix", | 110 fs.Var(&a.butler.Prefix, "prefix", |
| 109 "Prefix to apply to all stream names.") | 111 "Prefix to apply to all stream names.") |
| 110 fs.Var(&a.outputConfig, "output", | 112 fs.Var(&a.outputConfig, "output", |
| 111 "The output name and configuration. Specify 'help' for more info
rmation.") | 113 "The output name and configuration. Specify 'help' for more info
rmation.") |
| 112 fs.StringVar(&a.cpuProfile, | 114 fs.StringVar(&a.cpuProfile, |
| 113 "cpuprofile", "", "If specified, enables CPU profiling and profi
les to the specified path.") | 115 "cpuprofile", "", "If specified, enables CPU profiling and profi
les to the specified path.") |
| 114 fs.IntVar(&a.butler.OutputWorkers, "output-workers", butler.DefaultOutpu
tWorkers, | 116 fs.IntVar(&a.butler.OutputWorkers, "output-workers", butler.DefaultOutpu
tWorkers, |
| 115 "The maximum number of parallel output dispatches.") | 117 "The maximum number of parallel output dispatches.") |
| 116 fs.Var(&a.maxBufferAge, "output-max-buffer-age", | 118 fs.Var(&a.maxBufferAge, "output-max-buffer-age", |
| 117 "Send buffered messages if they've been held for longer than thi
s period.") | 119 "Send buffered messages if they've been held for longer than thi
s period.") |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 | 167 |
| 166 return output, nil | 168 return output, nil |
| 167 } | 169 } |
| 168 | 170 |
| 169 // An execution harness that adds application-level management to a Butler run. | 171 // An execution harness that adds application-level management to a Butler run. |
| 170 func (a *application) Main(runFunc func(b *butler.Butler) error) error { | 172 func (a *application) Main(runFunc func(b *butler.Butler) error) error { |
| 171 // Enable CPU profiling if specified | 173 // Enable CPU profiling if specified |
| 172 if a.cpuProfile != "" { | 174 if a.cpuProfile != "" { |
| 173 f, err := os.Create(a.cpuProfile) | 175 f, err := os.Create(a.cpuProfile) |
| 174 if err != nil { | 176 if err != nil { |
| 175 » » » return fmt.Errorf("Failed to create CPU profile output:
%v", err) | 177 » » » return fmt.Errorf("failed to create CPU profile output:
%v", err) |
| 176 } | 178 } |
| 177 pprof.StartCPUProfile(f) | 179 pprof.StartCPUProfile(f) |
| 178 defer pprof.StopCPUProfile() | 180 defer pprof.StopCPUProfile() |
| 179 } | 181 } |
| 180 | 182 |
| 183 // Generate a prefix secret for this Butler session. |
| 184 var err error |
| 185 if a.butler.Secret, err = types.NewPrefixSecret(); err != nil { |
| 186 return fmt.Errorf("failed to generate prefix secret: %s", err) |
| 187 } |
| 188 |
| 181 // Instantiate our Butler. | 189 // Instantiate our Butler. |
| 182 a.butler.MaxBufferAge = time.Duration(a.maxBufferAge) | 190 a.butler.MaxBufferAge = time.Duration(a.maxBufferAge) |
| 183 a.butler.BufferLogs = !a.noBufferLogs | 191 a.butler.BufferLogs = !a.noBufferLogs |
| 184 a.butler.Output = a.output | 192 a.butler.Output = a.output |
| 185 a.butler.TeeStdout = os.Stdout | 193 a.butler.TeeStdout = os.Stdout |
| 186 a.butler.TeeStderr = os.Stderr | 194 a.butler.TeeStderr = os.Stderr |
| 187 if err := a.butler.Validate(); err != nil { | 195 if err := a.butler.Validate(); err != nil { |
| 188 return err | 196 return err |
| 189 } | 197 } |
| 190 | 198 |
| (...skipping 82 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 |