-
Notifications
You must be signed in to change notification settings - Fork 46
capi: allow printing calls on stderr #388
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // SPDX-FileCopyrightText: 2022 Vector Informatik GmbH | ||
| // | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| #include "capi/CapiImpl.hpp" | ||
|
|
||
| #ifdef SILKIT_ENABLE_API_TRACING_INSTRUMENTATION | ||
|
|
||
| #include "util/StringHelpers.hpp" | ||
|
|
||
| #include <iostream> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <thread> | ||
|
|
||
| #include "fmt/format.h" | ||
| #include "fmt/ostream.h" | ||
|
|
||
| namespace VSilKit { | ||
|
|
||
| void ApiTraceEventImpl(const std::string_view func, const std::string_view data) | ||
| { | ||
| thread_local std::string message; | ||
|
|
||
| message.clear(); | ||
| message.append(R"({"thread":)"); | ||
| fmt::format_to(std::back_inserter(message), "{}", fmt::streamed(std::this_thread::get_id())); | ||
| message.append(R"(,"func":")"); | ||
| SilKit::Util::AppendEscapedJsonStringTo(func, message); | ||
| message.append(R"(","data":")"); | ||
| SilKit::Util::AppendEscapedJsonStringTo(data, message); | ||
| message.append(R"("})"); | ||
|
|
||
| std::cerr << message << '\n'; | ||
| } | ||
|
|
||
| } // namespace VSilKit | ||
|
|
||
| #endif | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,74 +10,68 @@ | |
|
|
||
| #include "capi/CapiExceptions.hpp" | ||
|
|
||
| #define CAPI_CATCH_EXCEPTIONS \ | ||
| catch (const SilKit::CapiBadParameterError& e) \ | ||
| { \ | ||
| SilKit_error_string = e.what(); \ | ||
| return SilKit_ReturnCode_BADPARAMETER; \ | ||
| } \ | ||
| catch (const SilKit::StateError& e) \ | ||
| { \ | ||
| SilKit_error_string = e.what(); \ | ||
| return SilKit_ReturnCode_WRONGSTATE; \ | ||
| } \ | ||
| catch (const SilKit::TypeConversionError& e) \ | ||
| { \ | ||
| SilKit_error_string = e.what(); \ | ||
| return SilKit_ReturnCode_TYPECONVERSIONERROR; \ | ||
| } \ | ||
| catch (const SilKit::ConfigurationError& e) \ | ||
| { \ | ||
| SilKit_error_string = e.what(); \ | ||
| return SilKit_ReturnCode_CONFIGURATIONERROR; \ | ||
| } \ | ||
| catch (const SilKit::ProtocolError& e) \ | ||
| { \ | ||
| SilKit_error_string = e.what(); \ | ||
| return SilKit_ReturnCode_PROTOCOLERROR; \ | ||
| } \ | ||
| catch (const SilKit::AssertionError& e) \ | ||
| { \ | ||
| SilKit_error_string = e.what(); \ | ||
| return SilKit_ReturnCode_ASSERTIONERROR; \ | ||
| } \ | ||
| catch (const SilKit::ExtensionError& e) \ | ||
| { \ | ||
| SilKit_error_string = e.what(); \ | ||
| return SilKit_ReturnCode_EXTENSIONERROR; \ | ||
| } \ | ||
| catch (const SilKit::LengthError& e) \ | ||
| { \ | ||
| SilKit_error_string = e.what(); \ | ||
| return SilKit_ReturnCode_LENGTHERROR; \ | ||
| } \ | ||
| catch (const SilKit::OutOfRangeError& e) \ | ||
| { \ | ||
| SilKit_error_string = e.what(); \ | ||
| return SilKit_ReturnCode_OUTOFRANGEERROR; \ | ||
| } \ | ||
| catch (const SilKit::LogicError& e) \ | ||
| { \ | ||
| SilKit_error_string = e.what(); \ | ||
| return SilKit_ReturnCode_LOGICERROR; \ | ||
| } \ | ||
| catch (const SilKit::SilKitError& e) \ | ||
| { \ | ||
| SilKit_error_string = e.what(); \ | ||
| return SilKit_ReturnCode_UNSPECIFIEDERROR; \ | ||
| } \ | ||
| catch (const std::runtime_error& e) \ | ||
| { \ | ||
| SilKit_error_string = e.what(); \ | ||
| return SilKit_ReturnCode_UNSPECIFIEDERROR; \ | ||
| } \ | ||
| catch (const std::exception& e) \ | ||
| #include <string_view> | ||
|
|
||
| #include "fmt/format.h" | ||
|
|
||
|
|
||
| #ifdef SILKIT_ENABLE_API_TRACING_INSTRUMENTATION | ||
|
|
||
| namespace VSilKit { | ||
|
|
||
| void ApiTraceEventImpl(std::string_view func, std::string_view data); | ||
|
|
||
| template <typename... Args> | ||
| void ApiTraceEvent(const std::string_view func, Args&&... args) | ||
| { | ||
| thread_local std::string data; | ||
|
|
||
| data.clear(); | ||
| fmt::format_to(std::back_inserter(data), std::forward<Args>(args)...); | ||
|
|
||
| ApiTraceEventImpl(func, data); | ||
| } | ||
|
|
||
| } // namespace VSilKit | ||
|
|
||
| #define VSILKIT_API_TRACE(...) ::VSilKit::ApiTraceEvent((__func__), __VA_ARGS__) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. func is an implementation defined string, so the traces are tied to the current OS/compiler.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| #else | ||
|
|
||
| #define VSILKIT_API_TRACE(...) \ | ||
| do \ | ||
| { \ | ||
| } while (false) | ||
|
|
||
| #endif | ||
|
|
||
|
|
||
| #define CAPI_CATCH_EXCEPTIONS_CATCH_BLOCK(ErrorType, ReturnCode) \ | ||
| catch (const ErrorType& e) \ | ||
| { \ | ||
| SilKit_error_string = e.what(); \ | ||
| return SilKit_ReturnCode_UNSPECIFIEDERROR; \ | ||
| } \ | ||
| VSILKIT_API_TRACE("ERROR {}", #ReturnCode); \ | ||
| return ReturnCode; \ | ||
| } | ||
|
|
||
|
|
||
| #define CAPI_CATCH_EXCEPTIONS \ | ||
| CAPI_CATCH_EXCEPTIONS_CATCH_BLOCK(SilKit::CapiBadParameterError, SilKit_ReturnCode_BADPARAMETER) \ | ||
| CAPI_CATCH_EXCEPTIONS_CATCH_BLOCK(SilKit::StateError, SilKit_ReturnCode_WRONGSTATE) \ | ||
| CAPI_CATCH_EXCEPTIONS_CATCH_BLOCK(SilKit::TypeConversionError, SilKit_ReturnCode_TYPECONVERSIONERROR) \ | ||
| CAPI_CATCH_EXCEPTIONS_CATCH_BLOCK(SilKit::ConfigurationError, SilKit_ReturnCode_CONFIGURATIONERROR) \ | ||
| CAPI_CATCH_EXCEPTIONS_CATCH_BLOCK(SilKit::ProtocolError, SilKit_ReturnCode_PROTOCOLERROR) \ | ||
| CAPI_CATCH_EXCEPTIONS_CATCH_BLOCK(SilKit::AssertionError, SilKit_ReturnCode_ASSERTIONERROR) \ | ||
| CAPI_CATCH_EXCEPTIONS_CATCH_BLOCK(SilKit::ExtensionError, SilKit_ReturnCode_EXTENSIONERROR) \ | ||
| CAPI_CATCH_EXCEPTIONS_CATCH_BLOCK(SilKit::LengthError, SilKit_ReturnCode_LENGTHERROR) \ | ||
| CAPI_CATCH_EXCEPTIONS_CATCH_BLOCK(SilKit::OutOfRangeError, SilKit_ReturnCode_OUTOFRANGEERROR) \ | ||
| CAPI_CATCH_EXCEPTIONS_CATCH_BLOCK(SilKit::LogicError, SilKit_ReturnCode_LOGICERROR) \ | ||
| CAPI_CATCH_EXCEPTIONS_CATCH_BLOCK(SilKit::SilKitError, SilKit_ReturnCode_UNSPECIFIEDERROR) \ | ||
| CAPI_CATCH_EXCEPTIONS_CATCH_BLOCK(std::runtime_error, SilKit_ReturnCode_UNSPECIFIEDERROR) \ | ||
| CAPI_CATCH_EXCEPTIONS_CATCH_BLOCK(std::exception, SilKit_ReturnCode_UNSPECIFIEDERROR) \ | ||
| catch (...) \ | ||
| { \ | ||
| VSILKIT_API_TRACE("ERROR SilKit_ReturnCode_UNSPECIFIEDERROR"); \ | ||
| return SilKit_ReturnCode_UNSPECIFIEDERROR; \ | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the 'textual' version of a
std::thread::idisn't neccessarily a number this could produce invalid JSON. Just keep that in mind.