From 805b79b32ca009d7c8afc45a02e155d3f925d86c Mon Sep 17 00:00:00 2001 From: Paul Rouget Date: Mon, 8 Jun 2020 12:32:00 +0200 Subject: [PATCH] log and format for wstrings. --- support/hololens/ServoApp/BrowserPage.cpp | 4 +-- support/hololens/ServoApp/ServoApp.vcxproj | 3 +-- .../ServoApp/ServoApp.vcxproj.filters | 3 +-- .../ServoApp/ServoControl/OpenGLES.cpp | 4 +-- .../hololens/ServoApp/ServoControl/Servo.cpp | 2 +- .../hololens/ServoApp/ServoControl/Servo.h | 2 +- .../ServoApp/ServoControl/ServoControl.cpp | 20 +++++--------- support/hololens/ServoApp/logs.cpp | 27 ------------------- support/hololens/ServoApp/logs.h | 7 ----- support/hololens/ServoApp/strutils.h | 22 +++++++++++++++ 10 files changed, 37 insertions(+), 57 deletions(-) delete mode 100644 support/hololens/ServoApp/logs.cpp delete mode 100644 support/hololens/ServoApp/logs.h create mode 100644 support/hololens/ServoApp/strutils.h diff --git a/support/hololens/ServoApp/BrowserPage.cpp b/support/hololens/ServoApp/BrowserPage.cpp index a2b13bf60b95..31a056be6487 100644 --- a/support/hololens/ServoApp/BrowserPage.cpp +++ b/support/hololens/ServoApp/BrowserPage.cpp @@ -3,7 +3,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #include "pch.h" -#include "logs.h" +#include "strutils.h" #include "BrowserPage.h" #include "BrowserPage.g.cpp" #include "DefaultUrl.h" @@ -103,7 +103,7 @@ void BrowserPage::LoadServoURI(Uri uri) { auto scheme = uri.SchemeName(); if (scheme != SERVO_SCHEME) { - log("Unexpected URL: ", uri.RawUri().c_str()); + log(L"Unexpected URL: ", uri.RawUri().c_str()); return; } std::wstring raw{uri.RawUri()}; diff --git a/support/hololens/ServoApp/ServoApp.vcxproj b/support/hololens/ServoApp/ServoApp.vcxproj index 9d0c1c7716b2..04c559bfa011 100644 --- a/support/hololens/ServoApp/ServoApp.vcxproj +++ b/support/hololens/ServoApp/ServoApp.vcxproj @@ -119,7 +119,7 @@ - + App.xaml @@ -923,7 +923,6 @@ BrowserPage.xaml - diff --git a/support/hololens/ServoApp/ServoApp.vcxproj.filters b/support/hololens/ServoApp/ServoApp.vcxproj.filters index 39acc9fc3c38..399755d5d3f4 100644 --- a/support/hololens/ServoApp/ServoApp.vcxproj.filters +++ b/support/hololens/ServoApp/ServoApp.vcxproj.filters @@ -10,7 +10,6 @@ - @@ -25,7 +24,6 @@ - @@ -38,6 +36,7 @@ ServoControl + diff --git a/support/hololens/ServoApp/ServoControl/OpenGLES.cpp b/support/hololens/ServoApp/ServoControl/OpenGLES.cpp index a89e5e373d4d..5906bbfb5c7e 100644 --- a/support/hololens/ServoApp/ServoControl/OpenGLES.cpp +++ b/support/hololens/ServoApp/ServoControl/OpenGLES.cpp @@ -3,7 +3,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #include "pch.h" -#include "logs.h" +#include "strutils.h" #include "OpenGLES.h" using namespace winrt::Windows::UI::Xaml::Controls; @@ -13,7 +13,7 @@ using namespace winrt::Windows::Foundation::Collections; OpenGLES::OpenGLES() : mEglConfig(nullptr), mEglDisplay(EGL_NO_DISPLAY), mEglContext(EGL_NO_CONTEXT) { - log("OpenGLES::OpenGLES()"); + log(L"OpenGLES::OpenGLES()"); Initialize(); } diff --git a/support/hololens/ServoApp/ServoControl/Servo.cpp b/support/hololens/ServoApp/ServoControl/Servo.cpp index a53ea2489f17..65205693aa64 100644 --- a/support/hololens/ServoApp/ServoControl/Servo.cpp +++ b/support/hololens/ServoApp/ServoControl/Servo.cpp @@ -178,7 +178,7 @@ Servo::Servo(hstring url, hstring args, GLsizei width, GLsizei height, } else if (type == Windows::Foundation::PropertyType::Empty) { pref.pref_type = capi::CPrefType::Missing; } else { - log("skipping pref %s. Unknown type", key); + log(L"skipping pref %s. Unknown type", key); continue; } cprefs.push_back(pref); diff --git a/support/hololens/ServoApp/ServoControl/Servo.h b/support/hololens/ServoApp/ServoControl/Servo.h index 025b0f30c3fd..7484c778f883 100644 --- a/support/hololens/ServoApp/ServoControl/Servo.h +++ b/support/hololens/ServoApp/ServoControl/Servo.h @@ -6,7 +6,7 @@ #include "pch.h" #include -#include "logs.h" +#include "strutils.h" #include namespace winrt::servo { diff --git a/support/hololens/ServoApp/ServoControl/ServoControl.cpp b/support/hololens/ServoApp/ServoControl/ServoControl.cpp index cca58cce0aca..280e86778e0f 100644 --- a/support/hololens/ServoApp/ServoControl/ServoControl.cpp +++ b/support/hololens/ServoApp/ServoControl/ServoControl.cpp @@ -1,4 +1,5 @@ #include "pch.h" +#include "strutils.h" #include "ServoControl.h" #include "ServoControl.g.cpp" #include "Pref.g.cpp" @@ -277,16 +278,9 @@ hstring ServoControl::LoadURIOrSearch(hstring input) { // Doesn't look like a URI. Let's search for the string. auto escapedInput = Uri::EscapeComponent(input); - std::wstring searchUri = - unbox_value(std::get<1>(Servo::GetPref(L"shell.searchpage"))) - .c_str(); - std::wstring keyword = L"%s"; - size_t start_pos = searchUri.find(keyword); - if (start_pos == std::string::npos) - searchUri = searchUri + escapedInput; - else - searchUri.replace(start_pos, keyword.length(), escapedInput); - hstring finalUri{searchUri}; + std::wstring searchUri = unbox_value(std::get<1>(Servo::GetPref(L"shell.searchpage"))).c_str(); + std::wstring formated = format(searchUri, escapedInput.c_str()); + hstring finalUri{formated}; TryLoadUri(finalUri); return finalUri; } @@ -323,10 +317,10 @@ void ServoControl::RunOnGLThread(std::function task) { /**** GL THREAD LOOP ****/ void ServoControl::Loop() { - log("BrowserPage::Loop(). GL thread: %i", GetCurrentThreadId()); + log(L"BrowserPage::Loop(). GL thread: %i", GetCurrentThreadId()); if (mServo == nullptr) { - log("Entering loop"); + log(L"Entering loop"); ServoDelegate *sd = static_cast(this); EGLNativeWindowType win = GetNativeWindow(); mServo = std::make_unique(mInitialURL, mArgs, mPanelWidth, @@ -367,7 +361,7 @@ void ServoControl::StartRenderLoop() { #endif } mLooping = true; - log("BrowserPage::StartRenderLoop(). UI thread: %i", GetCurrentThreadId()); + log(L"BrowserPage::StartRenderLoop(). UI thread: %i", GetCurrentThreadId()); auto task = Concurrency::create_task([=] { Loop(); }); mLoopTask = std::make_unique>(task); } diff --git a/support/hololens/ServoApp/logs.cpp b/support/hololens/ServoApp/logs.cpp deleted file mode 100644 index 854684e6c202..000000000000 --- a/support/hololens/ServoApp/logs.cpp +++ /dev/null @@ -1,27 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ - -#include "pch.h" - -void log(const char *format, ...) { - char buf[4096], *p = buf; - va_list args; - int n; - - va_start(args, format); - n = vsnprintf(p, sizeof buf - 3, format, args); - va_end(args); - - p += (n < 0) ? sizeof buf - 3 : n; - - while (p > buf && isspace(p[-1])) { - *--p = '\0'; - } - - *p++ = '\r'; - *p++ = '\n'; - *p = '\0'; - - OutputDebugStringA(buf); -} \ No newline at end of file diff --git a/support/hololens/ServoApp/logs.h b/support/hololens/ServoApp/logs.h deleted file mode 100644 index d71fd3e1233e..000000000000 --- a/support/hololens/ServoApp/logs.h +++ /dev/null @@ -1,7 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ - -#pragma once - -void log(const char *format, ...); \ No newline at end of file diff --git a/support/hololens/ServoApp/strutils.h b/support/hololens/ServoApp/strutils.h new file mode 100644 index 000000000000..22198332c90f --- /dev/null +++ b/support/hololens/ServoApp/strutils.h @@ -0,0 +1,22 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +#pragma once + +template +std::wstring format(const std::wstring &txt, Args... args) { + size_t size = swprintf(nullptr, 0, txt.c_str(), args...) + 1; + if (size <= 0) { + throw std::runtime_error("Error during formatting."); + } + auto ptr = new wchar_t[size]; + swprintf(ptr, size, txt.c_str(), args...); + auto wstr = std::wstring(ptr); + delete ptr; + return wstr; +} + +template void log(const std::wstring &txt, Args... args) { + OutputDebugString((format(txt, args...) + L"\r\n").c_str()); +}