Skip to content
This repository has been archived by the owner on Jan 3, 2020. It is now read-only.

Adding logger to write console output to file + Adding instructions for running tests #2

Merged
merged 4 commits into from
May 29, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,26 @@ Steps:
* Set node_dir to the path of your Node.js clone
* Set release_dir if desired (optional if copyrelease is not used)
* Run "build.bat [x86|x64|arm] [copyrelease]"

##To test:
Follow the steps below to run [tests](https://github.com/joyent/node/tree/master/test) included with Node.js.

* Clone Node.js from https://github.com/microsoft/node
* Install the NTVS IoT Extension using the steps [here](http:https://ms-iot.github.io/content/en-US/win10/samples/NodejsWU.htm) and create a new Node.js (Windows Universal) project
* Copy <Node.js clone path>\tests to <Node.js UWP project path (location of .njsproj file)>\tests
* Right click on the test you want to run and select "Set as Node.js Startup File". The file text will be made bold (see test-assert.js example below)
![Set test as Startup File]({{site.baseurl}}/images/test-startup-file.png)
* Press F5 (or click on Debug->Start Debugging menu) to run the test
* Console output can be redirected to file. You can view the logs on the device in c:\Users\DefaultAccount\AppData\Local\Packages\<Your app ID (get it from VS build logs)>\LocalState\nodeuwp.log

##Node.js compatibility with UWP
The following API's are not supported in Node.js UWP:

* Child Processes
* Cluster
* Debugger
* TTY

There are a few other limitations in other modules. For example, the [fs](https://nodejs.org/api/fs.html) module can only access files within its the UWP package or within paths declared in the [package capabilities](https://msdn.microsoft.com/en-us/library/windows/apps/hh464936.aspx).

Detailed documentation on which API's are supported in Node.js UWP can be found [here]({{site.baseurl}}/compatibility.xlsx).
6 changes: 3 additions & 3 deletions build.bat
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ set buildall=1

:arm
@rem build node.dll
call "%node_dir%\vcbuild.bat" arm chakra uwp-dll withoutssl
call "%node_dir%\vcbuild.bat" arm chakra uwp-dll openssl-no-asm
pushd %batch_dir%
@rem build nodeuwp.dll
set WindowsSdkDir=%programfiles(x86)%\Windows Kits\10\
Expand All @@ -56,7 +56,7 @@ if not defined buildall goto end

:x86
@rem build node.dll
call "%node_dir%\vcbuild.bat" x86 chakra uwp-dll withoutssl
call "%node_dir%\vcbuild.bat" x86 chakra uwp-dll
pushd %batch_dir%
@rem build nodeuwp.dll
set WindowsSdkDir=%programfiles(x86)%\Windows Kits\10\
Expand All @@ -71,7 +71,7 @@ if not defined buildall goto end

:x64
@rem build node.dll
call "%node_dir%\vcbuild.bat" x64 chakra uwp-dll withoutssl
call "%node_dir%\vcbuild.bat" x64 chakra uwp-dll
pushd %batch_dir%
@rem build nodeuwp.dll
set WindowsSdkDir=%programfiles(x86)%\Windows Kits\10\
Expand Down
Binary file added compatibility.xlsx
Binary file not shown.
Binary file added images/test-startup-file.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions nodeuwp/Logger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
Copyright(c) Microsoft Open Technologies, Inc. All rights reserved.

The MIT License(MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "pch.h"
#include "Logger.h"
#include <windows.storage.h>
#include <ppltasks.h>

using namespace Windows::Storage;
using namespace Windows::Foundation;
using namespace Platform;
using namespace concurrency;

namespace nodeuwp
{
std::unique_ptr<Logger> Logger::s_pInstance;

const Logger& Logger::GetInstance()
{
if (s_pInstance == nullptr)
{
if (s_pInstance == nullptr)
{
s_pInstance.reset(new (std::nothrow) Logger());
}
}
return *s_pInstance.get();
}

Logger::Logger()
{
// Create a new file in the local folder if it doesn't exist
StorageFolder^ localFolder = ApplicationData::Current->LocalFolder;
String^ logFile = "nodeuwp.log";
auto createFileTask = create_task(localFolder->CreateFileAsync(logFile, Windows::Storage::CreationCollisionOption::OpenIfExists));
createFileTask.then([this](StorageFile^ file) {
m_file = file;
});
}

void Logger::Log(ILogger::LogLevel logLevel, const char* str) const
{
// Convert char* to Platform::String
size_t newsize = strlen(str) + 1;
wchar_t * wcstring = new wchar_t[newsize];
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wcstring, newsize, str, _TRUNCATE);
String^ pstr = ref new String(wcstring);

// Append console logs to file
IAsyncAction^ Action = FileIO::AppendTextAsync(m_file, pstr + "\r\n");
create_task(Action).then([this](){}).wait(); // wait is required to log messages sequentially.
}
}
48 changes: 48 additions & 0 deletions nodeuwp/Logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright(c) Microsoft Open Technologies, Inc. All rights reserved.

The MIT License(MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#pragma once

#include <windows.h>
#include "ILogger.h"
#include <windows.storage.h>

using namespace Windows::Storage;

namespace nodeuwp
{
// Logger class routes log messages to file
class Logger : public node::logger::ILogger
{
public:
static const Logger &GetInstance();
~Logger() {}
virtual void Log(ILogger::LogLevel logLevel, const char* str) const override;

private:
Logger();
StorageFile^ m_file;
static std::unique_ptr<Logger> s_pInstance;
};
}
11 changes: 10 additions & 1 deletion nodeuwp/StartupTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <ppltasks.h>
#include "v8.h"
#include "node.h"
#include "Logger.h"

using namespace nodeuwp;

Expand Down Expand Up @@ -115,6 +116,7 @@ void StartupTask::Run(IBackgroundTaskInstance^ taskInstance)
getStartupInfoXml.then([=](XmlDocument^ startupInfoXml)
{
std::vector<std::shared_ptr<char>> argumentVector;
bool useLogger = true; //TODO: set value from a property in Visual Studio

std::shared_ptr<char> argChar = PlatformStringToChar(L" ", 1);
argumentVector.push_back(argChar);
Expand All @@ -138,7 +140,14 @@ void StartupTask::Run(IBackgroundTaskInstance^ taskInstance)
argv.get()[i] = (argumentVector[i]).get();
}

node::Start(argc, argv.get());
if (!useLogger)
{
node::Start(argc, argv.get());
}
else
{
node::Start(argc, argv.get(), &Logger::GetInstance());
}
deferral->Complete();
});
});
Expand Down
21 changes: 12 additions & 9 deletions nodeuwp/nodeuwp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PreprocessorDefinitions>_WINRT_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WINRT_DLL;UWP_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
<AdditionalUsingDirectories>$(WindowsSDK_WindowsMetadata);$(AdditionalUsingDirectories)</AdditionalUsingDirectories>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>28204</DisableSpecificWarnings>
<AdditionalIncludeDirectories>$(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;$(NODE_DIR)\deps\logger\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -135,13 +135,13 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PreprocessorDefinitions>_WINRT_DLL;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WINRT_DLL;NDEBUG;UWP_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
<AdditionalUsingDirectories>$(WindowsSDK_WindowsMetadata);$(AdditionalUsingDirectories)</AdditionalUsingDirectories>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>28204</DisableSpecificWarnings>
<AdditionalIncludeDirectories>$(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;$(NODE_DIR)\deps\logger\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -153,7 +153,8 @@
</IgnoreSpecificDefaultLibraries>
</Link>
<PreBuildEvent>
<Command>setx NODE_DIR E:\Repos\my_ntvsiot</Command>
<Command>
</Command>
</PreBuildEvent>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM'">
Expand All @@ -175,13 +176,13 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PreprocessorDefinitions>_WINRT_DLL;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WINRT_DLL;NDEBUG;UWP_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
<AdditionalUsingDirectories>$(WindowsSDK_WindowsMetadata);$(AdditionalUsingDirectories)</AdditionalUsingDirectories>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>28204</DisableSpecificWarnings>
<AdditionalIncludeDirectories>$(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;$(NODE_DIR)\deps\logger\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand Down Expand Up @@ -209,13 +210,13 @@
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<PreprocessorDefinitions>_WINRT_DLL;NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_WINRT_DLL;NDEBUG;UWP_DLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<PrecompiledHeaderOutputFile>$(IntDir)pch.pch</PrecompiledHeaderOutputFile>
<AdditionalUsingDirectories>$(WindowsSDK_WindowsMetadata);$(AdditionalUsingDirectories)</AdditionalUsingDirectories>
<AdditionalOptions>/bigobj %(AdditionalOptions)</AdditionalOptions>
<DisableSpecificWarnings>28204</DisableSpecificWarnings>
<AdditionalIncludeDirectories>$(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(NODE_DIR)\src;$(NODE_DIR)\deps\chakrashim\include;$(NODE_DIR)\deps\logger\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
Expand All @@ -237,10 +238,12 @@
<Image Include="Assets\WideLogo.scale-100.png" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="Logger.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="StartupTask.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Logger.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
Expand Down