log and format for wstrings.

This commit is contained in:
Paul Rouget 2020-06-08 12:32:00 +02:00
parent 60ee588c71
commit 805b79b32c
10 changed files with 37 additions and 57 deletions

View file

@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "pch.h" #include "pch.h"
#include "logs.h" #include "strutils.h"
#include "BrowserPage.h" #include "BrowserPage.h"
#include "BrowserPage.g.cpp" #include "BrowserPage.g.cpp"
#include "DefaultUrl.h" #include "DefaultUrl.h"
@ -103,7 +103,7 @@ void BrowserPage::LoadServoURI(Uri uri) {
auto scheme = uri.SchemeName(); auto scheme = uri.SchemeName();
if (scheme != SERVO_SCHEME) { if (scheme != SERVO_SCHEME) {
log("Unexpected URL: ", uri.RawUri().c_str()); log(L"Unexpected URL: ", uri.RawUri().c_str());
return; return;
} }
std::wstring raw{uri.RawUri()}; std::wstring raw{uri.RawUri()};

View file

@ -119,7 +119,7 @@
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="logs.h" /> <ClInclude Include="strutils.h" />
<ClInclude Include="pch.h" /> <ClInclude Include="pch.h" />
<ClInclude Include="App.h"> <ClInclude Include="App.h">
<DependentUpon>App.xaml</DependentUpon> <DependentUpon>App.xaml</DependentUpon>
@ -923,7 +923,6 @@
<DependentUpon>BrowserPage.xaml</DependentUpon> <DependentUpon>BrowserPage.xaml</DependentUpon>
</ClCompile> </ClCompile>
<ClCompile Include="$(GeneratedFilesDir)module.g.cpp" /> <ClCompile Include="$(GeneratedFilesDir)module.g.cpp" />
<ClCompile Include="logs.cpp" />
<ClCompile Include="ServoControl\OpenGLES.cpp" /> <ClCompile Include="ServoControl\OpenGLES.cpp" />
<ClCompile Include="ServoControl\Servo.cpp" /> <ClCompile Include="ServoControl\Servo.cpp" />
<ClCompile Include="ServoControl\ServoControl.cpp" /> <ClCompile Include="ServoControl\ServoControl.cpp" />

View file

@ -10,7 +10,6 @@
<ItemGroup> <ItemGroup>
<ClCompile Include="pch.cpp" /> <ClCompile Include="pch.cpp" />
<ClCompile Include="$(GeneratedFilesDir)module.g.cpp" /> <ClCompile Include="$(GeneratedFilesDir)module.g.cpp" />
<ClCompile Include="logs.cpp" />
<ClCompile Include="BrowserPage.cpp" /> <ClCompile Include="BrowserPage.cpp" />
<ClCompile Include="App.cpp" /> <ClCompile Include="App.cpp" />
<ClCompile Include="ServoControl\OpenGLES.cpp"> <ClCompile Include="ServoControl\OpenGLES.cpp">
@ -25,7 +24,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="pch.h" /> <ClInclude Include="pch.h" />
<ClInclude Include="logs.h" />
<ClInclude Include="BrowserPage.h" /> <ClInclude Include="BrowserPage.h" />
<ClInclude Include="App.h" /> <ClInclude Include="App.h" />
<ClInclude Include="ServoControl\OpenGLES.h"> <ClInclude Include="ServoControl\OpenGLES.h">
@ -38,6 +36,7 @@
<Filter>ServoControl</Filter> <Filter>ServoControl</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="DefaultUrl.h" /> <ClInclude Include="DefaultUrl.h" />
<ClInclude Include="strutils.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Image Include="Assets\Wide310x150Logo.scale-200.png"> <Image Include="Assets\Wide310x150Logo.scale-200.png">

View file

@ -3,7 +3,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#include "pch.h" #include "pch.h"
#include "logs.h" #include "strutils.h"
#include "OpenGLES.h" #include "OpenGLES.h"
using namespace winrt::Windows::UI::Xaml::Controls; using namespace winrt::Windows::UI::Xaml::Controls;
@ -13,7 +13,7 @@ using namespace winrt::Windows::Foundation::Collections;
OpenGLES::OpenGLES() OpenGLES::OpenGLES()
: mEglConfig(nullptr), mEglDisplay(EGL_NO_DISPLAY), : mEglConfig(nullptr), mEglDisplay(EGL_NO_DISPLAY),
mEglContext(EGL_NO_CONTEXT) { mEglContext(EGL_NO_CONTEXT) {
log("OpenGLES::OpenGLES()"); log(L"OpenGLES::OpenGLES()");
Initialize(); Initialize();
} }

View file

@ -178,7 +178,7 @@ Servo::Servo(hstring url, hstring args, GLsizei width, GLsizei height,
} else if (type == Windows::Foundation::PropertyType::Empty) { } else if (type == Windows::Foundation::PropertyType::Empty) {
pref.pref_type = capi::CPrefType::Missing; pref.pref_type = capi::CPrefType::Missing;
} else { } else {
log("skipping pref %s. Unknown type", key); log(L"skipping pref %s. Unknown type", key);
continue; continue;
} }
cprefs.push_back(pref); cprefs.push_back(pref);

View file

@ -6,7 +6,7 @@
#include "pch.h" #include "pch.h"
#include <EGL/egl.h> #include <EGL/egl.h>
#include "logs.h" #include "strutils.h"
#include <stdlib.h> #include <stdlib.h>
namespace winrt::servo { namespace winrt::servo {

View file

@ -1,4 +1,5 @@
#include "pch.h" #include "pch.h"
#include "strutils.h"
#include "ServoControl.h" #include "ServoControl.h"
#include "ServoControl.g.cpp" #include "ServoControl.g.cpp"
#include "Pref.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. // Doesn't look like a URI. Let's search for the string.
auto escapedInput = Uri::EscapeComponent(input); auto escapedInput = Uri::EscapeComponent(input);
std::wstring searchUri = std::wstring searchUri = unbox_value<hstring>(std::get<1>(Servo::GetPref(L"shell.searchpage"))).c_str();
unbox_value<hstring>(std::get<1>(Servo::GetPref(L"shell.searchpage"))) std::wstring formated = format(searchUri, escapedInput.c_str());
.c_str(); hstring finalUri{formated};
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};
TryLoadUri(finalUri); TryLoadUri(finalUri);
return finalUri; return finalUri;
} }
@ -323,10 +317,10 @@ void ServoControl::RunOnGLThread(std::function<void()> task) {
/**** GL THREAD LOOP ****/ /**** GL THREAD LOOP ****/
void ServoControl::Loop() { void ServoControl::Loop() {
log("BrowserPage::Loop(). GL thread: %i", GetCurrentThreadId()); log(L"BrowserPage::Loop(). GL thread: %i", GetCurrentThreadId());
if (mServo == nullptr) { if (mServo == nullptr) {
log("Entering loop"); log(L"Entering loop");
ServoDelegate *sd = static_cast<ServoDelegate *>(this); ServoDelegate *sd = static_cast<ServoDelegate *>(this);
EGLNativeWindowType win = GetNativeWindow(); EGLNativeWindowType win = GetNativeWindow();
mServo = std::make_unique<Servo>(mInitialURL, mArgs, mPanelWidth, mServo = std::make_unique<Servo>(mInitialURL, mArgs, mPanelWidth,
@ -367,7 +361,7 @@ void ServoControl::StartRenderLoop() {
#endif #endif
} }
mLooping = true; mLooping = true;
log("BrowserPage::StartRenderLoop(). UI thread: %i", GetCurrentThreadId()); log(L"BrowserPage::StartRenderLoop(). UI thread: %i", GetCurrentThreadId());
auto task = Concurrency::create_task([=] { Loop(); }); auto task = Concurrency::create_task([=] { Loop(); });
mLoopTask = std::make_unique<Concurrency::task<void>>(task); mLoopTask = std::make_unique<Concurrency::task<void>>(task);
} }

View file

@ -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);
}

View file

@ -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, ...);

View file

@ -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 <typename... Args>
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 <typename... Args> void log(const std::wstring &txt, Args... args) {
OutputDebugString((format(txt, args...) + L"\r\n").c_str());
}