add a single-element ring-buffer write function
[ardour.git] / libs / pbd / locale_guard.cc
index 62318c6445bbc0024f38282b3aeebec07c51d4d7..b4225d38758359f08f1c8d02074f4c97b9ffa874 100644 (file)
 #include <assert.h>
 #include <locale.h>
 
+#include "pbd/compose.h"
+#include "pbd/debug.h"
+#include "pbd/error.h"
 #include "pbd/locale_guard.h"
 
 using namespace PBD;
 
 /* The initial C++ locale is "C" regardless of the user's preferred locale.
- * and affects std::sprintf() et al from <cstdio>
- *
- * the C locale from setlocale() matches the user's preferred locale
- * and effects ::sprintf() et al from <stdio.h>
+ * The C locale from setlocale() matches the user's preferred locale
  *
  * Setting the C++ locale will change the C locale, but not the other way 'round.
  * and some plugin may change either behind our back.
  */
 
-LocaleGuard::LocaleGuard (const char*)
-       : old_c (0)
-{
-       init ();
-}
-
 LocaleGuard::LocaleGuard ()
        : old_c (0)
-{
-       init ();
-}
-
-void
-LocaleGuard::init ()
 {
        char* actual = setlocale (LC_NUMERIC, NULL);
        if (strcmp ("C", actual)) {
+               DEBUG_TRACE (DEBUG::Locale, string_compose ("LG: change locale from '%1' to 'C'\n", actual));
                /* purpose of LocaleGuard is to make sure we're using "C" for
                   the numeric locale during its lifetime, so make it so.
                */
@@ -60,7 +49,10 @@ LocaleGuard::init ()
                /* this changes both C++ and C locale */
                std::locale::global (std::locale (std::locale::classic(), "C", std::locale::numeric));
        }
-       assert (old_cpp == std::locale::classic ());
+       if (old_cpp != std::locale::classic ()) {
+               PBD::error << "LocaleGuard: initial C++ locale is not 'C'. Expect non-portable session files.\n";
+               DEBUG_TRACE (DEBUG::Locale, "LG: initial C++ locale is not 'C'. Expect non-portable session files.\n");
+       }
 }
 
 LocaleGuard::~LocaleGuard ()
@@ -75,11 +67,16 @@ LocaleGuard::~LocaleGuard ()
                 *
                 * if it's not: some plugin meddled with it.
                 */
-               assert (old_cpp == std::locale::classic ());
+               if (old_cpp != std::locale::classic ()) {
+                       PBD::error << "LocaleGuard: someone (a plugin) changed the C++ locale, expect non-portable session files.\n";
+                       DEBUG_TRACE (DEBUG::Locale, "LG: someone (a plugin) changed the C++ locale, expect non-portable session files.\n");
+               }
                std::locale::global (old_cpp);
+               DEBUG_TRACE (DEBUG::Locale, string_compose ("LG: restore C++ locale: '%1'\n", old_cpp.name ()));
        }
        if (old_c && strcmp (old_c, actual)) {
                setlocale (LC_NUMERIC, old_c);
+               DEBUG_TRACE (DEBUG::Locale, string_compose ("LG: restore C locale: '%1'\n", old_c));
        }
        free (old_c);
 }