Skip to content

fix: make logging thread safe #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 24, 2025
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
35 changes: 35 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/include/**",
"${workspaceFolder}/libsession-util/include/**",
"${workspaceFolder}/node_modules/node-addon-api/",
"${workspaceFolder}/node_modules/node-api-headers/include/"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++20",
"intelliSenseMode": "linux-clang-x64"
},
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/include/**",
"${workspaceFolder}/libsession-util/include/**",
"${workspaceFolder}/node_modules/node-addon-api/",
"${workspaceFolder}/node_modules/node-api-headers/include/"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c17",
"cppStandard": "c++20",
"intelliSenseMode": "macos-clang-arm64"
}
],
"version": 4
}
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ if(MSVC)
endif()

add_library(${PROJECT_NAME} SHARED ${SOURCE_FILES} ${CMAKE_JS_SRC})
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC} "node_modules/node-addon-api" "../../node_modules/node-addon-api" "node_modules/node-api-headers/include" "../../node_modules/node-api-headers/include")
target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_JS_INC} "include/" "node_modules/node-addon-api" "../../node_modules/node-addon-api" "node_modules/node-api-headers/include" "../../node_modules/node-api-headers/include")

set_target_properties(${PROJECT_NAME} PROPERTIES PREFIX "" SUFFIX ".node")
target_link_libraries(${PROJECT_NAME} PRIVATE ${CMAKE_JS_LIB} ${LIBSESSION_STATIC_BUNDLE_LIBS})
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

#include <napi.h>

#include <optional>
#include <span>
#include <vector>

#include "../base_config.hpp"
#include "../groups/meta_group.hpp"

namespace session::nodeapi {

class MetaBaseWrapper {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"main": "index.js",
"name": "libsession_util_nodejs",
"description": "Wrappers for the Session Util Library",
"version": "0.5.0",
"version": "0.5.1",
"license": "GPL-3.0",
"author": {
"name": "Oxen Project",
Expand All @@ -11,16 +11,18 @@
"scripts": {
"update_version": "sh update_version.sh",
"clean": "rimraf .cache build",
"lint:cpp": "cppcheck --std=c++20 -j8 --quiet src libsession-util/src",
"install": "cmake-js build --runtime=electron --runtime-version=34.2.0 --CDSUBMODULE_CHECK=OFF --CDLOCAL_MIRROR=https://oxen.rocks/deps --CDENABLE_ONIONREQ=OFF --CDWITH_TESTS=OFF",
"prepare_release": "sh prepare_release.sh"
"prepare_release": "sh prepare_release.sh",
"dedup": "npx --yes yarn-deduplicate yarn.lock"
},
"devDependencies": {
"clang-format": "^1.8.0",
"rimraf": "2.6.2"
},
"dependencies": {
"cmake-js": "7.2.1",
"node-addon-api": "^6.1.0"
"cmake-js": "7.3.1",
"node-addon-api": "^8.3.1"
},
"typings": "index.d.ts",
"packageManager": "[email protected]"
Expand Down
34 changes: 26 additions & 8 deletions src/addon.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <napi.h>

#include <oxen/log.hpp>

#include "blinding/blinding.hpp"
#include "constants.hpp"
#include "contacts_config.hpp"
Expand All @@ -9,18 +11,34 @@
#include "user_config.hpp"
#include "user_groups_config.hpp"

Napi::ThreadSafeFunction tsfn;

Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
using namespace session::nodeapi;

session::add_logger([env](std::string_view msg) {
std::string toLog = std::string("libsession-util: ") + std::string(msg) + "\n";

Napi::Function consoleLog =
env.Global().Get("console").As<Napi::Object>().Get("log").As<Napi::Function>();
consoleLog.Call({Napi::String::New(env, toLog)});
tsfn = Napi::ThreadSafeFunction::New(
env,
Napi::Function::New(env, [](const Napi::CallbackInfo& info) {}),
"LoggerCallback",
0,
1);

session::add_logger([](std::string_view msg) {
tsfn.BlockingCall(
new std::string(msg),
[](Napi::Env env, Napi::Function jsCallback, std::string* msg) {
Napi::HandleScope scope(env);
Napi::Function consoleLog = env.Global()
.Get("console")
.As<Napi::Object>()
.Get("log")
.As<Napi::Function>();
Napi::String jsStr = Napi::String::New(env, "libsession-util: " + *msg);
consoleLog.Call({jsStr});
delete msg;
});
});

// session::logger_set_level_default(session::LogLevel::debug);
oxen::log::set_level_default(oxen::log::Level::info);

ConstantsWrapper::Init(env, exports);

Expand Down
1 change: 1 addition & 0 deletions src/convo_info_volatile_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "community.hpp"
#include "session/config/convo_info_volatile.hpp"
#include "session/types.hpp"
#include "utilities.hpp"

namespace session::nodeapi {

Expand Down
9 changes: 4 additions & 5 deletions src/groups/meta_group_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include "meta_group_wrapper.hpp"
#include "groups/meta_group_wrapper.hpp"

#include <napi.h>
#include <oxenc/bt_producer.h>

#include <memory>
#include <session/types.hpp>
#include <session/util.hpp>
#include <span>
#include <vector>

#include "oxenc/bt_producer.h"
#include "session/types.hpp"

namespace session::nodeapi {

Napi::Object member_to_js(const Napi::Env& env, const member& info, const member::Status& status) {
Expand Down Expand Up @@ -275,7 +275,6 @@ Napi::Value MetaGroupWrapper::metaMerge(const Napi::CallbackInfo& info) {

auto count_merged = 0;


// Note: we need to process keys first as they might allow us the incoming info+members
// details
if (!groupKeys.IsNull() && !groupKeys.IsUndefined()) {
Expand Down
70 changes: 32 additions & 38 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ asynckit@^0.4.0:
resolved "https://registry.yarnpkg.com/asynckit/-/asynckit-0.4.0.tgz#c79ed97f7f34cb8f2ba1bc9790bcc366474b4b79"
integrity sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==

axios@^1.3.2:
version "1.7.7"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.7.7.tgz#2f554296f9892a72ac8d8e4c5b79c14a91d0a47f"
integrity sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==
axios@^1.6.5:
version "1.8.4"
resolved "https://registry.yarnpkg.com/axios/-/axios-1.8.4.tgz#78990bb4bc63d2cae072952d374835950a82f447"
integrity sha512-eBSYY4Y68NNlHbHBMdeDmKNtDgXWhQsJcGqzO3iLUM0GraQFSS9cVgPX5I9b3lbdFKyYoAEGAZF1DwhTaljNAw==
dependencies:
follow-redirects "^1.15.6"
form-data "^4.0.0"
Expand Down Expand Up @@ -82,24 +82,23 @@ cliui@^8.0.1:
strip-ansi "^6.0.1"
wrap-ansi "^7.0.0"

cmake-js@7.2.1:
version "7.2.1"
resolved "https://registry.yarnpkg.com/cmake-js/-/cmake-js-7.2.1.tgz#757c0d39994121b084bab96290baf115ee7712cd"
integrity sha512-AdPSz9cSIJWdKvm0aJgVu3X8i0U3mNTswJkSHzZISqmYVjZk7Td4oDFg0mCBA383wO+9pG5Ix7pEP1CZH9x2BA==
cmake-js@7.3.1:
version "7.3.1"
resolved "https://registry.yarnpkg.com/cmake-js/-/cmake-js-7.3.1.tgz#ed661eebd22a56d4743d7d2106a56fe50aa4355c"
integrity sha512-aJtHDrTFl8qovjSSqXT9aC2jdGfmP8JQsPtjdLAXFfH1BF4/ImZ27Jx0R61TFg8Apc3pl6e2yBKMveAeRXx2Rw==
dependencies:
axios "^1.3.2"
axios "^1.6.5"
debug "^4"
fs-extra "^10.1.0"
lodash.isplainobject "^4.0.6"
fs-extra "^11.2.0"
memory-stream "^1.0.0"
node-api-headers "^0.0.2"
node-api-headers "^1.1.0"
npmlog "^6.0.2"
rc "^1.2.7"
semver "^7.3.8"
tar "^6.1.11"
semver "^7.5.4"
tar "^6.2.0"
url-join "^4.0.1"
which "^2.0.2"
yargs "^17.6.0"
yargs "^17.7.2"

color-convert@^2.0.1:
version "2.0.1"
Expand Down Expand Up @@ -181,10 +180,10 @@ form-data@^4.0.0:
combined-stream "^1.0.8"
mime-types "^2.1.12"

fs-extra@^10.1.0:
version "10.1.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-10.1.0.tgz#02873cfbc4084dde127eaa5f9905eef2325d1abf"
integrity sha512-oRXApq54ETRj4eMiFzGnHWGy+zo5raudjuxN0b8H7s/RU2oW0Wvsx9O0ACRN/kRq9E8Vu/ReskGB5o3ji+FzHQ==
fs-extra@^11.2.0:
version "11.3.0"
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-11.3.0.tgz#0daced136bbaf65a555a326719af931adc7a314d"
integrity sha512-Z4XaCL6dUDHfP/jT25jJKMmtxvuwbkrD1vNSMFlo9lNLY2c5FHYSQgHPRZUjAB26TpDEoW9HCOgplrdbaPV/ew==
dependencies:
graceful-fs "^4.2.0"
jsonfile "^6.0.1"
Expand Down Expand Up @@ -299,11 +298,6 @@ jsonfile@^6.0.1:
optionalDependencies:
graceful-fs "^4.1.6"

lodash.isplainobject@^4.0.6:
version "4.0.6"
resolved "https://registry.yarnpkg.com/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz#7c526a52d89b45c45cc690b88163be0497f550cb"
integrity sha512-oSXzaWypCMHkPC3NvBEaPHf0KsA5mvPrOPgQWDsbg8n7orZ290M0BmC/jgRZ4vcJ6DTAhjrsSYgdsW/F+MFOBA==

memory-stream@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/memory-stream/-/memory-stream-1.0.0.tgz#481dfd259ccdf57b03ec2c9632960044180e73c2"
Expand Down Expand Up @@ -365,15 +359,15 @@ ms@^2.1.3:
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.3.tgz#574c8138ce1d2b5861f0b44579dbadd60c6615b2"
integrity sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==

node-addon-api@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-6.1.0.tgz#ac8470034e58e67d0c6f1204a18ae6995d9c0d76"
integrity sha512-+eawOlIgy680F0kBzPUNFhMZGtJ1YmqM6l4+Crf4IkImjYrO/mqPwRMh352g23uIaQKFItcQ64I7KMaJxHgAVA==
node-addon-api@^8.3.1:
version "8.3.1"
resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-8.3.1.tgz#53bc8a4f8dbde3de787b9828059da94ba9fd4eed"
integrity sha512-lytcDEdxKjGJPTLEfW4mYMigRezMlyJY8W4wxJK8zE533Jlb8L8dRuObJFWg2P+AuOIxoCgKF+2Oq4d4Zd0OUA==

node-api-headers@^0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/node-api-headers/-/node-api-headers-0.0.2.tgz#31f4c6c2750b63e598128e76a60aefca6d76ac5d"
integrity sha512-YsjmaKGPDkmhoNKIpkChtCsPVaRE0a274IdERKnuc/E8K1UJdBZ4/mvI006OijlQZHCfpRNOH3dfHQs92se8gg==
node-api-headers@^1.1.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/node-api-headers/-/node-api-headers-1.5.0.tgz#73a0bab642c77e39b815b6d24ad4c6b56f695912"
integrity sha512-Yi/FgnN8IU/Cd6KeLxyHkylBUvDTsSScT0Tna2zTrz8klmc8qF2ppj6Q1LHsmOueJWhigQwR4cO2p0XBGW5IaQ==

npmlog@^6.0.2:
version "6.0.2"
Expand Down Expand Up @@ -452,10 +446,10 @@ safe-buffer@~5.2.0:
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==

semver@^7.3.8:
version "7.6.3"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.6.3.tgz#980f7b5550bc175fb4dc09403085627f9eb33143"
integrity sha512-oVekP1cKtI+CTDvHWYFUcMtsK/00wmAEfyqKfNdARm8u1wNVhSgaX7A8d4UuIlUI5e84iEwOhs7ZPYRmzU9U6A==
semver@^7.5.4:
version "7.7.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.7.1.tgz#abd5098d82b18c6c81f6074ff2647fd3e7220c9f"
integrity sha512-hlq8tAfn0m/61p4BVRcPzIGr6LKiMwo4VM6dGi6pt4qcRkmNzTcWq6eCEjEh+qXjkMDvPlOFFSGwQjoEa6gyMA==

set-blocking@^2.0.0:
version "2.0.0"
Expand Down Expand Up @@ -500,7 +494,7 @@ supports-preserve-symlinks-flag@^1.0.0:
resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09"
integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==

tar@^6.1.11:
tar@^6.2.0:
version "6.2.1"
resolved "https://registry.yarnpkg.com/tar/-/tar-6.2.1.tgz#717549c541bc3c2af15751bea94b1dd068d4b03a"
integrity sha512-DZ4yORTwrbTj/7MZYq2w+/ZFdI6OZ/f9SFHR+71gIVUZhOQPHzVCLpvRnPgyaMpfWxxk/4ONva3GQSyNIKRv6A==
Expand Down Expand Up @@ -570,7 +564,7 @@ yargs-parser@^21.1.1:
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.1.1.tgz#9096bceebf990d21bb31fa9516e0ede294a77d35"
integrity sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==

yargs@^17.6.0:
yargs@^17.7.2:
version "17.7.2"
resolved "https://registry.yarnpkg.com/yargs/-/yargs-17.7.2.tgz#991df39aca675a192b816e1e0363f9d75d2aa269"
integrity sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==
Expand Down