Skip to content
Snippets Groups Projects
Commit eeef17c8 authored by Nicolas Jager's avatar Nicolas Jager
Browse files

Ringdebug and utils

Change-Id: I7144be0052eb7d1900d17b31edbfe48d0498d7db
parent 8a85e1f2
No related branches found
No related tags found
No related merge requests found
......@@ -30,6 +30,7 @@
</Grid.RowDefinitions>
<ScrollViewer x:Name="_scrollView_"
Grid.Row="0"
Padding="5,5"
Style="{StaticResource ConsoleScrollViewerStyle}">
<RichTextBlock x:Name="_debugWindowOutput_"
Style="{StaticResource ConsoleTextStyle1}"/>
......@@ -42,8 +43,10 @@
</Grid.ColumnDefinitions>
<TextBox x:Name="_tBoxDbg_"
Grid.Column="0"
KeyDown="_sendDbgCmd__KeyDown"
Style="{StaticResource ConsoleTextBoxStyle}"/>
<Button x:Name="_btnSendDbgCmd_"
Click="_btnSendDbgCmd__Click"
Grid.Column="1"
Style="{StaticResource ButtonSendCmdStyle}"/>
</Grid>
......
......@@ -21,8 +21,96 @@
using namespace RingClientUWP;
using namespace RingClientUWP::Views;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml::Documents;
RingConsolePanel::RingConsolePanel()
{
InitializeComponent();
RingDebug::instance->messageToScreen += ref new debugMessageToScreen([this](Platform::String^ message) {
output(message);
});
}
void
RingConsolePanel::output(Platform::String^ message)
{
try {
Run^ inlineText = ref new Run();
inlineText->Text = message;
Paragraph^ paragraph = ref new Paragraph();
paragraph->Inlines->Append(inlineText);
_debugWindowOutput_->Blocks->Append(paragraph);
_scrollView_->UpdateLayout();
_scrollView_->ScrollToVerticalOffset(_scrollView_->ScrollableHeight);
}
catch (Platform::Exception^ e) {
return;
}
}
void RingConsolePanel::_btnSendDbgCmd__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
sendCommand();
}
void RingConsolePanel::_sendDbgCmd__KeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e)
{
if (e->Key == Windows::System::VirtualKey::Enter && _tBoxDbg_->Text != "") {
sendCommand();
}
else if (e->Key == Windows::System::VirtualKey::PageUp) {
if (historyLevel < 1)
return;
if (historyLevel == historyCmds.Size)
currentCmd = _tBoxDbg_->Text;
historyLevel--;
_tBoxDbg_->Text = historyCmds.GetAt(historyLevel);
}
else if (e->Key == Windows::System::VirtualKey::PageDown) {
if (historyLevel < historyCmds.Size) {
_tBoxDbg_->Text = historyCmds.GetAt(historyLevel);
historyLevel++;
}
else {
_tBoxDbg_->Text = currentCmd;
}
return;
}
}
/*\ ADD EACH NEW COMMAND TO THE HELP LIST \*/
void RingConsolePanel::sendCommand()
{
auto cmdInput = _tBoxDbg_->Text;
addCommandToHistory();
historyLevel++;
_tBoxDbg_->Text = "";
currentCmd = "";
historyLevel = historyCmds.Size;
if (cmdInput == "") {
return;
}
else if (cmdInput == "help") {
MSG_(">> Help :");
MSG_("use PgUp/PgDown for crawling commands history.");
return;
}
std::wstring wStr(cmdInput->Begin());
std::string result(wStr.begin(), wStr.end());
MSG_(">> error, command \'" + result + "\' not found");
}
void RingConsolePanel::addCommandToHistory()
{
historyCmds.Append(_tBoxDbg_->Text);
}
\ No newline at end of file
......@@ -26,6 +26,19 @@ public ref class RingConsolePanel sealed
{
public:
RingConsolePanel();
void output(Platform::String^ message);
private:
void sendCommand();
void addCommandToHistory();
void _btnSendDbgCmd__Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e);
void _sendDbgCmd__KeyDown(Platform::Object^ sender, Windows::UI::Xaml::Input::KeyRoutedEventArgs^ e);
int historyLevel = 0;
/* commands already send */
Vector<String^> historyCmds;
/* command not already send */
String^ currentCmd;
};
}
}
\ No newline at end of file
/***************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jäger Nicolas <nicolas.jager@savoirfairelinux.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
**************************************************************************/
/* client */
#include "pch.h"
using namespace RingClientUWP;
using namespace Platform;
using namespace Windows::UI::Core;
void
RingDebug::print(const std::string& message,
const Type& type)
{
/* get the current time */
std::time_t currentTime = std::time(nullptr);
char timeBuffer[64];
ctime_s(timeBuffer, sizeof timeBuffer, &currentTime);
/* timestamp */
auto messageTimestamped = timeBuffer + message;
std::wstring wString = std::wstring(message.begin(), message.end());
/* set message type. */
switch (type) {
case Type::ERR:
wString = L"(EE) " + wString;
break;
case Type::WNG:
wString = L"(WW) " + wString;
break;
/*case Type::message:*/
}
/* screen it into VS debug console */
OutputDebugString(wString.c_str());
/* fire the event. */
messageToScreen(ref new String(wString.c_str(), wString.length()));
}
\ No newline at end of file
/***************************************************************************
* Copyright (C) 2016 by Savoir-faire Linux *
* Author: Jger Nicolas <nicolas.jager@savoirfairelinux.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
**************************************************************************/
#pragma once
namespace RingClientUWP
{
/* forward declaration */
ref class RingDebug;
/* delegate */
delegate void debugMessageToScreen(Platform::String^ message);
/* this is how to implement a singleton class*/
public ref class RingDebug sealed
{
public:
/* singleton */
static property RingDebug^ instance
{
RingDebug^ get()
{
static RingDebug^ instance_ = ref new RingDebug();
return instance_;
}
}
/* properties */
/* functions */
internal:
enum class Type { MSG, WNG, ERR };
void print(const std::string& message, const Type& type = Type::MSG);
/* event */
event debugMessageToScreen^ messageToScreen;
private:
RingDebug() {}; // singleton
};
#define MSG_(cstr) CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Low, \
ref new DispatchedHandler([=]() { RingDebug::instance->print(cstr); }))
#define WNG_(cstr) CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Low, \
ref new DispatchedHandler([=]() { RingDebug::instance->print(std::string(cstr), RingDebug::Type::WNG); }))
#define ERR_(cstr) CoreApplication::MainView->CoreWindow->Dispatcher->RunAsync(CoreDispatcherPriority::Low, \
ref new DispatchedHandler([=]() { RingDebug::instance->print(std::string(cstr), RingDebug::Type::ERR); }))
}
\ No newline at end of file
Utils.h 0 → 100644
#pragma once
#include <pch.h>
using namespace Platform;
using namespace Windows::Storage;
namespace RingClientUWP
{
namespace Utils {
task<bool>
fileExists(StorageFolder^ folder, String^ fileName)
{
return create_task(folder->GetFileAsync(fileName))
.then([](task<StorageFile^> taskResult)
{
bool exists;
try {
taskResult.get();
exists = true;
}
catch (COMException ^e) {
if (e->HResult == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) {
exists = false;
}
else {
throw;
}
}
return exists;
});
}
std::string makeString(const std::wstring& wstr)
{
auto wideData = wstr.c_str();
int bufferSize = WideCharToMultiByte(CP_UTF8, 0, wideData, -1, nullptr, 0, NULL, NULL);
std::unique_ptr<char[]> utf8;
utf8.reset(new char[bufferSize]);
if (WideCharToMultiByte(CP_UTF8, 0, wideData, -1, utf8.get(), bufferSize, NULL, NULL) == 0) {
return std::string();
}
return std::string(utf8.get());
}
std::wstring makeWString(const std::string& str)
{
auto utf8Data = str.c_str();
int bufferSize = MultiByteToWideChar(CP_UTF8, 0, utf8Data, -1, nullptr, 0);
std::unique_ptr<wchar_t[]> wide;
wide.reset(new wchar_t[bufferSize]);
if (MultiByteToWideChar(CP_UTF8, 0, utf8Data, -1, wide.get(), bufferSize) == 0) {
return std::wstring();
}
return std::wstring(wide.get());;
}
std::string toString(Platform::String ^str)
{
std::wstring wsstr(str->Data());
return makeString(wsstr);
}
Platform::String^ toPlatformString(const std::string& str)
{
std::wstring wsstr = makeWString(str);
return ref new Platform::String(wsstr.c_str(), wsstr.length());
}
}
}
\ No newline at end of file
......@@ -19,6 +19,7 @@
/* standard system include files. */
#include <ppltasks.h>
#include <iomanip>
/* required by generated headers. */
#include "App.xaml.h"
......@@ -26,3 +27,7 @@
#include "AccountsViewModel.h"
#include "Contact.h"
#include "ContactsViewModel.h"
/* ensure to be accessed from anywhere */
#include "RingDebug.h"
#include "Utils.h"
......@@ -175,9 +175,11 @@
<ClInclude Include="RingConsolePanel.xaml.h">
<DependentUpon>RingConsolePanel.xaml</DependentUpon>
</ClInclude>
<ClInclude Include="RingDebug.h" />
<ClInclude Include="SmartPanel.xaml.h">
<DependentUpon>SmartPanel.xaml</DependentUpon>
</ClInclude>
<ClInclude Include="Utils.h" />
<ClInclude Include="VideoPage.xaml.h">
<DependentUpon>VideoPage.xaml</DependentUpon>
</ClInclude>
......@@ -263,6 +265,7 @@
<ClCompile Include="RingConsolePanel.xaml.cpp">
<DependentUpon>RingConsolePanel.xaml</DependentUpon>
</ClCompile>
<ClCompile Include="RingDebug.cpp" />
<ClCompile Include="SmartPanel.xaml.cpp">
<DependentUpon>SmartPanel.xaml</DependentUpon>
</ClCompile>
......
......@@ -58,6 +58,9 @@
<ClCompile Include="ContactsViewModel.cpp">
<Filter>ModelViews</Filter>
</ClCompile>
<ClCompile Include="RingDebug.cpp">
<Filter>Common</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pch.h" />
......@@ -78,6 +81,12 @@
<ClInclude Include="ContactsViewModel.h">
<Filter>ModelViews</Filter>
</ClInclude>
<ClInclude Include="RingDebug.h">
<Filter>Common</Filter>
</ClInclude>
<ClInclude Include="Utils.h">
<Filter>Common</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="Assets\LockScreenLogo.scale-200.png">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment