Skip to content

Commit

Permalink
Handle logging of dynamic env depending on log status
Browse files Browse the repository at this point in the history
Signed-off-by: Łukasz Gryglicki <[email protected]>
  • Loading branch information
lukaszgryglicki committed Dec 24, 2023
1 parent 65df5ed commit b88ee04
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (ctx *Ctx) SetCPUs() {
func (ctx *Ctx) Init() {
// Initialize env syncer once
syncerOnce.Do(func() {
UpdateEnv()
UpdateEnv(false)
go EnvSyncer()
})
ctx.ExecFatal = true
Expand Down
12 changes: 9 additions & 3 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ var gEnvMap map[string]string = make(map[string]string)
func EnvSyncer() {
for {
time.Sleep(30 * time.Second)
UpdateEnv()
UpdateEnv(true)
}
}

// UpdateEnv - update (eventually) env using env.env file
func UpdateEnv() {
func UpdateEnv(useLog bool) {
ef, err := os.Open("env.env")
if err != nil {
return
Expand All @@ -42,7 +42,13 @@ func UpdateEnv() {
os.Setenv(k, v)
cv, ok := gEnvMap[k]
if !ok || cv != v {
fmt.Printf("new environment overwrite: '%s' --> '%s'\n", k, v)
if useLog {
if IsLogInitialized() {
Printf("new environment overwrite: '%s' --> '%s'\n", k, v)
}
} else {
fmt.Printf("new environment overwrite: '%s' --> '%s'\n", k, v)
}
}
gEnvMap[k] = v
}
Expand Down
28 changes: 21 additions & 7 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ type logContext struct {
// This variable is initialized *only* once, and must be guared by the mutex
// to avoid initializing it from multiple go routines
var (
logCtx *logContext
logCtxMutex sync.RWMutex
logOnce sync.Once
BuildStamp = "None"
GitHash = "None"
HostName = "None"
GoVersion = "None"
logCtx *logContext
logCtxMutex sync.RWMutex
logOnce sync.Once
logInitialized = false
logInitMtx sync.Mutex
BuildStamp = "None"
GitHash = "None"
HostName = "None"
GoVersion = "None"
)

// Returns new context when not yet created
Expand All @@ -52,6 +54,11 @@ func newLogContext() *logContext {
now,
info,
)
defer func() {
logInitMtx.Lock()
logInitialized = true
logInitMtx.Unlock()
}()
return &logContext{
ctx: ctx,
con: con,
Expand Down Expand Up @@ -108,6 +115,13 @@ func Printf(format string, args ...interface{}) (n int, err error) {
return
}

// IsLogInitialized - check if log is initialized
func IsLogInitialized() bool {
logInitMtx.Lock()
defer logInitMtx.Unlock()
return logInitialized
}

// ClearDBLogs clears logs older by defined period (in context.go)
// It clears logs on `devstats` database
func ClearDBLogs() {
Expand Down

0 comments on commit b88ee04

Please sign in to comment.