sync_logger.cpp
3.3 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
108
109
110
111
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2016 Realm Inc.
//
// 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
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////
#include "sync_logger.hpp"
#include "util/event_loop_signal.hpp"
#include <realm/util/logger.hpp>
#include <mutex>
#include <stdexcept>
#include <string>
#include <utility>
#include <set>
#include <queue>
using namespace realm;
using namespace realm::node;
namespace {
struct SyncLoggerMessage {
std::string m_message;
realm::util::Logger::Level m_level;
};
class SyncLoggerQueue {
public:
SyncLoggerQueue(v8::Isolate* v8_isolate, v8::Local<v8::Function> callback)
: m_log_uv_async([this] { log_uv_callback(); }) // Throws
, m_v8_isolate(v8_isolate)
, m_callback(v8_isolate, callback)
{
}
protected:
void log_uv_callback();
std::queue<SyncLoggerMessage> m_log_queue;
std::mutex m_mutex;
EventLoopSignal<std::function<void()>> m_log_uv_async;
private:
v8::Isolate* m_v8_isolate;
v8::Persistent<v8::Function> m_callback;
};
class SyncLogger : public realm::util::RootLogger, public SyncLoggerQueue {
public:
SyncLogger(v8::Isolate* v8_isolate, v8::Local<v8::Function> callback)
: SyncLoggerQueue(v8_isolate, callback)
{
}
protected:
void do_log(realm::util::Logger::Level, std::string) override final;
};
void SyncLoggerQueue::log_uv_callback()
{
// This function is always executed by the Node.js event loop
// thread.
v8::HandleScope scope(m_v8_isolate);
v8::Local<v8::Function> callback = v8::Local<v8::Function>::New(m_v8_isolate, m_callback);
std::queue<SyncLoggerMessage> popped;
{
std::lock_guard<std::mutex> lock(m_mutex); // Throws
popped.swap(m_log_queue);
}
while (!popped.empty()) {
v8::Local<v8::Value> argv[] = {v8::Integer::New(m_v8_isolate, static_cast<int>(popped.front().m_level)),
v8::String::NewFromUtf8(m_v8_isolate, popped.front().m_message.c_str())};
callback->Call(m_v8_isolate->GetCurrentContext(), v8::Null(m_v8_isolate), 2, argv);
popped.pop();
}
}
void SyncLogger::do_log(realm::util::Logger::Level level, std::string message)
{
std::lock_guard<std::mutex> lock(m_mutex); // Throws
m_log_queue.push({std::move(message), level});
m_log_uv_async.notify();
}
} // anonymous namespace
std::unique_ptr<util::Logger> realm::node::SyncLoggerFactory::make_logger(util::Logger::Level level)
{
v8::Local<v8::Function> callback = v8::Local<v8::Function>::New(m_v8_isolate, m_callback);
auto logger = std::make_unique<SyncLogger>(m_v8_isolate, callback); // Throws
logger->set_level_threshold(level);
return std::unique_ptr<util::Logger>(logger.release());
}