1212#define O2_FRAMEWORK_SIGNPOST_H_
1313
1414#include < atomic>
15+ #include < array>
1516
1617struct o2_log_handle_t {
1718 char const * name = nullptr ;
1819 void * log = nullptr ;
1920 o2_log_handle_t * next = nullptr ;
2021};
2122
23+ // Helper function which replaces engineering types with a printf
24+ // compatible format string.
25+ template <auto N>
26+ constexpr auto remove_engineering_type (char const (&src)[N])
27+ {
28+ std::array<char , N> res = {};
29+ // do whatever string manipulation you want in res.
30+ char * t = res.data ();
31+ for (int i = 0 ; i < N; ++i) {
32+ if (src[i] == ' %' && src[i + 1 ] == ' {' ) {
33+ *t++ = src[i];
34+ while (src[i] != ' }' && src[i] != 0 ) {
35+ ++i;
36+ }
37+ if (src[i] == 0 ) {
38+ *t = 0 ;
39+ return res;
40+ }
41+ } else {
42+ *t++ = src[i];
43+ }
44+ }
45+ return res;
46+ }
47+
2248// Loggers registry is actually a feature available to all platforms
2349// We use this to register the loggers and to walk over them.
2450// So that also on mac we can have a list of all the registered loggers.
@@ -71,10 +97,10 @@ void* _o2_log_create(char const* name, char const* category);
7197#define O2_LOG_DEBUG (log, ...) os_log_debug(private_o2_log_##log, __VA_ARGS__)
7298#define O2_SIGNPOST_ID_FROM_POINTER (name, log, pointer ) os_signpost_id_t name = os_signpost_id_make_with_pointer(private_o2_log_##log, pointer)
7399#define O2_SIGNPOST_ID_GENERATE (name, log ) os_signpost_id_t name = os_signpost_id_generate(private_o2_log_##log)
74- # define O2_SIGNPOST_EVENT_EMIT (log, id, name, ...) os_signpost_event_emit(private_o2_log_##log, id, name, __VA_ARGS__)
75- #define O2_SIGNPOST_START (log, id, name, ...) os_signpost_interval_begin (private_o2_log_##log, id, name, __VA_ARGS__)
76- #define O2_SIGNPOST_END (log, id, name, ...) os_signpost_interval_end (private_o2_log_##log, id, name, __VA_ARGS__)
77- #define O2_ENG_TYPE ( x, what ) " %{xcode: " #x " } " what
100+ // FIXME: use __VA_OPT__ when available in C++20
101+ #define O2_SIGNPOST_EVENT_EMIT (log, id, name, format, ...) os_signpost_event_emit (private_o2_log_##log, id, name, format, ## __VA_ARGS__)
102+ #define O2_SIGNPOST_START (log, id, name, format, ...) os_signpost_interval_begin (private_o2_log_##log, id, name, format, ## __VA_ARGS__)
103+ #define O2_SIGNPOST_END (log, id, name, format, ...) os_signpost_interval_end(private_o2_log_##log, id, name, format, ##__VA_ARGS__)
78104
79105#ifdef O2_SIGNPOST_IMPLEMENTATION
80106// / We use a wrapper so that we can keep track of the logs.
@@ -465,10 +491,9 @@ void _o2_log_set_stacktrace(_o2_log_t* log, int stacktrace)
465491#define O2_LOG_DEBUG (log, ...) O2_LOG_MACRO (__VA_ARGS__)
466492#define O2_SIGNPOST_ID_FROM_POINTER (name, log, pointer ) _o2_signpost_id_t name = _o2_signpost_id_make_with_pointer(private_o2_log_##log, pointer)
467493#define O2_SIGNPOST_ID_GENERATE (name, log ) _o2_signpost_id_t name = _o2_signpost_id_generate_local(private_o2_log_##log)
468- #define O2_SIGNPOST_EVENT_EMIT (log, id, name, ...) _o2_signpost_event_emit(private_o2_log_##log, id, name, __VA_ARGS__)
469- #define O2_SIGNPOST_START (log, id, name, ...) _o2_signpost_interval_begin(private_o2_log_##log, id, name, __VA_ARGS__)
470- #define O2_SIGNPOST_END (log, id, name, ...) _o2_signpost_interval_end(private_o2_log_##log, id, name, __VA_ARGS__)
471- #define O2_ENG_TYPE (x, what ) " %" what
494+ #define O2_SIGNPOST_EVENT_EMIT (log, id, name, format, ...) _o2_signpost_event_emit(private_o2_log_##log, id, name, remove_engineering_type(format).data(), ##__VA_ARGS__)
495+ #define O2_SIGNPOST_START (log, id, name, format, ...) _o2_signpost_interval_begin(private_o2_log_##log, id, name, remove_engineering_type(format).data(), ##__VA_ARGS__)
496+ #define O2_SIGNPOST_END (log, id, name, format, ...) _o2_signpost_interval_end(private_o2_log_##log, id, name, remove_engineering_type(format).data(), ##__VA_ARGS__)
472497#else // This is the release implementation, it does nothing.
473498#define O2_DECLARE_DYNAMIC_LOG (x )
474499#define O2_DECLARE_DYNAMIC_STACKTRACE_LOG (x )
@@ -478,10 +503,9 @@ void _o2_log_set_stacktrace(_o2_log_t* log, int stacktrace)
478503#define O2_LOG_DEBUG (log, ...)
479504#define O2_SIGNPOST_ID_FROM_POINTER (name, log, pointer )
480505#define O2_SIGNPOST_ID_GENERATE (name, log )
481- #define O2_SIGNPOST_EVENT_EMIT (log, id, name, ...)
482- #define O2_SIGNPOST_START (log, id, name, ...)
483- #define O2_SIGNPOST_END (log, id, name, ...)
484- #define O2_ENG_TYPE (x )
506+ #define O2_SIGNPOST_EVENT_EMIT (log, id, name, format, ...)
507+ #define O2_SIGNPOST_START (log, id, name, format, ...)
508+ #define O2_SIGNPOST_END (log, id, name, format, ...)
485509#endif
486510
487511#endif // O2_FRAMEWORK_SIGNPOST_H_
0 commit comments