How to print glibmm(glib) UTF-8 exception in the C locale?

Hi,

In GParted we’ve got code like this:

    try {
        Glib::spawn_async_with_pipes(...);
    } catch (Glib::SpawnError &e) {
        std::cerr << e.what() << std::endl;
    }

When the exception is caught it prints the error like this and GParted carries on. Note the unicode quotation marks around the missing btrfs program name:

    # gparted
    ...
    Failed to execute child process “btrfs” (No such file or directory)

However when the user runs GParted in the C locale it hangs forever like this:

    # LANG=C gparted
    ...
    (gpartedbin-1.6.0-master-53-ged5623ff:1461271): glibmm-CRITICAL **: 08:54:42.030: 
    unhandled exception (type Glib::Error) in signal handler:
    domain: g_convert_error
    code  : 1
    what  : Invalid byte sequence in conversion input

I understand that I can change the error printing line to this to just bypass ustring’s character set conversion and print the UTF-8 characters to stderr regardless:

        std::cerr << std::string(e.what()) << std::endl;
  1. However is there a proper way to convert and print this glibmm(glib) exception containing UTF-8 characters in the C locale?

  2. Is there a reason glib needs to use unicode quote marks in gspawn-posix.c, or should it use plain ascii quote marks so the message is valid in the C locale (all locales) without conversion?

Thanks,
Mike

This (untested code) should give you something like what g_log() does to utf8 log messages outputed to stderr

std::string charset;
Glib::get_charset(charset);
std::cerr << Glib::convert_with_fallback(e.what().raw(), charset, "UTF-8", "?") << '\n';

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.