Skip to content

Commit

Permalink
Add logging section
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeross committed Jun 20, 2024
1 parent dbf1a9b commit f78d283
Show file tree
Hide file tree
Showing 16 changed files with 429 additions and 243 deletions.
23 changes: 23 additions & 0 deletions snippets/csharp/GettingStarted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,27 @@ public void FetchNodeInfo(BlockingBreezServices sdk)
}
// ANCHOR_END: fetch-balance
}

// ANCHOR: logging
public void GettingStartedLogging()
{
try
{
sdk = BreezSdkMethods.SetLogStream(new SdkLogStream());

Check failure on line 85 in snippets/csharp/GettingStarted.cs

View workflow job for this annotation

GitHub Actions / Check C# snippets

The name 'sdk' does not exist in the current context

Check failure on line 85 in snippets/csharp/GettingStarted.cs

View workflow job for this annotation

GitHub Actions / Check C# snippets

The name 'sdk' does not exist in the current context
}
catch (Exception)
{
// Handle error
}
}

class SdkLogStream : LogStream
{
public void Log(LogEntry l)
{
Console.WriteLine($"Received Log [{l.Level}]: {l.Line}");

Check failure on line 97 in snippets/csharp/GettingStarted.cs

View workflow job for this annotation

GitHub Actions / Check C# snippets

'LogEntry' does not contain a definition for 'Level' and no accessible extension method 'Level' accepting a first argument of type 'LogEntry' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 97 in snippets/csharp/GettingStarted.cs

View workflow job for this annotation

GitHub Actions / Check C# snippets

'LogEntry' does not contain a definition for 'Line' and no accessible extension method 'Line' accepting a first argument of type 'LogEntry' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 97 in snippets/csharp/GettingStarted.cs

View workflow job for this annotation

GitHub Actions / Check C# snippets

'LogEntry' does not contain a definition for 'Level' and no accessible extension method 'Level' accepting a first argument of type 'LogEntry' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 97 in snippets/csharp/GettingStarted.cs

View workflow job for this annotation

GitHub Actions / Check C# snippets

'LogEntry' does not contain a definition for 'Line' and no accessible extension method 'Line' accepting a first argument of type 'LogEntry' could be found (are you missing a using directive or an assembly reference?)
}
}
// ANCHOR_END: logging

}
10 changes: 10 additions & 0 deletions snippets/dart_snippets/lib/getting_started.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,13 @@ Future<void> fetchBalance(String lspId) async {
}
// ANCHOR_END: fetch-balance
}

// ANCHOR: logging
void onLogEntry(log) {
print("Received log ${log.level}]: ${log.line}");
}

void logging(BreezSDK breezSDK) {
breezSDK.logStream.listen(onLogEntry);
}
// ANCHOR_END: logging
15 changes: 15 additions & 0 deletions snippets/go/getting_started.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,18 @@ func FetchBalance() {
}
// ANCHOR_END: fetch-balance
}

// ANCHOR: logging
type SdkLogStream struct{}

func (SdkLogStream) Log(l breez_sdk.LogEntry) {
log.Printf("Received log [%v]: %v", l.Level, l.Line)
}

func GettingStartedLogging() {
err := breez_sdk.SetLogStream(SdkLogStream{})
if err != nil {
log.Fatalf("SetLogStream failed: %#v", err)
}
}
// ANCHOR_END: logging
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,20 @@ class GettingStarted {
}
// ANCHOR_END: fetch-balance
}

fun logging() {
// ANCHOR: logging
class SDKLogStream : LogStream {
override fun log(l: LogEntry) {
// Log.v("SDKListener", "Received log [${l.level}]: ${l.line}")
}
}

try {
setLogStream(SDKLogStream())
} catch (e: Exception) {
// handle error
}
// ANCHOR_END: logging
}
}
11 changes: 11 additions & 0 deletions snippets/python/src/getting_started.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,15 @@ def getting_started_node_info(sdk_services):
logging.error(error)
raise

# ANCHOR: logging
class SDKLogStream(breez_sdk.LogStream):
def log(self, l):
print("Received log [", l.level, "]: ", l.line)

def logging():
try:
breez_sdk.set_log_stream(SDKLogStream())
except Exception as error:
print(error)
raise
# ANCHOR_END: logging
14 changes: 13 additions & 1 deletion snippets/react-native/getting_started.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
mnemonicToSeed,
type NodeConfig,
NodeConfigVariant,
nodeInfo
nodeInfo,
LogEntry,
setLogStream
} from '@breeztech/react-native-breez-sdk'

const exampleGettingStarted = async () => {
Expand Down Expand Up @@ -78,3 +80,13 @@ const exampleFetchNodeInfo = async () => {
}
// ANCHOR_END: fetch-balance
}

const exampleLogging = async () => {
// ANCHOR: logging
const onLogEntry = (l: LogEntry) => {
console.log(`Received log [${l.level}]: ${l.line}`)
}

const subscription = await setLogStream(onLogEntry)
// ANCHOR_END: logging
}
2 changes: 1 addition & 1 deletion snippets/react-native/production.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {

const productionNodeConfig = (): NodeConfig => {
// ANCHOR: moving-to-production
// Read in your Greenlight credentials from the file file
// Read in your Greenlight credentials from the file
// system, environment variable or build config
const deviceKey: number[] = []
const deviceCert: number[] = []
Expand Down
15 changes: 14 additions & 1 deletion snippets/rust/src/getting_started.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{fs, path::PathBuf};
use std::sync::Arc;

use anyhow::Result;
use bip39::{Language, Mnemonic};
use breez_sdk_core::*;
use std::sync::Arc;

use crate::AppEventListener;

Expand Down Expand Up @@ -60,3 +62,14 @@ async fn getting_started_node_info(sdk: Arc<BreezServices>) -> Result<()> {

Ok(())
}

async fn getting_started_logging(data_dir: String) -> Result<()> {
// ANCHOR: logging
let data_dir_path = PathBuf::from(&data_dir);
fs::create_dir_all(data_dir_path)?;

BreezServices::init_logging(&data_dir, None)?;
// ANCHOR_END: logging

Ok(())
}
12 changes: 12 additions & 0 deletions snippets/swift/BreezSDKExamples/Sources/GettingStarted.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,15 @@ func gettingStartedNodeInfo(sdk: BlockingBreezServices) {
}
// ANCHOR_END: fetch-balance
}

// ANCHOR: logging
class SDKLogStream: LogStream {
func log(l: LogEntry) {
print("Received log [", l.level, "]: ", l.line)
}
}

func logging() throws {
try? setLogStream(log_stream: SDKLogStream())
}
// ANCHOR_END: logging
5 changes: 4 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
# API Overview

- [Getting Started](guide/getting_started.md)
- [Installing](guide/install.md)
- [Installing the Breez SDK](guide/install.md)
- [Connecting to a node](guide/connecting.md)
- [Getting the node state](guide/node_state.md)
- [Adding logging](guide/logging.md)
- [Paying in Lightning](guide/payments.md)
- [Receiving payments](guide/receive_payment.md)
- [Sending payments](guide/send_payment.md)
Expand Down
157 changes: 157 additions & 0 deletions src/guide/connecting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
# Connecting to a node

The first step is to construct the SDK configuration. In it the environment and Greenlight node configuration is defined, whether you are using an invite code or partner credentials.

The SDK uses the config working directory to store the state of the SDK instance. Once a connection has been established with a node, the working directory can only be used for that node. When handling multiple instances of the SDK, one per node, each needs to have a different working directory defined.

Now you are ready to interact with the SDK.

<custom-tabs category="lang">
<div slot="title">Rust</div>
<section>

```rust,ignore
{{#include ../../snippets/rust/src/getting_started.rs:init-sdk}}
```

</section>

<div slot="title">Swift</div>
<section>

```swift,ignore
{{#include ../../snippets/swift/BreezSDKExamples/Sources/GettingStarted.swift:init-sdk}}
```

</section>

<div slot="title">Kotlin</div>
<section>

```kotlin,ignore
{{#include ../../snippets/kotlin_mpp_lib/shared/src/commonMain/kotlin/com/example/kotlinmpplib/GettingStarted.kt:init-sdk}}
```

</section>

<div slot="title">React Native</div>
<section>

```typescript
{{#include ../../snippets/react-native/getting_started.ts:init-sdk}}
```

</section>

<div slot="title">Dart</div>
<section>

```dart,ignore
{{#include ../../snippets/dart_snippets/lib/getting_started.dart:init-sdk}}
```
</section>

<div slot="title">Python</div>
<section>

```python,ignore
{{#include ../../snippets/python/src/getting_started.py:init-sdk}}
```
</section>

<div slot="title">Go</div>
<section>

```go,ignore
{{#include ../../snippets/go/getting_started.go:init-sdk}}
```
</section>

<div slot="title">C#</div>
<section>

```cs,ignore
{{#include ../../snippets/csharp/GettingStarted.cs:init-sdk}}
```
</section>
</custom-tabs>

<div class="warning">
<h4>Developer note</h4>

By default the config working directory is set to `./`. Some platforms may require that you use an application specific directory that is writable within the application sandbox. For example applications running on Android or iOS.

</div>
<br/>

To connect to an already existing node without registering a new one, use the `restore_only` flag in the connect request. If the node does not exist it will result in an error.

<custom-tabs category="lang">
<div slot="title">Rust</div>
<section>

```rust,ignore
{{#include ../../snippets/rust/src/getting_started.rs:init-sdk-restore-only}}
```

</section>

<div slot="title">Swift</div>
<section>

```swift,ignore
{{#include ../../snippets/swift/BreezSDKExamples/Sources/GettingStarted.swift:init-sdk-restore-only}}
```

</section>

<div slot="title">Kotlin</div>
<section>

```kotlin,ignore
{{#include ../../snippets/kotlin_mpp_lib/shared/src/commonMain/kotlin/com/example/kotlinmpplib/GettingStarted.kt:init-sdk-restore-only}}
```

</section>

<div slot="title">React Native</div>
<section>

```typescript
{{#include ../../snippets/react-native/getting_started.ts:init-sdk-restore-only}}
```

</section>

<div slot="title">Dart</div>
<section>

```dart,ignore
{{#include ../../snippets/dart_snippets/lib/getting_started.dart:init-sdk-restore-only}}
```
</section>

<div slot="title">Python</div>
<section>

```python,ignore
{{#include ../../snippets/python/src/getting_started.py:init-sdk-restore-only}}
```
</section>

<div slot="title">Go</div>
<section>

```go,ignore
{{#include ../../snippets/go/getting_started.go:init-sdk-restore-only}}
```
</section>

<div slot="title">C#</div>
<section>

```cs,ignore
{{#include ../../snippets/csharp/GettingStarted.cs:init-sdk-restore-only}}
```
</section>
</custom-tabs>
Loading

0 comments on commit f78d283

Please sign in to comment.