Skip to content

Commit

Permalink
CBL-6606 : LogSinks C++ Wrapper API (#588)
Browse files Browse the repository at this point in the history
* CBL-6606 : LogSinks C++ Wrapper API

* Update CreateDir() per code review suggestion

* Update to use vector search release package for testing
  • Loading branch information
pasin authored Jan 18, 2025
1 parent bf47956 commit 97e5361
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 58 deletions.
12 changes: 12 additions & 0 deletions CBL_C.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@
40ACB2392C2F98B200CBE8F2 /* Prediction.hh in Headers */ = {isa = PBXBuildFile; fileRef = 40ACB2382C2F20DA00CBE8F2 /* Prediction.hh */; };
40ACB23A2C2F98B600CBE8F2 /* VectorIndex.hh in Headers */ = {isa = PBXBuildFile; fileRef = 400AB0542C2E7AC300DB6223 /* VectorIndex.hh */; };
40D1862529B6D1A50061AA85 /* Collection.hh in Headers */ = {isa = PBXBuildFile; fileRef = FCC064BD287CBD95000C5BD7 /* Collection.hh */; };
40E32F402D2F3ED4000CED57 /* LogSinks.hh in Headers */ = {isa = PBXBuildFile; fileRef = 40E32F3F2D2F3ED4000CED57 /* LogSinks.hh */; };
40E32F422D2F45A3000CED57 /* LogTest_Cpp.cc in Sources */ = {isa = PBXBuildFile; fileRef = 40E32F412D2F45A3000CED57 /* LogTest_Cpp.cc */; };
40E32F432D2F45A3000CED57 /* LogTest_Cpp.cc in Sources */ = {isa = PBXBuildFile; fileRef = 40E32F412D2F45A3000CED57 /* LogTest_Cpp.cc */; };
40E32F442D2F45A3000CED57 /* LogTest_Cpp.cc in Sources */ = {isa = PBXBuildFile; fileRef = 40E32F412D2F45A3000CED57 /* LogTest_Cpp.cc */; };
40E7CA732BFE7336004BE7E1 /* ContextManager.cc in Sources */ = {isa = PBXBuildFile; fileRef = 40E7CA632BFE7336004BE7E1 /* ContextManager.cc */; };
40E7CA742BFE7336004BE7E1 /* ContextManager.hh in Headers */ = {isa = PBXBuildFile; fileRef = 40E7CA722BFE7336004BE7E1 /* ContextManager.hh */; };
40F902D82B9F7B12002EA0A0 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 40F902C82B9F7AF6002EA0A0 /* PrivacyInfo.xcprivacy */; };
Expand Down Expand Up @@ -508,6 +512,8 @@
4083FCBF2BAA203A0061509D /* CBLPrediction_Internal.hh */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = CBLPrediction_Internal.hh; sourceTree = "<group>"; };
4083FCC12BAA38F30061509D /* CBLPrediction.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = CBLPrediction.cc; sourceTree = "<group>"; };
40ACB2382C2F20DA00CBE8F2 /* Prediction.hh */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Prediction.hh; sourceTree = "<group>"; };
40E32F3F2D2F3ED4000CED57 /* LogSinks.hh */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = LogSinks.hh; sourceTree = "<group>"; };
40E32F412D2F45A3000CED57 /* LogTest_Cpp.cc */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = LogTest_Cpp.cc; sourceTree = "<group>"; };
40E7CA632BFE7336004BE7E1 /* ContextManager.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ContextManager.cc; sourceTree = "<group>"; };
40E7CA722BFE7336004BE7E1 /* ContextManager.hh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = ContextManager.hh; sourceTree = "<group>"; };
40F902C82B9F7AF6002EA0A0 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
Expand Down Expand Up @@ -815,6 +821,7 @@
FCC064BD287CBD95000C5BD7 /* Collection.hh */,
27B61DD821DEEC540027CCDB /* Database.hh */,
277FEE4E21DEF54D00B60E3C /* Document.hh */,
40E32F3F2D2F3ED4000CED57 /* LogSinks.hh */,
40ACB2382C2F20DA00CBE8F2 /* Prediction.hh */,
277FEE4F21E6AB1100B60E3C /* Query.hh */,
400AB0522C2E66B500DB6223 /* QueryIndex.hh */,
Expand Down Expand Up @@ -845,6 +852,7 @@
FCC063F2285BA641000C5BD7 /* DocumentTest.cc */,
FC0907E628AD763600201B07 /* DocumentTest_Cpp.cc */,
93965A6226A7CD50008728EE /* LogTest.cc */,
40E32F412D2F45A3000CED57 /* LogTest_Cpp.cc */,
275B359D234D064600FE9CF0 /* QueryTest.cc */,
AED22E6F291BE1AF005F065D /* QueryTest_Cpp.cc */,
2736A625242E5A07002B9D65 /* ReplicatorTest.hh */,
Expand Down Expand Up @@ -1021,6 +1029,7 @@
27984E322249A247000FE777 /* Base.hh in Headers */,
40FE2DD22C082466005E99E9 /* CBLQueryIndex.h in Headers */,
EA34BC5A2C3698B8000CEA70 /* CBLPrediction.h in Headers */,
40E32F402D2F3ED4000CED57 /* LogSinks.hh in Headers */,
93C70CE126C4D3F90093E927 /* CBLEncryptable.h in Headers */,
40ACB23A2C2F98B600CBE8F2 /* VectorIndex.hh in Headers */,
27984E332249A247000FE777 /* Blob.hh in Headers */,
Expand Down Expand Up @@ -1627,6 +1636,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
40E32F432D2F45A3000CED57 /* LogTest_Cpp.cc in Sources */,
93965A6426A7CD50008728EE /* LogTest.cc in Sources */,
275B3599234C158900FE9CF0 /* CouchbaseLiteTests.mm in Sources */,
275B358E23481D0C00FE9CF0 /* CBLTest.c in Sources */,
Expand Down Expand Up @@ -1675,6 +1685,7 @@
27C9B5F321F7EE670040BC45 /* CBLTest.c in Sources */,
FCC06403285BCAC9000C5BD7 /* DocumentTest.cc in Sources */,
40FE2E0E2C12AEF6005E99E9 /* LazyVectorIndexTest.cc in Sources */,
40E32F422D2F45A3000CED57 /* LogTest_Cpp.cc in Sources */,
27B61DAF21D6E4B70027CCDB /* CBLTest.cc in Sources */,
FCD8298C283591D4004AA814 /* CollectionTest.cc in Sources */,
FC645E2D29088211007D5536 /* Platform_Apple.mm in Sources */,
Expand Down Expand Up @@ -1731,6 +1742,7 @@
FC645E1029085C26007D5536 /* BlobTest.cc in Sources */,
FC645E1129085C2A007D5536 /* BlobTest_Cpp.cc in Sources */,
FC645E1429085C5D007D5536 /* DatabaseTest_Cpp.cc in Sources */,
40E32F442D2F45A3000CED57 /* LogTest_Cpp.cc in Sources */,
FC645E1629085C6B007D5536 /* DocumentTest_Cpp.cc in Sources */,
FC645E1829085C71007D5536 /* QueryTest.cc in Sources */,
FC645E0F29085C0A007D5536 /* CBLTest.cc in Sources */,
Expand Down
1 change: 1 addition & 0 deletions include/cbl++/CouchbaseLite.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "Collection.hh"
#include "Database.hh"
#include "Document.hh"
#include "LogSinks.hh"
#include "Prediction.hh"
#include "Query.hh"
#include "QueryIndex.hh"
Expand Down
56 changes: 56 additions & 0 deletions include/cbl++/LogSinks.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// LogSinks.hh
//
// Copyright (c) 2025 Couchbase, Inc 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
//
// 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.
//

// VOLATILE API: Couchbase Lite C++ API is not finalized, and may change in
// future releases.

#pragma once
#include "cbl/CBLLogSinks.h"

CBL_ASSUME_NONNULL_BEGIN

namespace cbl {
class LogSinks {
public:
static void setConsole(const CBLConsoleLogSink& sink) {
CBLLogSinks_SetConsole(sink);
}

static CBLConsoleLogSink console() {
return CBLLogSinks_Console();
}

static void setCustom(const CBLCustomLogSink& sink) {
CBLLogSinks_SetCustom(sink);
}

static CBLCustomLogSink custom() {
return CBLLogSinks_CustomSink();
}

static void setFile(const CBLFileLogSink& sink) {
CBLLogSinks_SetFile(sink);
}

static CBLFileLogSink file() {
return CBLLogSinks_File();
}
};
}

CBL_ASSUME_NONNULL_END
20 changes: 13 additions & 7 deletions scripts/download_vector_search_extension.ps1
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
Push-Location $PSScriptRoot\..\test\extensions

$VsVersionFile = Join-Path (Get-Location).Path "version.txt"
$Version = (Get-Content $VsVersionFile).Split('-')[0]
$BlNum = (Get-Content $VsVersionFile).Split('-')[1]
$VersionNumber = Get-Content $VsVersionFile

if ($VersionNumber -like "*-*") {
$Version = $VersionNumber.Split('-')[0]
$BuildNum = $VersionNumber.Split('-')[1]
$ZipFilename = "couchbase-lite-vector-search-${Version}-${BuildNum}-windows-x86_64.zip"
$Url = "http://latestbuilds.service.couchbase.com/builds/latestbuilds/couchbase-lite-vector-search/${Version}/${BlNum}/${ZipFilename}"
} else {
$Version = $VersionNumber
$ZipFilename = "couchbase-lite-vector-search-${Version}-windows-x86_64.zip"
$Url = "https://packages.couchbase.com/releases/couchbase-lite-vector-search/${Version}/${ZipFilename}"
}

Remove-Item -Path windows\x86_64 -Recurse -Force -ErrorAction Ignore
New-Item -Path windows\x86_64 -ItemType Directory | Out-Null
Push-Location windows\x86_64

Write-Output "Downloading Vector Search Framework ${Version}-${BlNum} ..."

$Filename = "couchbase-lite-vector-search-${Version}-${BlNum}-windows-x86_64"
$ZipFilename = "${Filename}.zip"
$Url = "http://latestbuilds.service.couchbase.com/builds/latestbuilds/couchbase-lite-vector-search/${Version}/${BlNum}/${ZipFilename}"
Write-Output "Downloading Vector Search Framework from ${Url}"
$DestFile = Join-Path (Get-Location).Path $ZipFilename
Invoke-WebRequest $Url -OutFile $DestFile
Expand-Archive -Path $ZipFilename -DestinationPath (Get-Location).Path
Expand Down
41 changes: 24 additions & 17 deletions scripts/download_vector_search_extension.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,44 @@ pushd "${EXTENSIONS_DIR}" > /dev/null
EXTENSIONS_DIR=`pwd`

VS_VERSION_FILE="${EXTENSIONS_DIR}/version.txt"
VERSION=$(cat ${VS_VERSION_FILE} | cut -f1 -d-)
BLD_NUM=$(cat ${VS_VERSION_FILE} | cut -f2 -d-)
VERSION_NUMBER=$(cat ${VS_VERSION_FILE})

if [[ "$VERSION_NUMBER" == *"-"* ]]; then
VERSION="${VERSION_NUMBER%-*}"
BLD_NUM="${VERSION_NUMBER##*-}"
XCFRAMEWORK_ZIP_FILENAME="couchbase-lite-vector-search_xcframework_${VERSION}-${BLD_NUM}.zip"
BASE_ZIP_FILENAME="couchbase-lite-vector-search-${VERSION}-${BLD_NUM}"
BASE_DOWNLOAD_URL="http://latestbuilds.service.couchbase.com/builds/latestbuilds/couchbase-lite-c/${VERSION}/${BLD_NUM}"
else
VERSION="$VERSION_NUMBER"
XCFRAMEWORK_ZIP_FILENAME="couchbase-lite-vector-search_xcframework_${VERSION}.zip"
BASE_ZIP_FILENAME="couchbase-lite-vector-search-${VERSION}"
BASE_DOWNLOAD_URL="https://packages.couchbase.com/releases/couchbase-lite-vector-search/${VERSION}"
fi

function download() {
echo "Download Vector Search Framework ${VERSION}-${BLD_NUM} ..."
curl -O http://latestbuilds.service.couchbase.com/builds/latestbuilds/couchbase-lite-vector-search/${VERSION}/${BLD_NUM}/${1}
DOWNLOAD_URL="${BASE_DOWNLOAD_URL}/${1}"
echo "Download Version Search from ${DOWNLOAD_URL}"
curl -O ${DOWNLOAD_URL}
}

ZIP_BASE_FILENAME="couchbase-lite-vector-search-${VERSION}-${BLD_NUM}"

if [ "${PLATFORM}" == "apple" ]
then
rm -rf apple && mkdir -p apple
pushd apple > /dev/null

FILENAME="couchbase-lite-vector-search_xcframework_${VERSION}-${BLD_NUM}"
ZIP_FILENAME="${FILENAME}.zip"
download "${ZIP_FILENAME}"
unzip "${ZIP_FILENAME}"

download "${XCFRAMEWORK_ZIP_FILENAME}"
unzip "${XCFRAMEWORK_ZIP_FILENAME}"
cp CouchbaseLiteVectorSearch.xcframework/macos-arm64_x86_64/CouchbaseLiteVectorSearch.framework/Versions/Current/CouchbaseLiteVectorSearch CouchbaseLiteVectorSearch.dylib
rm -rf "${ZIP_FILENAME}"
rm -rf "${XCFRAMEWORK_FILENAME}"

popd > /dev/null
elif [ "${PLATFORM}" == "linux" ]
then
rm -rf linux/x86_64 && mkdir -p linux/x86_64
pushd linux/x86_64 > /dev/null

ZIP_FILENAME="${ZIP_BASE_FILENAME}"-linux-x86_64.zip
ZIP_FILENAME="${BASE_ZIP_FILENAME}"-linux-x86_64.zip
download "${ZIP_FILENAME}"
unzip "${ZIP_FILENAME}"

Expand All @@ -69,8 +77,7 @@ then
rm -rf windows/x86_64 && mkdir -p windows/x86_64
pushd windows/x86_64 > /dev/null

FILENAME="${ZIP_BASE_FILENAME}-windows-x86_64"
ZIP_FILENAME="${FILENAME}.zip"
ZIP_FILENAME="${BASE_ZIP_FILENAME}-windows-x86_64.zip"
download "${ZIP_FILENAME}"
unzip "${ZIP_FILENAME}"

Expand All @@ -85,7 +92,7 @@ then
rm -rf android/x86_64 && mkdir -p android/x86_64
pushd android/x86_64 > /dev/null

ZIP_FILENAME="${ZIP_BASE_FILENAME}-android-x86_64.zip"
ZIP_FILENAME="${BASE_ZIP_FILENAME}-android-x86_64.zip"
download "${ZIP_FILENAME}"
unzip "${ZIP_FILENAME}"

Expand All @@ -98,7 +105,7 @@ then
rm -rf android/arm64-v8a && mkdir -p android/arm64-v8a
pushd android/arm64-v8a > /dev/null

ZIP_FILENAME="${ZIP_BASE_FILENAME}-android-arm64-v8a.zip"
ZIP_FILENAME="${BASE_ZIP_FILENAME}-android-arm64-v8a.zip"
download "${ZIP_FILENAME}"
unzip "${ZIP_FILENAME}"
rm -rf "${ZIP_FILENAME}"
Expand Down
10 changes: 10 additions & 0 deletions test/CBLTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ string GetExtensionPath() {

#endif

void CreateDir(const string &dir) {
#ifdef WIN32
if (_mkdir(dir.c_str()) != 0 && errno != EEXIST)
FAIL("Can't create temp directory: errno " << errno);
#else
if (mkdir(dir.c_str(), 0744) != 0 && errno != EEXIST)
FAIL("Can't create temp directory: errno " << errno);
#endif
}

bool ReadFileByLines(const string &path, const function<bool(FLSlice)> &callback) {
INFO("Reading lines from " << path);
fstream fd(path.c_str(), ios_base::in);
Expand Down
12 changes: 9 additions & 3 deletions test/CBLTest.hh
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@
#include <iostream>
#include <string>

#ifdef WIN32
constexpr char kPathSeparator[] = "\\";
#ifdef _MSC_VER
static const char kPathSeparator = '\\';
static const char kBackupPathSeparator = '/';
static const char* kCurrentDirectory = ".\\";
#else
constexpr char kPathSeparator[] = "/";
static const char kPathSeparator = '/';
static const char kBackupPathSeparator = '\\';
static const char* kCurrentDirectory = "./";
#endif

#ifdef __APPLE__
Expand Down Expand Up @@ -101,6 +105,8 @@ void EnableVectorSearch();

#endif

void CreateDir(const std::string &dir);

bool ReadFileByLines(const std::string &path, const std::function<bool(FLSlice)> &callback);

unsigned ImportJSONLines(std::string filename, CBLDatabase* database);
Expand Down
40 changes: 10 additions & 30 deletions test/LogTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,6 @@
using namespace std;
using namespace fleece;

#ifdef _MSC_VER
static const char kSeparatorChar = '\\';
static const char kBackupSeparatorChar = '/';
static const char* kCurrentDir = ".\\";
#else
static const char kSeparatorChar = '/';
static const char kBackupSeparatorChar = '\\';
static const char* kCurrentDir = "./";
#endif

static const array<CBLLogLevel, 6> kLogLevels = {{
kCBLLogDebug, kCBLLogVerbose, kCBLLogInfo, kCBLLogWarning, kCBLLogError, kCBLLogNone
}};
Expand Down Expand Up @@ -103,11 +93,11 @@ class LogTest : public CBLTest {
while ((entry = readdir(dir)) != NULL) {
string filename = string(entry->d_name);
#ifndef WIN32
string path = logDir() + kSeparatorChar + filename;
string path = logDir() + kPathSeparator + filename;
struct stat statEntry;
stat(path.c_str(), &statEntry);
#else
string path = logDir() + kSeparatorChar + filename;
string path = logDir() + kPathSeparator + filename;
struct _stat statEntry;
_stat(path.c_str(), &statEntry);
#endif
Expand Down Expand Up @@ -189,12 +179,12 @@ class LogTest : public CBLTest {

void prepareLogDir() {
// Base:
string dir = string(CBLTest::databaseDir()) + kSeparatorChar + "CBLLogTest";
createDir(dir);
string dir = string(CBLTest::databaseDir()) + kPathSeparator + "CBLLogTest";
CreateDir(dir);

// Log dir:
dir += kSeparatorChar + to_string(++sLogDirCount);
createDir(dir);
dir += kPathSeparator + to_string(++sLogDirCount);
CreateDir(dir);

_logDir = dir;
}
Expand All @@ -214,19 +204,9 @@ class LogTest : public CBLTest {

// File Utils:

void createDir(string dir) {
#ifndef WIN32
if (mkdir(dir.c_str(), 0744) != 0 && errno != EEXIST)
FAIL("Can't create temp directory: errno " << errno);
#else
if (_mkdir(dir.c_str()) != 0 && errno != EEXIST)
FAIL("Can't create temp directory: errno " << errno);
#endif
}

pair<string,string> splitExtension(const string &file) {
auto dot = file.rfind('.');
auto lastSlash = file.rfind(kSeparatorChar);
auto lastSlash = file.rfind(kPathSeparator);
if (dot == string::npos || (lastSlash != string::npos && dot < lastSlash))
return {file, ""};
else
Expand All @@ -235,10 +215,10 @@ class LogTest : public CBLTest {

pair<string,string> splitPath(const string &path) {
string dirname, basename;
auto slash = path.rfind(kSeparatorChar);
auto backupSlash = path.rfind(kBackupSeparatorChar);
auto slash = path.rfind(kPathSeparator);
auto backupSlash = path.rfind(kBackupPathSeparator);
if (slash == string::npos && backupSlash == string::npos) {
return{ kCurrentDir, string(path) };
return{ kCurrentDirectory, string(path) };
}

if (slash == string::npos) {
Expand Down
Loading

0 comments on commit 97e5361

Please sign in to comment.