Skip to content

Commit cb2207f

Browse files
committed
feat(observe api): seperate c++ bindings from c bindings
1 parent 45a56f5 commit cb2207f

File tree

8 files changed

+69
-14
lines changed

8 files changed

+69
-14
lines changed

observe-api/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ build:
66
@cp test/rust/target/wasm32-wasi/debug/rust_guest.wasm test/rust_guest.wasm
77
$(WASICC) -o test/c_guest.wasm -I c test/c/main.c
88
$(WASICXX) -o test/cxx_guest.wasm -I c -x c++ test/c/main.c
9-
$(WASICXX) -o test/cxx_guest_2.wasm -fno-exceptions -I c test/c/main.cpp
10-
$(WASICXX) -o test/cxx_guest_3.wasm -fno-exceptions -I c test/c/main2.cpp
9+
$(WASICXX) -o test/cxx_guest_2.wasm -fno-exceptions -I cxx test/cxx/main.cpp
10+
$(WASICXX) -o test/cxx_guest_3.wasm -fno-exceptions -I cxx test/cxx/main2.cpp

observe-api/README.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ We currently provide these language bindings to this API:
2222
* [example](test/rust/src/main.rs)
2323

2424

25-
### [c and c++](c/)
25+
### [c](c/) and [c++](cxx/)
2626

27-
Both the C and C++ bindings are implemented as header-only libraries. To use the C bindings,
27+
Both the C and C++ bindings are implemented as single header libraries. To use the C bindings,
2828
in __ONE__ source file:
2929

3030
```c
@@ -39,17 +39,16 @@ In other source files, just `#include "observe_api.h"`
3939
To use the C++ bindings, instead, in __ONE__ source file:
4040

4141
```c++
42-
#define OBSERVE_API_IMPLEMENTATION
4342
#define OBSERVE_API_CPP_IMPLEMENTATION
4443
#include "observe_api.hpp"
4544
```
4645

4746
In other source files, just `#include "observe_api.hpp"`
4847

49-
__NOTE:__ `observe_api.hpp` `#include`s `observe_api.h`
48+
* [functional example](test/cxx/main.cpp)
49+
* [OO example](test/cxx/main2.cpp)
5050

51-
* [functional example](test/c/main.cpp)
52-
* [OO example](test/c/main2.cpp)
51+
In C++, both bindings may be used at the same time without conflict.
5352

5453
### Other
5554

observe-api/c/observe_api.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
#include <stdint.h>
55

6-
#define IMPORT(a, b) __attribute__((import_module(a), import_name(b)))
7-
6+
#ifndef OBSERVE_API_ENUM
7+
#define OBSERVE_API_ENUM
88
enum DO_LOG_LEVEL {
99
DO_LL_ERROR = 1,
1010
DO_LL_WARN = 2,
@@ -14,6 +14,9 @@ enum DO_LOG_LEVEL {
1414
};
1515

1616
enum DO_METRIC_FMT { DO_MF_STATSD = 1 };
17+
#endif
18+
19+
#define IMPORT(a, b) __attribute__((import_module(a), import_name(b)))
1720

1821
IMPORT("dylibso:observe/api", "metric")
1922
extern void observe_api_metric_n(enum DO_METRIC_FMT format, const char *metric,
@@ -28,6 +31,8 @@ extern void observe_api_span_exit(void);
2831
IMPORT("dylibso:observe/api", "span-tags")
2932
extern void observe_api_span_tags_n(const char *tags, size_t tags_length);
3033

34+
#undef IMPORT
35+
3136
#ifdef __cplusplus
3237
extern "C" {
3338
#endif

observe-api/c/observe_api.hpp renamed to observe-api/cxx/observe_api.hpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
#ifndef OBSERVE_API_HPP
22
#define OBSERVE_API_HPP
33

4-
#include "observe_api.h"
54
#include <string>
65
#include <string_view>
76
#include <vector>
87

8+
#ifndef OBSERVE_API_ENUM
9+
#define OBSERVE_API_ENUM
10+
enum DO_LOG_LEVEL {
11+
DO_LL_ERROR = 1,
12+
DO_LL_WARN = 2,
13+
DO_LL_INFO = 3,
14+
DO_LL_DEBUG = 4,
15+
DO_LL_TRACE = 5
16+
};
17+
18+
enum DO_METRIC_FMT { DO_MF_STATSD = 1 };
19+
#endif
20+
921
namespace observe_api {
1022
void span_enter(std::string_view name);
1123
void span_exit();
@@ -40,13 +52,29 @@ class Span {
4052
#ifndef OBSERVE_API_CPP
4153
#define OBSERVE_API_CPP
4254

43-
#include "observe_api.h"
4455
#include <iterator>
4556
#include <sstream>
4657
#include <string>
4758
#include <string_view>
4859
#include <vector>
4960

61+
#define IMPORT(a, b) __attribute__((import_module(a), import_name(b)))
62+
63+
IMPORT("dylibso:observe/api", "metric")
64+
extern void observe_api_metric_n(enum DO_METRIC_FMT format, const char *metric,
65+
size_t metric_length);
66+
IMPORT("dylibso:observe/api", "log")
67+
extern void observe_api_log_n(enum DO_LOG_LEVEL level, const char *message,
68+
size_t message_length);
69+
IMPORT("dylibso:observe/api", "span-enter")
70+
extern void observe_api_span_enter_n(const char *name, size_t name_length);
71+
IMPORT("dylibso:observe/api", "span-exit")
72+
extern void observe_api_span_exit(void);
73+
IMPORT("dylibso:observe/api", "span-tags")
74+
extern void observe_api_span_tags_n(const char *tags, size_t tags_length);
75+
76+
#undef IMPORT
77+
5078
namespace observe_api {
5179

5280
void span_enter(std::string_view name) {
@@ -68,7 +96,7 @@ void span_tags(std::string_view tags) {
6896
}
6997

7098
void statsd(std::string_view mtc) {
71-
observe_api_statsd_n(mtc.data(), mtc.size());
99+
observe_api_metric_n(DO_MF_STATSD, mtc.data(), mtc.size());
72100
}
73101

74102
void span_tags(std::vector<std::string> &tags) {

observe-api/test/cxx/main.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#define OBSERVE_API_CPP_IMPLEMENTATION
2+
#include "observe_api.hpp"
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string>
6+
#include <vector>
7+
8+
void run() {
9+
observe_api::span_enter("printf");
10+
observe_api::statsd("ok:aaaaa");
11+
observe_api::log(DO_LL_INFO, "bbbbb");
12+
observe_api::span_tags("abbc:def,(another:tag");
13+
std::vector<std::string> tags = {"taga:one", "tagb:two"};
14+
observe_api::span_tags(tags);
15+
printf("Hello from Wasm!\n");
16+
observe_api::span_exit();
17+
}
18+
19+
int main(int argc, char *argv[]) {
20+
observe_api::span_enter("run");
21+
run();
22+
observe_api::span_exit();
23+
return 0;
24+
}

observe-api/test/c/main2.cpp renamed to observe-api/test/cxx/main2.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#define OBSERVE_API_IMPLEMENTATION
21
#define OBSERVE_API_CPP_IMPLEMENTATION
32
#include "observe_api.hpp"
43
#include <stdio.h>

observe-api/test/cxx_guest_2.wasm

-114 Bytes
Binary file not shown.

observe-api/test/cxx_guest_3.wasm

-114 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)