-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnativeconsole.h
More file actions
109 lines (90 loc) · 3.34 KB
/
nativeconsole.h
File metadata and controls
109 lines (90 loc) · 3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the ""License""); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
//
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
#pragma once
#include <v8.h>
#include <uv.h>
#include <functional>
#include "node-async.h"
namespace NodeUtils
{
using namespace v8;
class Console
{
public:
Console()
{
ConnectToJSConsole();
}
~Console()
{
Dispose();
}
// may be called from main thread or worker threads / RT event threads
void Log(const wchar_t *wstr) const { Async::RunOnMain([&, wstr] { Log(_logSymbol, wstr); }); }
void Info(const wchar_t *wstr) const { Async::RunOnMain([&, wstr] { Log(_infoSymbol, wstr); }); }
void Warn(const wchar_t *wstr) const { Async::RunOnMain([&, wstr] { Log(_warnSymbol, wstr); }); }
void Error(const wchar_t *wstr) const { Async::RunOnMain([&, wstr] { Log(_errorSymbol, wstr); }); }
void ConnectToJSConsole()
{
Dispose();
HandleScope scope;
_console = Persistent<Object>::New(Object::New());
_console->SetPrototype(v8::Context::GetCurrent()->Global()->Get(String::NewSymbol("console")).As<Object>());
// attach domain:
// get the current domain:
Handle<Value> currentDomain;
Handle<Object> process = v8::Context::GetCurrent()->Global()->Get(String::NewSymbol("process")).As<Object>();
if (!process->Equals(Undefined()))
{
currentDomain = process->Get(String::NewSymbol("domain")) ;
}
if (!currentDomain.IsEmpty() && !currentDomain->Equals(Undefined()))
{
_console->Set(String::NewSymbol("domain"), currentDomain);
}
if (!_console.IsEmpty())
{
_logSymbol = Persistent<String>::New(String::NewSymbol("log"));
_infoSymbol = Persistent<String>::New(String::NewSymbol("info"));
_warnSymbol = Persistent<String>::New(String::NewSymbol("warn"));
_errorSymbol = Persistent<String>::New(String::NewSymbol("error"));
}
}
private:
void Dispose()
{
_console.Dispose();
_logSymbol.Dispose();
_infoSymbol.Dispose();
_warnSymbol.Dispose();
_errorSymbol.Dispose();
}
void Log(const Handle<String>& logFunctionSymbol, const wchar_t *wstr) const
{
HandleScope scope;
Local<String> str = String::New(wstr);
Log(logFunctionSymbol, str);
}
void Log(const Handle<String>& logFunctionSymbol, Handle<String>& str) const
{
if (!logFunctionSymbol.IsEmpty())
{
HandleScope scope;
Local<Value> argv[] = { Local<Value>::New(str) };
node::MakeCallback(_console, logFunctionSymbol, _countof(argv), argv);
}
}
private:
Persistent<Object> _console;
Persistent<String> _logSymbol;
Persistent<String> _infoSymbol;
Persistent<String> _warnSymbol;
Persistent<String> _errorSymbol;
};
}