Repository: microsoft/terminal
File: src/cascadia/TerminalApp/AppLogic.cpp (C++)

Code before:
        TraceLoggingWrite(
            g_hTerminalAppProvider,
            "ThemesInUse",
            TraceLoggingDescription("Data about the themes in use"),
            TraceLoggingBool(usingDefaultTheme, "UsingDefaultTheme"),
            TraceLoggingInt32(numThemes, "NumThemes"),
            TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES),
            TelemetryPrivacyDataTag(PDT_ProductAndServiceUsage));

        // --------------------------- RE: sendInput ---------------------------

        auto totalSendInput = 0;
        const auto& allActions = _settings.GlobalSettings().ActionMap().AvailableActions();
        for (const auto&& [name, actionAndArgs] : allActions)
        {
            if (actionAndArgs.Action() == ShortcutAction::SendInput)
                totalSendInput++;
        }

        TraceLoggingWrite(
            g_hTerminalAppProvider,
            "SendInputUsage",
            TraceLoggingDescription("Data about usage of sendInput"),
            TraceLoggingInt32(totalSendInput, "NumSendInputActions"),
            TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES),
            TelemetryPrivacyDataTag(PDT_ProductAndServiceUsage));

        // ------------------------ RE: automarkPrompts ------------------------
        auto totalAutoMark = 0;
        auto totalShowMarks = 0;
        for (const auto&& p : _settings.AllProfiles())
        {
            totalAutoMark += p.AutoMarkPrompts() ? 1 : 0;
            totalShowMarks += p.ShowMarks() ? 1 : 0;
        }
        TraceLoggingWrite(
            g_hTerminalAppProvider,
            "MarksProfilesUsage",
            TraceLoggingDescription("Data about usage of scrollbar marks"),
            TraceLoggingInt32(totalAutoMark, "NumProfilesAutoMarkPrompts"),
            TraceLoggingInt32(totalShowMarks, "NumProfilesShowMarks"),
            TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES),
            TelemetryPrivacyDataTag(PDT_ProductAndServiceUsage));
    }

    void AppLogic::Quit()
    {
        if (_root)
        {
            _root->CloseWindow(true);
        }

Review comment (suggestion):
Can you do something more like this...


```
auto getTotalSendInputCount = [&](){
    // calculate how many Send Input actions there are
}

...

TraceLoggingInt32(getTotalSendInputCount(), "NumSendInputActions"),
...
```

`TraceLogging` is intentionally very lightweight. It only evaluates its arguments if tracing is enabled. Very, very explicitly.

It compiles to code like this.

```
if (_globalTraceLoggingHookEnabled_g_hTerminalAppProvider) {
   // all the TraceLoggingWrite code
}

```

If we don't guard evaluations safely, we effectively waste a bunch of CPU time for an event that isn't being listened to.

Code after:
    //   displays buttons (or a single button). Two buttons (primary and secondary)
    //   will be displayed if this is an warning dialog for closing the terminal,
    //   this allows the users to abandon the closing action. Otherwise, a single
    //   close button will be displayed.
    // - Only one dialog can be visible at a time. If another dialog is visible
    //   when this is called, nothing happens.
    // Arguments:
    // - dialog: the dialog object that is going to show up
    // Return value:
    // - an IAsyncOperation with the dialog result
    winrt::Windows::Foundation::IAsyncOperation<ContentDialogResult> AppLogic::ShowDialog(winrt::Windows::UI::Xaml::Controls::ContentDialog dialog)
    {
        // DON'T release this lock in a wil::scope_exit. The scope_exit will get
        // called when we await, which is not what we want.
        std::unique_lock lock{ _dialogLock, std::try_to_lock };
        if (!lock)
        {
            // Another dialog is visible.
            co_return ContentDialogResult::None;
        }

        _dialog = dialog;

        // IMPORTANT: This is necessary as documented in the ContentDialog MSDN docs.
        // Since we're hosting the dialog in a Xaml island, we need to connect it to the
        // xaml tree somehow.
        dialog.XamlRoot(_root->XamlRoot());

        // IMPORTANT: Set the requested theme of the dialog, because the
        // PopupRoot isn't directly in the Xaml tree of our root. So the dialog
        // won't inherit our RequestedTheme automagically.
        // GH#5195, GH#3654 Because we cannot set RequestedTheme at the application level,
        // we occasionally run into issues where parts of our UI end up themed incorrectly.
        // Dialogs, for example, live under a different Xaml root element than the rest of
        // our application. This makes our popup menus and buttons "disappear" when the
        // user wants Terminal to be in a different theme than the rest of the system.
        // This hack---and it _is_ a hack--walks up a dialog's ancestry and forces the
        // theme on each element up to the root. We're relying a bit on Xaml's implementation
        // details here, but it does have the desired effect.
        // It's not enough to set the theme on the dialog alone.
        auto themingLambda{ [this](const Windows::Foundation::IInspectable& sender, const RoutedEventArgs&) {
            auto theme{ _settings.GlobalSettings().CurrentTheme() };
            auto requestedTheme{ theme.RequestedTheme() };
            auto element{ sender.try_as<winrt::Windows::UI::Xaml::FrameworkElement>() };
            while (element)
            {
                element.RequestedTheme(requestedTheme);
                element = element.Parent().try_as<winrt::Windows::UI::Xaml::FrameworkElement>();
            }
        } };