-
Notifications
You must be signed in to change notification settings - Fork 79
/
ffi.cc
206 lines (149 loc) · 5.23 KB
/
ffi.cc
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <napi.h>
#include "pact-cpp.h"
using namespace Napi;
Napi::Value PactffiVersion(const Napi::CallbackInfo& info) {
const char* version = pactffi_version();
return Napi::String::New(info.Env(), version);
}
Napi::Value PactffiInit(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 1) {
throw Napi::Error::New(env, "PactffiInit(envVar) received < 1 argument");
}
if (!info[0].IsString()) {
throw Napi::Error::New(env, "PactffiInit(envVar) expected a string");
}
// Extract log level environment variable
std::string envVar = info[0].As<Napi::String>().Utf8Value();
// Initialise Pact
pactffi_init_with_log_level(envVar.c_str());
return env.Undefined();
}
Napi::Value PactffiInitWithLogLevel(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() < 1) {
throw Napi::Error::New(env, "PactffiInitWithLogLevel(logLevel) received < 1 argument");
}
if (!info[0].IsString()) {
throw Napi::Error::New(env, "PactffiInitWithLogLevel(logLevel) expected a string");
}
// Extract log level
std::string logLevel = info[0].As<Napi::String>().Utf8Value();
// Initialise Pact
pactffi_init_with_log_level(logLevel.c_str());
return env.Undefined();
}
LevelFilter integerToLevelFilter(Napi::Env &env, uint32_t number) {
LevelFilter logLevel = LevelFilter::LevelFilter_Off;
switch(number) {
case 0:
logLevel = LevelFilter::LevelFilter_Off;
break;
case 1:
logLevel = LevelFilter::LevelFilter_Error;
break;
case 2:
logLevel = LevelFilter::LevelFilter_Warn;
break;
case 3:
logLevel = LevelFilter::LevelFilter_Info;
break;
case 4:
logLevel = LevelFilter::LevelFilter_Debug;
break;
case 5:
logLevel = LevelFilter::LevelFilter_Trace;
break;
default:
std::string err = "pact-js-core C integration: Unable to parse log level number: ";
err += number;
throw Napi::Error::New(env, err);
}
return logLevel;
}
Napi::Value PactffiLogToFile(const Napi::CallbackInfo& info) {
// return: int
Napi::Env env = info.Env();
if (info.Length() < 2) {
throw Napi::Error::New(env, "PactffiLogToFile(envVar) received < 2 arguments");
}
if (!info[0].IsString()) {
throw Napi::Error::New(env, "PactffiLogToFile(envVar) expected a string");
}
if (!info[1].IsNumber()) {
throw Napi::Error::New(env, "PactffiLogToFile(envVar) expected a number");
}
// Extract log level
std::string fileName = info[0].As<Napi::String>().Utf8Value();
uint32_t levelFilterNumber = info[1].As<Napi::Number>().Uint32Value();
LevelFilter levelFilter = integerToLevelFilter(env, levelFilterNumber);
int res = pactffi_log_to_file(fileName.c_str(), levelFilter);
return Napi::Number::New(env, res);
}
/*
Napi::Value Pactffi_log_message(const Napi::CallbackInfo& info) {
// return: void
Napi::Env env = info.Env();
if (info.Length() < 3) {
throw Napi::Error::New(env, "PactffiInit(envVar) received < 1 argument");
}
if (!info[0].IsString()) {
throw Napi::Error::New(env, "PactffiInit(envVar) expected a string");
}
// pactffi_log_message(const char *source, const char *log_level, const char *message);
pactffi_log_message(const char *source, const char *log_level, const char *message);
return info.Env().Undefined();
}
Napi::Value Pactffi_log_to_stdout(const Napi::CallbackInfo& info) {
// return: int
Napi::Env env = info.Env();
if (info.Length() < 1) {
throw Napi::Error::New(env, "PactffiInit(envVar) received < 1 argument");
}
if (!info[0].IsString()) {
throw Napi::Error::New(env, "PactffiInit(envVar) expected a string");
}
// pactffi_log_to_stdout(LevelFilter level_filter);
pactffi_log_to_stdout(LevelFilter level_filter);
return info.Env().Undefined();
}
Napi::Value Pactffi_log_to_stderr(const Napi::CallbackInfo& info) {
// return: int
Napi::Env env = info.Env();
if (info.Length() < 1) {
throw Napi::Error::New(env, "PactffiInit(envVar) received < 1 argument");
}
if (!info[0].IsString()) {
throw Napi::Error::New(env, "PactffiInit(envVar) expected a string");
}
// pactffi_log_to_stderr(LevelFilter level_filter);
pactffi_log_to_stderr(LevelFilter level_filter);
return info.Env().Undefined();
}
Napi::Value Pactffi_log_to_file(const Napi::CallbackInfo& info) {
// return: int
Napi::Env env = info.Env();
if (info.Length() < 1) {
throw Napi::Error::New(env, "PactffiInit(envVar) received < 1 argument");
}
if (!info[0].IsString()) {
throw Napi::Error::New(env, "PactffiInit(envVar) expected a string");
}
// pactffi_log_to_file(const char *file_name, LevelFilter level_filter);
pactffi_log_to_file(const char *file_name, LevelFilter level_filter);
return info.Env().Undefined();
}
Napi::Value Pactffi_log_to_buffer(const Napi::CallbackInfo& info) {
// return: int
Napi::Env env = info.Env();
if (info.Length() < 1) {
throw Napi::Error::New(env, "PactffiInit(envVar) received < 1 argument");
}
if (!info[0].IsString()) {
throw Napi::Error::New(env, "PactffiInit(envVar) expected a string");
}
// pactffi_log_to_buffer(LevelFilter level_filter);
pactffi_log_to_buffer(LevelFilter level_filter);
return info.Env().Undefined();
}
*/