获取更多动态,关注公众号:黑客下午茶

Native

The Sentry Native SDK is intended for C and C++. However, since it builds as a dynamic library and exposes C-bindings, it can be used by any language that supports interoperability with C, such as the Foreign Function Interface (FFI).

Sentry also offers higher-level SDKs for platforms with built-in support for native crashes:

On this page, we get you up and running with Sentry's SDK, so that it will automatically report errors and exceptions in your application.

We recommend using the Sentry Native SDK, which has built-in support for both Google Breakpad and Google Crashpad. However, if you would like to use Sentry with a third-party framework directly without using the Native SDK, see the following resources:

Install

Sentry captures data by using an SDK within your application’s runtime.

The Native SDK currently supports Windows, macOS, Linux, and Android.

To build the SDK, download the latest release of the SDK from the Releases page. The SDK is managed as a CMake project, which additionally supports several configuration options, such as the backend to use.

For example, CMake can be used like this (on macOS):

Copied
# configure the cmake build into the `build` directory, with crashpad (on macOS)
cmake -B build -D SENTRY_BACKEND=crashpad
# build the project
cmake --build build --parallel
# install the resulting artifacts into a specific prefix
cmake --install build --prefix install
# which will result in the following (on macOS):
exa --tree install
install
├── bin
│  └── crashpad_handler
├── include
│  └── sentry.h
└── lib
   ├── libsentry.dylib
   └── libsentry.dylib.dSYM

Configure

Configuration should happen as early as possible in your application's lifecycle.

Import and initialize the Sentry SDK early in your application setup:

Copied
#include <sentry.h>

int main(void) {
  sentry_options_t *options = sentry_options_new();
  sentry_options_set_dsn(options, "https://examplePublicKey@o0.ingest.sentry.io/0");
  sentry_options_set_release(options, "my-project-name@2.3.12");
  sentry_init(options);

  /* ... */

  // make sure everything flushes
  sentry_shutdown();
}

Alternatively, the DSN can be passed as SENTRY_DSN environment variable during runtime. This can be especially useful for server applications.

Verify

This snippet includes an intentional error, so you can test that everything is working as soon as you set it up:

The quickest way to verify Sentry in your Native application is by capturing a message:

Copied
sentry_capture_event(sentry_value_new_message_event(
  /*   level */ SENTRY_LEVEL_INFO,
  /*  logger */ "custom",
  /* message */ "It works!"
));

Or, by manually generating an event:

To capture an error or exception condition, create events containing an exception object. It needs to contain at least a value and type:

Copied
#include <sentry.h>

sentry_value_t exc = sentry_value_new_object();
sentry_value_set_by_key(exc, "type", sentry_value_new_string("Exception"));
sentry_value_set_by_key(exc, "value", sentry_value_new_string("Error message."));

sentry_value_t event = sentry_value_new_event();
sentry_value_set_by_key(event, "exception", exc);
sentry_capture_event(event);

This exception does not contain a stack trace, which must be added separately.

To view and resolve the recorded error, log into sentry.io and open your project. Clicking on the error's title will open a page where you can see detailed information and mark it as resolved.

You can edit this page on GitHub.