Skip to content

Commit eb95c5b

Browse files
authored
Enable relaying log events via FFI (#542)
<!-- Thanks for sending a pull request! Here are some tips for you: 1. If this is your first time, please read our contributor guidelines: https://github.com/delta-incubator/delta-kernel-rs/blob/main/CONTRIBUTING.md 2. Run `cargo t --all-features --all-targets` to get started testing, and run `cargo fmt`. 3. Ensure you have added or run the appropriate tests for your PR. 4. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP] Your PR title ...'. 5. Be sure to keep the PR description updated to reflect all changes. --> ## What changes are proposed in this pull request? <!-- Please clarify what changes you are proposing and why the changes are needed. The purpose of this section is to outline the changes, why they are needed, and how this PR fixes the issue. If the reason for the change is already explained clearly in an issue, then it does not need to be restated here. 1. If you propose a new API or feature, clarify the use case for a new API or feature. 2. If you fix a bug, you can clarify why it is a bug. --> This PR provides 3 ways for engines to hook into our logging: 1. getting a struct `Event` type with relevant information that they can format 2. A very simple call that will just pick some defaults and give them a string 3. A slightly more complex one that lets them control the string formatting a little, but still ultimately just hands over a string Note that the `Compact` format seems a bit broken upstream. I've filled tokio-rs/tracing#3157 for that. <!-- Uncomment this section if there are any changes affecting public APIs: ### This PR affects the following public APIs If there are breaking changes, please ensure the `breaking-changes` label gets added by CI, and describe why the changes are needed. Note that _new_ public APIs are not considered breaking. --> Adds 3 new public ffi APIs ## How was this change tested? <!-- Please make sure to add test cases that check the changes thoroughly including negative and positive cases if possible. If it was tested in a way different from regular unit tests, please clarify how you tested, ideally via a reproducible test documented in the PR description. --> Running in `read_table`, and unit tests
1 parent 695b841 commit eb95c5b

File tree

7 files changed

+689
-5
lines changed

7 files changed

+689
-5
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ jobs:
145145
cargo build
146146
popd
147147
pushd ffi
148-
cargo b --features default-engine,sync-engine,test-ffi
148+
cargo b --features default-engine,sync-engine,test-ffi,tracing
149149
popd
150150
- name: build and run read-table test
151151
run: |

ffi/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ crate-type = ["lib", "cdylib", "staticlib"]
1515

1616
[dependencies]
1717
tracing = "0.1"
18+
tracing-core = { version = "0.1", optional = true }
19+
tracing-subscriber = { version = "0.3", optional = true, features = [ "json" ] }
1820
url = "2"
1921
delta_kernel = { path = "../kernel", default-features = false, features = [
2022
"developer-visibility",
@@ -51,6 +53,7 @@ default-engine = [
5153
"arrow-data",
5254
"arrow-schema",
5355
]
56+
tracing = [ "tracing-core", "tracing-subscriber" ]
5457
sync-engine = ["delta_kernel/sync-engine"]
5558
developer-visibility = []
5659
test-ffi = []

ffi/examples/read-table/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ This example is built with [cmake]. Instructions below assume you start in the d
1010
Note that prior to building these examples you must build `delta_kernel_ffi` (see [the FFI readme] for details). TLDR:
1111
```bash
1212
# from repo root
13-
$ cargo build -p delta_kernel_ffi [--release] [--features default-engine]
13+
$ cargo build -p delta_kernel_ffi [--release] [--features default-engine, tracing]
1414
# from ffi/ dir
15-
$ cargo build [--release] [--features default-engine]
15+
$ cargo build [--release] [--features default-engine, tracing]
1616
```
1717

1818
There are two configurations that can currently be configured in cmake:
@@ -62,4 +62,4 @@ If you don't want to have to install this, you can run `ccmake ..` (`cmake-gui.e
6262
from the `build` directory, and turn `OFF`/uncheckmark `PRINT_DATA`. Then "configure" and
6363
"generate" and follow the above instructions again.
6464

65-
[cmake]: https://cmake.org/
65+
[cmake]: https://cmake.org/

ffi/examples/read-table/read_table.c

+56
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <inttypes.h>
22
#include <stdio.h>
33
#include <string.h>
4+
#include <sys/time.h>
45

56
#include "arrow.h"
67
#include "read_table.h"
@@ -150,13 +151,68 @@ void free_partition_list(PartitionList* list) {
150151
free(list);
151152
}
152153

154+
static const char *LEVEL_STRING[] = {
155+
"ERROR", "WARN", "INFO", "DEBUG", "TRACE"
156+
};
157+
158+
// define some ansi color escapes so we can have nice colored output in our logs
159+
#define RED "\x1b[31m"
160+
#define BLUE "\x1b[34m"
161+
#define DIM "\x1b[2m"
162+
#define RESET "\x1b[0m"
163+
164+
void tracing_callback(struct Event event) {
165+
struct timeval tv;
166+
char buffer[32];
167+
gettimeofday(&tv, NULL);
168+
struct tm *tm_info = gmtime(&tv.tv_sec);
169+
strftime(buffer, 26, "%Y-%m-%dT%H:%M:%S", tm_info);
170+
char* level_color = event.level < 3 ? RED : BLUE;
171+
printf(
172+
"%s%s.%06dZ%s [%sKernel %s%s] %s%.*s%s: %.*s\n",
173+
DIM,
174+
buffer,
175+
(int)tv.tv_usec, // safe, microseconds are in int range
176+
RESET,
177+
level_color,
178+
LEVEL_STRING[event.level],
179+
RESET,
180+
DIM,
181+
(int)event.target.len,
182+
event.target.ptr,
183+
RESET,
184+
(int)event.message.len,
185+
event.message.ptr);
186+
if (event.file.ptr) {
187+
printf(
188+
" %sat%s %.*s:%i\n",
189+
DIM,
190+
RESET,
191+
(int)event.file.len,
192+
event.file.ptr,
193+
event.line);
194+
}
195+
}
196+
197+
void log_line_callback(KernelStringSlice line) {
198+
printf("%.*s", (int)line.len, line.ptr);
199+
}
200+
153201
int main(int argc, char* argv[])
154202
{
155203
if (argc < 2) {
156204
printf("Usage: %s table/path\n", argv[0]);
157205
return -1;
158206
}
159207

208+
#ifdef VERBOSE
209+
enable_event_tracing(tracing_callback, TRACE);
210+
// we could also do something like this if we want less control over formatting
211+
// enable_formatted_log_line_tracing(log_line_callback, TRACE, FULL, true, true, false, false);
212+
#else
213+
enable_event_tracing(tracing_callback, INFO);
214+
#endif
215+
160216
char* table_path = argv[1];
161217
printf("Reading table at %s\n", table_path);
162218

0 commit comments

Comments
 (0)