Auto merge of #26196 - jdm:stdout-log, r=Manishearth

Dump all UWP logging output to a file

These changes ensure that we keep getting debug output in the VS output window, but we also write each message to a file in the app's local data directory. These changes also extend the C API a bit to support more generic logging facilities, removing some of the Windows-specific nature of the VSLogger. Fixes part of #23813.
This commit is contained in:
bors-servo 2020-04-17 14:45:32 -04:00 committed by GitHub
commit aa37904bbd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 70 additions and 30 deletions

View file

@ -36,6 +36,10 @@ void on_animating_changed(bool aAnimating) {
}
void on_panic(const char *backtrace) {
if (sLogHandle != INVALID_HANDLE_VALUE) {
CloseHandle(sLogHandle);
sLogHandle = INVALID_HANDLE_VALUE;
}
throw hresult_error(E_FAIL, char2hstring(backtrace));
}
@ -86,6 +90,23 @@ void on_devtools_started(Servo::DevtoolsServerState result,
result == Servo::DevtoolsServerState::Started, port);
}
void on_log_output(const char *buffer, uint32_t buffer_length) {
OutputDebugStringA(buffer);
if (sLogHandle == INVALID_HANDLE_VALUE) {
return;
}
DWORD bytesWritten;
auto writeResult =
WriteFile(sLogHandle, buffer, buffer_length, &bytesWritten, nullptr);
if (writeResult == FALSE || bytesWritten != buffer_length)
throw std::runtime_error(
"Failed to write log message to the log file: error code " +
std::to_string(GetLastError()));
}
Servo::PromptResult prompt_ok_cancel(const char *message, bool trusted) {
return sServo->Delegate().OnServoPromptOkCancel(char2hstring(message),
trusted);
@ -141,6 +162,22 @@ Servo::Servo(hstring url, hstring args, GLsizei width, GLsizei height,
sServo = this; // FIXME;
#ifdef _DEBUG
auto current = winrt::Windows::Storage::ApplicationData::Current();
auto filePath = std::wstring(current.LocalFolder().Path()) + L"\\stdout.txt";
sLogHandle =
CreateFile2(filePath.c_str(), GENERIC_WRITE, 0, CREATE_ALWAYS, nullptr);
if (sLogHandle == INVALID_HANDLE_VALUE)
throw std::runtime_error("Failed to open the log file: error code " +
std::to_string(GetLastError()));
if (SetFilePointer(sLogHandle, 0, nullptr, FILE_END) ==
INVALID_SET_FILE_POINTER)
throw std::runtime_error(
"Failed to set file pointer to the end of file: error code " +
std::to_string(GetLastError()));
#endif
capi::CHostCallbacks c;
c.flush = &flush;
c.make_current = &make_current;
@ -164,13 +201,18 @@ Servo::Servo(hstring url, hstring args, GLsizei width, GLsizei height,
c.prompt_input = &prompt_input;
c.on_devtools_started = &on_devtools_started;
c.show_context_menu = &show_context_menu;
c.on_log_output = &on_log_output;
capi::register_panic_handler(&on_panic);
capi::init_with_egl(o, &wakeup, c);
}
Servo::~Servo() { sServo = nullptr; }
Servo::~Servo() {
sServo = nullptr;
if (sLogHandle != INVALID_HANDLE_VALUE)
CloseHandle(sLogHandle);
}
winrt::hstring char2hstring(const char *c_str) {
// FIXME: any better way of doing this?