/
syslog.go
113 lines (92 loc) · 2.3 KB
/
syslog.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package config
import (
"fmt"
"github.com/hashicorp/consul-terraform-sync/version"
)
const (
// DefaultSyslogFacility is the default facility to log to.
DefaultSyslogFacility = "LOCAL0"
)
var (
// DefaultSyslogName is the default app name in syslog.
DefaultSyslogName = version.Name
)
// SyslogConfig is the configuration for syslog.
type SyslogConfig struct {
Enabled *bool `mapstructure:"enabled"`
Facility *string `mapstructure:"facility"`
Name *string `mapstructure:"name"`
}
// DefaultSyslogConfig returns the default configuration struct.
func DefaultSyslogConfig() *SyslogConfig {
return &SyslogConfig{
// No default values. `Enabled` value depends on other fields as
// handled in Finalize()
}
}
// Copy returns a deep copy of this configuration.
func (c *SyslogConfig) Copy() *SyslogConfig {
if c == nil {
return nil
}
var o SyslogConfig
o.Enabled = BoolCopy(c.Enabled)
o.Facility = StringCopy(c.Facility)
o.Name = StringCopy(c.Name)
return &o
}
// Merge combines all values in this configuration with the values in the other
// configuration, with values in the other configuration taking precedence.
// Maps and slices are merged, most other values are overwritten. Complex
// structs define their own merge functionality.
func (c *SyslogConfig) Merge(o *SyslogConfig) *SyslogConfig {
if c == nil {
if o == nil {
return nil
}
return o.Copy()
}
if o == nil {
return c.Copy()
}
r := c.Copy()
if o.Enabled != nil {
r.Enabled = BoolCopy(o.Enabled)
}
if o.Facility != nil {
r.Facility = StringCopy(o.Facility)
}
if o.Name != nil {
r.Name = StringCopy(o.Name)
}
return r
}
// Finalize ensures there no nil pointers.
func (c *SyslogConfig) Finalize() {
if c.Enabled == nil {
c.Enabled = Bool(StringPresent(c.Facility) || StringPresent(c.Name))
}
if c.Facility == nil {
c.Facility = String(DefaultSyslogFacility)
}
if c.Name == nil {
c.Name = String(DefaultSyslogName)
}
}
// GoString defines the printable version of this struct.
func (c *SyslogConfig) GoString() string {
if c == nil {
return "(*SyslogConfig)(nil)"
}
return fmt.Sprintf("&SyslogConfig{"+
"Enabled:%t, "+
"Facility:%s, "+
"Name:%s"+
"}",
BoolVal(c.Enabled),
StringVal(c.Facility),
StringVal(c.Name),
)
}