diff --git a/cmd/viam-agent/main.go b/cmd/viam-agent/main.go index eda31b5..398f547 100644 --- a/cmd/viam-agent/main.go +++ b/cmd/viam-agent/main.go @@ -76,7 +76,7 @@ func main() { } if opts.Debug { - globalLogger = logging.NewDebugLogger("viam-agent") + globalLogger.SetLevel(logging.DEBUG) } // need to be root to go any further than this diff --git a/manager.go b/manager.go index a93a88b..55fb34e 100644 --- a/manager.go +++ b/manager.go @@ -309,6 +309,7 @@ func (m *Manager) LoadSubsystems(ctx context.Context) error { if err != nil { m.logger.Error(errw.Wrap(err, "getting cached config")) } + m.processConfig(cachedConfig) for _, name := range registry.List() { cfg, ok := cachedConfig[name] @@ -344,8 +345,8 @@ func (m *Manager) getCachedConfig() (map[string]*pb.DeviceSubsystemConfig, error cachedConfig := map[string]*pb.DeviceSubsystemConfig{SubsystemName: {}} cacheFilePath := filepath.Join(ViamDirs["cache"], agentCachePath) - //nolint:gosec - cacheBytes, err := os.ReadFile(cacheFilePath) + + cacheBytes, err := os.ReadFile(cacheFilePath) //nolint:gosec if err != nil { if errors.Is(err, fs.ErrNotExist) { return cachedConfig, nil @@ -422,6 +423,26 @@ func (m *Manager) dial(ctx context.Context) error { return nil } +// process non-subsystem effects of a config (i.e. agent-specific stuff that needs to happen when loading cache and when updating). +func (m *Manager) processConfig(cfg map[string]*pb.DeviceSubsystemConfig) { + if agent, ok := cfg["viam-agent"]; ok { + if debugRaw, ok := agent.GetAttributes().AsMap()["debug"]; ok { + if debug, ok := debugRaw.(bool); !ok { + m.logger.Error("viam-agent debug attribute is present but is not a bool") + } else { + // note: if this is present (true or false, rather than missing) it overrides the CLI debug switch. + // if the user removes the `debug` attribute, we don't revert to the CLI debug switch state. (we ideally should). + // note: this assumes m.logger is the global logger shared by the other subsystems. + if debug { + m.logger.SetLevel(logging.DEBUG) + } else { + m.logger.SetLevel(logging.INFO) + } + } + } + } +} + // GetConfig retrieves the configuration from the cloud, or returns a cached version if unable to communicate. func (m *Manager) GetConfig(ctx context.Context) (map[string]*pb.DeviceSubsystemConfig, time.Duration, error) { if m.cloudConfig == nil { @@ -453,6 +474,8 @@ func (m *Manager) GetConfig(ctx context.Context) (map[string]*pb.DeviceSubsystem m.logger.Error(errw.Wrap(err, "saving agent config to cache")) } + m.processConfig(resp.GetSubsystemConfigs()) + interval := resp.GetCheckInterval().AsDuration() if interval < minimalCheckInterval {