Skip to content

fix: handle wide-character output on Windows console to avoid truncation #3767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

a492219408
Copy link

@a492219408 a492219408 commented Mar 29, 2025

Description

This change fixes a bug where the console would stop printing any subsequent output if it encountered Chinese characters. By converting the wide-string output to UTF-8 with converter.to_bytes(), the console now continues to properly display messages containing Chinese characters without interruption.

Screenshot

Snipaste_2025-03-27_23-16-13 Snipaste_2025-03-27_23-16-44

Issues Fixed or Closed

No specific issue references.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Dependency update (updates to dependencies)
  • Documentation update (changes to documentation)
  • Repository update (changes to repository files, e.g. .github/...)

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated the in code docstring/documentation-blocks for new or existing methods/components

Copy link

codecov bot commented Mar 29, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 11.59%. Comparing base (f921ae4) to head (bbbac2d).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3767   +/-   ##
=======================================
  Coverage   11.59%   11.59%           
=======================================
  Files          92       92           
  Lines       17358    17358           
  Branches     8106     8106           
=======================================
  Hits         2013     2013           
  Misses      12853    12853           
  Partials     2492     2492           
Flag Coverage Δ
Linux 11.25% <ø> (ø)
Windows 13.00% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@FrogTheFrog
Copy link
Collaborator

FrogTheFrog commented Mar 31, 2025

What if we change the code to look some thing like this?

void print_device(device_t &device) {
    audio::wstring_t wstring;
    DWORD device_state;

    device->GetState(&device_state);
    device->GetId(&wstring);

    audio::prop_t prop;
    device->OpenPropertyStore(STGM_READ, &prop);

    prop_var_t adapter_friendly_name;
    prop_var_t device_friendly_name;
    prop_var_t device_desc;

    prop->GetValue(PKEY_Device_FriendlyName, &device_friendly_name.prop);
    prop->GetValue(PKEY_DeviceInterface_FriendlyName, &adapter_friendly_name.prop);
    prop->GetValue(PKEY_Device_DeviceDesc, &device_desc.prop);

    if (!(device_state & device_state_filter)) {
      return;
    }

    std::string device_state_string = "Unknown"s;
    switch (device_state) {
      case DEVICE_STATE_ACTIVE:
        device_state_string = "Active"s;
        break;
      case DEVICE_STATE_DISABLED:
        device_state_string = "Disabled"s;
        break;
      case DEVICE_STATE_UNPLUGGED:
        device_state_string = "Unplugged"s;
        break;
      case DEVICE_STATE_NOTPRESENT:
        device_state_string = "Not present"s;
        break;
    }

    std::string current_format = "Unknown"s;
    for (const auto &format : formats) {
      // This will fail for any format that's not the mix format for this device,
      // so we can take the first match as the current format to display.
      auto audio_client = make_audio_client(device, format);
      if (audio_client) {
        current_format = format.name.data();
        break;
      }
    }

    std::cout
      << "===== Device ====="sv << std::endl
      << "Device ID          : "sv << platf::to_utf8(wstring.get()) << std::endl
      << "Device name        : "sv << platf::to_utf8(no_null((LPWSTR) device_friendly_name.prop.pszVal)) << std::endl
      << "Adapter name       : "sv << platf::to_utf8(no_null((LPWSTR) adapter_friendly_name.prop.pszVal)) << std::endl
      << "Device description : "sv << platf::to_utf8(no_null((LPWSTR) device_desc.prop.pszVal)) << std::endl
      << "Device state       : "sv << device_state_string << std::endl
      << "Current format     : "sv << current_format << std::endl
      << std::endl;
  }

where the platf::to_utf8 is from https://github.com/LizardByte/Sunshine/blob/master/src/platform/windows/misc.h#L35? Getting rid of the static std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter; and std::wcout altogether.

I have not checked if it compiles, but please do try it out.

Copy link
Member

@ReenigneArcher ReenigneArcher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please try the suggestions from Frog.

@ReenigneArcher ReenigneArcher added the question Further information is requested label Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants