# Global Metrics

path: .metrics.halstead.N1
old: 101.0
new: 355.0
path: .metrics.halstead.level
old: 0.10666666666666667
new: 0.03605375286791216
path: .metrics.halstead.volume
old: 1096.272089527275
new: 4123.9566402000655
path: .metrics.halstead.N2
old: 75.0
new: 226.0
path: .metrics.halstead.purity_ratio
old: 2.346686333355903
new: 1.5048735474115769
path: .metrics.halstead.length
old: 176.0
new: 581.0
path: .metrics.halstead.time
old: 570.9750466287892
new: 6354.642277399192
path: .metrics.halstead.estimated_program_length
old: 413.0167946706389
new: 874.3315310461262
path: .metrics.halstead.difficulty
old: 9.375
new: 27.73636363636364
path: .metrics.halstead.bugs
old: 0.15756938300526804
new: 0.7854547200813053
path: .metrics.halstead.n1
old: 15.0
new: 27.0
path: .metrics.halstead.n2
old: 60.0
new: 110.0
path: .metrics.halstead.effort
old: 10277.550839318204
new: 114383.56099318546
path: .metrics.halstead.vocabulary
old: 75.0
new: 137.0
path: .metrics.mi.mi_sei
old: 32.416987904516
new: 8.966364092545927
path: .metrics.mi.mi_original
old: 59.86479573411026
new: 38.03349396570812
path: .metrics.mi.mi_visual_studio
old: 35.00865247608787
new: 22.241809336671412
path: .metrics.cognitive.average
old: 1.0
new: 6.2
path: .metrics.cognitive.sum
old: 1.0
new: 31.0
path: .metrics.cyclomatic.average
old: 1.1428571428571428
new: 4.571428571428571
path: .metrics.cyclomatic.sum
old: 8.0
new: 32.0
path: .metrics.loc.sloc
old: 90.0
new: 161.0
path: .metrics.loc.lloc
old: 1.0
new: 48.0
path: .metrics.loc.cloc
old: 7.0
new: 21.0
path: .metrics.loc.blank
old: 29.0
new: 21.0
path: .metrics.loc.ploc
old: 54.0
new: 119.0
path: .metrics.nexits.average
old: 1.0
new: 0.8
path: .metrics.nexits.sum
old: 1.0
new: 4.0
path: .metrics.nargs.sum
old: 0.0
new: 4.0
path: .metrics.nargs.average
old: 0.0
new: 0.8
path: .metrics.nom.functions
old: 1.0
new: 5.0
path: .metrics.nom.total
old: 1.0
new: 5.0

# Spaces Data

## Minimal test - lines (27, 161)

path: .spaces[0].metrics.nom.functions
old: 0.0
new: 5.0
path: .spaces[0].metrics.nom.total
old: 0.0
new: 5.0
path: .spaces[0].metrics.nexits.sum
old: 0.0
new: 4.0
path: .spaces[0].metrics.nexits.average
old: null
new: 0.8
path: .spaces[0].metrics.halstead.n1
old: 0.0
new: 24.0
path: .spaces[0].metrics.halstead.vocabulary
old: 1.0
new: 118.0
path: .spaces[0].metrics.halstead.effort
old: 0.0
new: 93534.24040554452
path: .spaces[0].metrics.halstead.length
old: 1.0
new: 527.0
path: .spaces[0].metrics.halstead.n2
old: 1.0
new: 94.0
path: .spaces[0].metrics.halstead.volume
old: 0.0
new: 3627.1528870136904
path: .spaces[0].metrics.halstead.level
old: null
new: 0.038778877887788776
path: .spaces[0].metrics.halstead.N2
old: 1.0
new: 202.0
path: .spaces[0].metrics.halstead.time
old: 0.0
new: 5196.346689196918
path: .spaces[0].metrics.halstead.N1
old: 0.0
new: 325.0
path: .spaces[0].metrics.halstead.bugs
old: 0.0
new: 0.6868456642163285
path: .spaces[0].metrics.halstead.estimated_program_length
old: null
new: 726.1704520750056
path: .spaces[0].metrics.halstead.difficulty
old: 0.0
new: 25.78723404255319
path: .spaces[0].metrics.halstead.purity_ratio
old: null
new: 1.3779325466318892
path: .spaces[0].metrics.mi.mi_original
old: null
new: 41.78429147679482
path: .spaces[0].metrics.mi.mi_sei
old: null
new: 12.425095990512975
path: .spaces[0].metrics.mi.mi_visual_studio
old: null
new: 24.435258173564225
path: .spaces[0].metrics.cognitive.average
old: null
new: 6.2
path: .spaces[0].metrics.cognitive.sum
old: 0.0
new: 31.0
path: .spaces[0].metrics.loc.ploc
old: 1.0
new: 102.0
path: .spaces[0].metrics.loc.cloc
old: 0.0
new: 15.0
path: .spaces[0].metrics.loc.blank
old: 0.0
new: 18.0
path: .spaces[0].metrics.loc.sloc
old: 1.0
new: 135.0
path: .spaces[0].metrics.loc.lloc
old: 0.0
new: 48.0
path: .spaces[0].metrics.cyclomatic.average
old: 1.0
new: 5.166666666666667
path: .spaces[0].metrics.cyclomatic.sum
old: 1.0
new: 31.0
path: .spaces[0].metrics.nargs.average
old: null
new: 0.8
path: .spaces[0].metrics.nargs.sum
old: 0.0
new: 4.0

### Code

    namespace mozilla {

    NS_IMPL_ISUPPORTS(LogModulePrefWatcher, nsIObserver)

    /**
     * Resets all the preferences in the logging. branch
     * This is needed because we may crash while logging, and this would cause us
     * to log after restarting as well.
     *
     * If logging after restart is desired, set the logging.config.clear_on_startup
     * pref to false, or use the MOZ_LOG_FILE and MOZ_LOG_MODULES env vars.
     */
    static void ResetExistingPrefs() {
      nsTArray names;
      nsresult rv =
          Preferences::GetRootBranch()->GetChildList(kLoggingPrefPrefix, names);
      if (NS_SUCCEEDED(rv)) {
        for (auto& name : names) {
          // Clearing the pref will cause it to reload, thus resetting the log level
          Preferences::ClearUser(name.get());
        }
      }
    }

    /**
     * Loads the log level from the given pref and updates the corresponding
     * LogModule.
     */
    static void LoadPrefValue(const char* aName) {
      LogLevel logLevel = LogLevel::Disabled;

      nsresult rv;
      int32_t prefLevel = 0;
      nsAutoCString prefValue;

      if (strncmp(aName, kLoggingConfigPrefPrefix, kLoggingConfigPrefixLen) == 0) {
        nsAutoCString prefName(aName);

        if (prefName.EqualsLiteral(kLoggingPrefLogFile)) {
          rv = Preferences::GetCString(aName, prefValue);
          // The pref was reset. Clear the user file.
          if (NS_FAILED(rv) || prefValue.IsEmpty()) {
            LogModule::SetLogFile(nullptr);
            return;
          }

          // If the pref value doesn't have a PID placeholder, append it to the end.
          if (!strstr(prefValue.get(), MOZ_LOG_PID_TOKEN)) {
            prefValue.AppendLiteral(MOZ_LOG_PID_TOKEN);
          }

          LogModule::SetLogFile(prefValue.BeginReading());
        } else if (prefName.EqualsLiteral(kLoggingPrefAddTimestamp)) {
          bool addTimestamp = Preferences::GetBool(aName, false);
          LogModule::SetAddTimestamp(addTimestamp);
        } else if (prefName.EqualsLiteral(kLoggingPrefSync)) {
          bool sync = Preferences::GetBool(aName, false);
          LogModule::SetIsSync(sync);
        }
        return;
      }

      if (Preferences::GetInt(aName, &prefLevel) == NS_OK) {
        logLevel = ToLogLevel(prefLevel);
      } else if (Preferences::GetCString(aName, prefValue) == NS_OK) {
        if (prefValue.LowerCaseEqualsLiteral("error")) {
          logLevel = LogLevel::Error;
        } else if (prefValue.LowerCaseEqualsLiteral("warning")) {
          logLevel = LogLevel::Warning;
        } else if (prefValue.LowerCaseEqualsLiteral("info")) {
          logLevel = LogLevel::Info;
        } else if (prefValue.LowerCaseEqualsLiteral("debug")) {
          logLevel = LogLevel::Debug;
        } else if (prefValue.LowerCaseEqualsLiteral("verbose")) {
          logLevel = LogLevel::Verbose;
        }
      }

      const char* moduleName = aName + strlen(kLoggingPrefPrefix);
      LogModule::Get(moduleName)->SetLevel(logLevel);
    }

    static void LoadExistingPrefs() {
      nsIPrefBranch* root = Preferences::GetRootBranch();
      if (!root) {
        return;
      }

      nsTArray names;
      nsresult rv = root->GetChildList(kLoggingPrefPrefix, names);
      if (NS_SUCCEEDED(rv)) {
        for (auto& name : names) {
          LoadPrefValue(name.get());
        }
      }
    }

    LogModulePrefWatcher::LogModulePrefWatcher() = default;

    void LogModulePrefWatcher::RegisterPrefWatcher() {
      RefPtr prefWatcher = new LogModulePrefWatcher();
      Preferences::AddStrongObserver(prefWatcher, kLoggingPrefPrefix);

      nsCOMPtr observerService =
          mozilla::services::GetObserverService();
      if (observerService && XRE_IsParentProcess()) {
        observerService->AddObserver(prefWatcher,
                                     "browser-delayed-startup-finished", false);
      }

      LoadExistingPrefs();
    }

    NS_IMETHODIMP
    LogModulePrefWatcher::Observe(nsISupports* aSubject, const char* aTopic,
                                  const char16_t* aData) {
      if (strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic) == 0) {
        NS_LossyConvertUTF16toASCII prefName(aData);
        LoadPrefValue(prefName.get());
      } else if (strcmp("browser-delayed-startup-finished", aTopic) == 0) {
        bool clear = Preferences::GetBool(kLoggingPrefClearOnStartup, true);
        if (clear) {
          ResetExistingPrefs();
        }
        nsCOMPtr observerService =
            mozilla::services::GetObserverService();
        if (observerService) {
          observerService->RemoveObserver(this, "browser-delayed-startup-finished");
        }
      }

      return NS_OK;
    }

    }  // namespace mozilla