asherah 3.0.0 → 3.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -1
- package/src/asherah.cc +1 -1
- package/src/logging.h +2 -0
- package/src/logging_napi.cc +151 -0
- package/src/logging_napi.h +43 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "asherah",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.2",
|
|
4
4
|
"description": "Asherah envelope encryption and key rotation library",
|
|
5
5
|
"exports": {
|
|
6
6
|
"node-addons": "./dist/asherah.node"
|
|
@@ -32,6 +32,8 @@
|
|
|
32
32
|
"src/hints.h",
|
|
33
33
|
"src/logging.cc",
|
|
34
34
|
"src/logging.h",
|
|
35
|
+
"src/logging_napi.cc",
|
|
36
|
+
"src/logging_napi.h",
|
|
35
37
|
"src/napi_utils.h",
|
|
36
38
|
"src/scoped_allocate.h",
|
|
37
39
|
"src/asherah.d.ts",
|
package/src/asherah.cc
CHANGED
package/src/logging.h
CHANGED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
#include "logging_napi.h"
|
|
2
|
+
#include "napi_utils.h"
|
|
3
|
+
|
|
4
|
+
LoggerNapi::LoggerNapi(Napi::Env &env, std::string system_name)
|
|
5
|
+
: Logger(system_name), log_hook(Napi::FunctionReference()), env(env) {}
|
|
6
|
+
|
|
7
|
+
LoggerNapi::LoggerNapi(Napi::Env &env, std::string system_name,
|
|
8
|
+
Napi::Function new_log_hook)
|
|
9
|
+
: Logger(system_name), env(env) {
|
|
10
|
+
if (unlikely(new_log_hook.IsEmpty())) {
|
|
11
|
+
NapiUtils::ThrowException(env, "new_log_hook cannot be nullptr");
|
|
12
|
+
}
|
|
13
|
+
log_hook = Napi::Persistent(new_log_hook);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
LoggerNapi::~LoggerNapi() {
|
|
17
|
+
auto old_log_hook = std::exchange(log_hook, Napi::FunctionReference());
|
|
18
|
+
if (!old_log_hook.IsEmpty()) {
|
|
19
|
+
old_log_hook.Unref();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
void LoggerNapi::set_log_hook(Napi::Function new_log_hook) {
|
|
24
|
+
if (unlikely(new_log_hook.IsUndefined() || new_log_hook.IsEmpty())) {
|
|
25
|
+
NapiUtils::ThrowException(env, "new_log_hook cannot be empty");
|
|
26
|
+
}
|
|
27
|
+
auto old_log_hook = std::exchange(log_hook, Napi::Persistent(new_log_hook));
|
|
28
|
+
if (!old_log_hook.IsEmpty()) {
|
|
29
|
+
old_log_hook.Unref();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
void LoggerNapi::debug_log(const char *function_name,
|
|
34
|
+
const char *message) const {
|
|
35
|
+
if (unlikely(verbose_flag)) {
|
|
36
|
+
if (unlikely(log_hook.IsEmpty())) {
|
|
37
|
+
stderr_debug_log(function_name, message);
|
|
38
|
+
} else {
|
|
39
|
+
Napi::Env env = log_hook.Env();
|
|
40
|
+
Napi::HandleScope scope(env);
|
|
41
|
+
Napi::Function log_hook_function = log_hook.Value();
|
|
42
|
+
log_hook_function.Call(
|
|
43
|
+
{Napi::Number::New(env, posix_log_level_debug),
|
|
44
|
+
Napi::String::New(env,
|
|
45
|
+
system_name + function_name + ": " + message)});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
void LoggerNapi::debug_log(const char *function_name,
|
|
51
|
+
const std::string &message) const {
|
|
52
|
+
if (unlikely(verbose_flag)) {
|
|
53
|
+
if (unlikely(log_hook.IsEmpty())) {
|
|
54
|
+
stderr_debug_log(function_name, message);
|
|
55
|
+
} else {
|
|
56
|
+
Napi::Env env = log_hook.Env();
|
|
57
|
+
Napi::HandleScope scope(env);
|
|
58
|
+
Napi::Function log_hook_function = log_hook.Value();
|
|
59
|
+
log_hook_function.Call(
|
|
60
|
+
{Napi::Number::New(env, posix_log_level_debug),
|
|
61
|
+
Napi::String::New(env,
|
|
62
|
+
system_name + function_name + ": " + message)});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
void LoggerNapi::debug_log_alloca(const char *function_name,
|
|
68
|
+
const char *variable_name,
|
|
69
|
+
size_t length) const {
|
|
70
|
+
if (unlikely(verbose_flag)) {
|
|
71
|
+
if (unlikely(log_hook.IsEmpty())) {
|
|
72
|
+
stderr_debug_log_alloca(function_name, variable_name, length);
|
|
73
|
+
} else {
|
|
74
|
+
Napi::Env env = log_hook.Env();
|
|
75
|
+
Napi::HandleScope scope(env);
|
|
76
|
+
Napi::Function log_hook_function = log_hook.Value();
|
|
77
|
+
log_hook_function.Call(
|
|
78
|
+
{Napi::Number::New(env, posix_log_level_debug),
|
|
79
|
+
Napi::String::New(env, system_name + function_name +
|
|
80
|
+
": Calling alloca(" +
|
|
81
|
+
std::to_string(length) +
|
|
82
|
+
") (stack) for " + variable_name)});
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
void LoggerNapi::debug_log_new(const char *function_name,
|
|
88
|
+
const char *variable_name, size_t length) const {
|
|
89
|
+
if (unlikely(verbose_flag)) {
|
|
90
|
+
if (unlikely(log_hook.IsEmpty())) {
|
|
91
|
+
stderr_debug_log_new(function_name, variable_name, length);
|
|
92
|
+
} else {
|
|
93
|
+
Napi::Env env = log_hook.Env();
|
|
94
|
+
Napi::HandleScope scope(env);
|
|
95
|
+
Napi::Function log_hook_function = log_hook.Value();
|
|
96
|
+
log_hook_function.Call(
|
|
97
|
+
{Napi::Number::New(env, posix_log_level_debug),
|
|
98
|
+
Napi::String::New(env, system_name + function_name +
|
|
99
|
+
": Calling new[" +
|
|
100
|
+
std::to_string(length) + "] (heap) for " +
|
|
101
|
+
variable_name)});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
void LoggerNapi::error_log(const char *function_name,
|
|
107
|
+
const char *message) const {
|
|
108
|
+
if (unlikely(verbose_flag)) {
|
|
109
|
+
if (unlikely(log_hook.IsEmpty())) {
|
|
110
|
+
stderr_error_log(function_name, message);
|
|
111
|
+
} else {
|
|
112
|
+
stderr_error_log(function_name, message);
|
|
113
|
+
Napi::Env env = log_hook.Env();
|
|
114
|
+
Napi::HandleScope scope(env);
|
|
115
|
+
Napi::Function log_hook_function = log_hook.Value();
|
|
116
|
+
log_hook_function.Call(
|
|
117
|
+
{Napi::Number::New(env, posix_log_level_error),
|
|
118
|
+
Napi::String::New(env,
|
|
119
|
+
system_name + function_name + ": " + message)});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
void LoggerNapi::error_log(const char *function_name,
|
|
125
|
+
const std::string &message) const {
|
|
126
|
+
// Unconditionally log errors to stderr
|
|
127
|
+
stderr_error_log(function_name, message);
|
|
128
|
+
|
|
129
|
+
if (likely(!log_hook.IsEmpty())) {
|
|
130
|
+
Napi::Env env = log_hook.Env();
|
|
131
|
+
Napi::HandleScope scope(env);
|
|
132
|
+
Napi::Function log_hook_function = log_hook.Value();
|
|
133
|
+
log_hook_function.Call(
|
|
134
|
+
{Napi::Number::New(env, posix_log_level_error),
|
|
135
|
+
Napi::String::New(env, system_name + function_name + ": " + message)});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
__attribute__((always_inline, noreturn)) inline void
|
|
140
|
+
LoggerNapi::log_error_and_throw(const char *function_name,
|
|
141
|
+
const std::string &error_msg) const {
|
|
142
|
+
std::string final_error_msg =
|
|
143
|
+
system_name + ": [EXCEPTION] " + function_name + (": " + error_msg);
|
|
144
|
+
|
|
145
|
+
// Unconditionally log errors to stderr
|
|
146
|
+
stderr_error_log(function_name, final_error_msg);
|
|
147
|
+
|
|
148
|
+
error_log(function_name, final_error_msg);
|
|
149
|
+
|
|
150
|
+
NapiUtils::ThrowException(env, final_error_msg);
|
|
151
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
#ifndef LOGGING_NAPI_H
|
|
2
|
+
#define LOGGING_NAPI_H
|
|
3
|
+
#include "hints.h"
|
|
4
|
+
#include "logging.h"
|
|
5
|
+
#include <napi.h>
|
|
6
|
+
|
|
7
|
+
#ifndef NAPI_CPP_EXCEPTIONS
|
|
8
|
+
#error Support for C++ exceptions is required
|
|
9
|
+
#endif
|
|
10
|
+
|
|
11
|
+
class LoggerNapi : public Logger {
|
|
12
|
+
public:
|
|
13
|
+
LoggerNapi(Napi::Env &env, std::string system_name);
|
|
14
|
+
explicit LoggerNapi(Napi::Env &env, std::string system_name,
|
|
15
|
+
Napi::Function new_log_hook);
|
|
16
|
+
~LoggerNapi();
|
|
17
|
+
|
|
18
|
+
void set_log_hook(Napi::Function new_log_hook);
|
|
19
|
+
|
|
20
|
+
void debug_log(const char *function_name, const char *message) const override;
|
|
21
|
+
void debug_log(const char *function_name,
|
|
22
|
+
const std::string &message) const override;
|
|
23
|
+
void debug_log_alloca(const char *function_name, const char *variable_name,
|
|
24
|
+
size_t length) const override;
|
|
25
|
+
|
|
26
|
+
void debug_log_new(const char *function_name, const char *variable_name,
|
|
27
|
+
size_t length) const override;
|
|
28
|
+
|
|
29
|
+
void error_log(const char *function_name, const char *message) const override;
|
|
30
|
+
void error_log(const char *function_name,
|
|
31
|
+
const std::string &message) const override;
|
|
32
|
+
[[noreturn]] void
|
|
33
|
+
log_error_and_throw(const char *function_name,
|
|
34
|
+
const std::string &error_msg) const override;
|
|
35
|
+
|
|
36
|
+
private:
|
|
37
|
+
Napi::FunctionReference log_hook;
|
|
38
|
+
Napi::Env env;
|
|
39
|
+
const int posix_log_level_error = 3;
|
|
40
|
+
const int posix_log_level_debug = 7;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
#endif // LOGGING_NAPI_H
|