-
-
Notifications
You must be signed in to change notification settings - Fork 200
feat: Support modifying attachments after init #433
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
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 |
|---|---|---|
|
|
@@ -13,6 +13,17 @@ | |
| * encoding, typically ANSI on Windows, UTF-8 macOS, and the locale encoding on | ||
| * Linux; and they provide wchar-compatible alternatives on Windows which are | ||
| * preferred. | ||
| * | ||
| * NOTE on attachments: | ||
| * | ||
| * Attachments are read lazily at the time of `sentry_capture_event` or at time | ||
| * of a hard crash. Relative attachment paths will be resolved according to the | ||
| * current working directory at the time of envelope creation. | ||
| * When adding and removing attachments, they are matched according to their | ||
| * given `path`. No normalization is performed. | ||
| * When using the `crashpad` backend, the list of attachments that will be added | ||
| * at the time of a hard crash will be frozen at the time of `sentry_init`, and | ||
| * later modifications will not be reflected. | ||
| */ | ||
|
|
||
| #ifndef SENTRY_H_INCLUDED | ||
|
|
@@ -821,6 +832,8 @@ SENTRY_API int sentry_options_get_symbolize_stacktraces( | |
| * `path` is assumed to be in platform-specific filesystem path encoding. | ||
| * API Users on windows are encouraged to use `sentry_options_add_attachmentw` | ||
| * instead. | ||
| * | ||
| * See the NOTE on attachments above for restrictions of this API. | ||
| */ | ||
| SENTRY_API void sentry_options_add_attachment( | ||
| sentry_options_t *opts, const char *path); | ||
|
|
@@ -1050,6 +1063,39 @@ SENTRY_API void sentry_remove_transaction(void); | |
| */ | ||
| SENTRY_API void sentry_set_level(sentry_level_t level); | ||
|
|
||
| /** | ||
| * Adds a new attachment to be sent along. | ||
| * | ||
| * `path` is assumed to be in platform-specific filesystem path encoding. | ||
| * API Users on windows are encouraged to use `sentry_add_attachmentw` instead. | ||
| * | ||
| * See the NOTE on attachments above for restrictions of this API. | ||
| */ | ||
| SENTRY_API void sentry_add_attachment(const char *path); | ||
|
Contributor
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. Will we always want this to be void? Does a status return make any sense now or in the future? |
||
|
|
||
| /** | ||
| * Removes a previously added attachment. | ||
| * | ||
| * `path` is assumed to be in platform-specific filesystem path encoding. | ||
| * API Users on windows are encouraged to use `sentry_remove_attachmentw` | ||
| * instead. | ||
| * | ||
| * See the NOTE on attachments above for restrictions of this API. | ||
| */ | ||
| SENTRY_API void sentry_remove_attachment(const char *path); | ||
|
Contributor
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. While an idempotent API is good, would a return from which you can tell whether anything happened or not be useful? |
||
|
|
||
| #ifdef SENTRY_PLATFORM_WINDOWS | ||
| /** | ||
| * Wide char version of `sentry_add_attachment`. | ||
| */ | ||
| SENTRY_API void sentry_add_attachmentw(const wchar_t *path); | ||
|
|
||
| /** | ||
| * Wide char version of `sentry_remove_attachment`. | ||
| */ | ||
| SENTRY_API void sentry_remove_attachmentw(const wchar_t *path); | ||
| #endif | ||
|
|
||
| /** | ||
| * Starts a new session. | ||
| */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| #include "sentry_attachment.h" | ||
| #include "sentry_alloc.h" | ||
| #include "sentry_envelope.h" | ||
| #include "sentry_options.h" | ||
| #include "sentry_path.h" | ||
| #include "sentry_value.h" | ||
|
|
||
| static void | ||
| sentry__attachment_free(sentry_attachment_t *attachment) | ||
| { | ||
| sentry__path_free(attachment->path); | ||
| sentry_free(attachment); | ||
| } | ||
|
|
||
| void | ||
| sentry__attachments_free(sentry_attachment_t *attachments) | ||
| { | ||
| sentry_attachment_t *next_attachment = attachments; | ||
| while (next_attachment) { | ||
| sentry_attachment_t *attachment = next_attachment; | ||
| next_attachment = attachment->next; | ||
|
|
||
| sentry__attachment_free(attachment); | ||
| } | ||
| } | ||
|
|
||
| void | ||
| sentry__attachment_add( | ||
| sentry_attachment_t **attachments_ptr, sentry_path_t *path) | ||
| { | ||
| if (!path) { | ||
| return; | ||
| } | ||
| sentry_attachment_t *attachment = SENTRY_MAKE(sentry_attachment_t); | ||
| if (!attachment) { | ||
| sentry__path_free(path); | ||
| return; | ||
| } | ||
| attachment->path = path; | ||
| attachment->next = NULL; | ||
|
|
||
| sentry_attachment_t **next_ptr = attachments_ptr; | ||
|
|
||
| for (sentry_attachment_t *last_attachment = *attachments_ptr; | ||
| last_attachment; last_attachment = last_attachment->next) { | ||
| if (sentry__path_eq(last_attachment->path, path)) { | ||
| sentry__attachment_free(attachment); | ||
| return; | ||
| } | ||
|
|
||
| next_ptr = &last_attachment->next; | ||
| } | ||
|
|
||
| *next_ptr = attachment; | ||
| } | ||
|
|
||
| void | ||
| sentry__attachment_remove( | ||
| sentry_attachment_t **attachments_ptr, sentry_path_t *path) | ||
| { | ||
| sentry_attachment_t **next_ptr = attachments_ptr; | ||
|
|
||
| for (sentry_attachment_t *attachment = *attachments_ptr; attachment; | ||
| attachment = attachment->next) { | ||
| if (sentry__path_eq(attachment->path, path)) { | ||
| *next_ptr = attachment->next; | ||
| sentry__attachment_free(attachment); | ||
| goto out; | ||
| } | ||
|
|
||
| next_ptr = &attachment->next; | ||
| } | ||
|
|
||
| out: | ||
| sentry__path_free(path); | ||
| } | ||
|
|
||
| /** | ||
| * Reads the attachments from disk and adds them to the `envelope`. | ||
| */ | ||
| void | ||
| sentry__apply_attachments_to_envelope( | ||
| sentry_envelope_t *envelope, const sentry_attachment_t *attachments) | ||
| { | ||
| if (!attachments) { | ||
| return; | ||
| } | ||
|
|
||
| SENTRY_TRACE("adding attachments to envelope"); | ||
| for (const sentry_attachment_t *attachment = attachments; attachment; | ||
| attachment = attachment->next) { | ||
| sentry_envelope_item_t *item = sentry__envelope_add_from_path( | ||
| envelope, attachment->path, "attachment"); | ||
| if (!item) { | ||
| continue; | ||
| } | ||
| sentry__envelope_item_set_header(item, "filename", | ||
| #ifdef SENTRY_PLATFORM_WINDOWS | ||
| sentry__value_new_string_from_wstr( | ||
| #else | ||
| sentry_value_new_string( | ||
| #endif | ||
| sentry__path_filename(attachment->path))); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #ifndef SENTRY_ATTACHMENT_H_INCLUDED | ||
| #define SENTRY_ATTACHMENT_H_INCLUDED | ||
|
|
||
| #include "sentry_boot.h" | ||
|
|
||
| typedef struct sentry_path_s sentry_path_t; | ||
| typedef struct sentry_options_s sentry_options_t; | ||
| typedef struct sentry_envelope_s sentry_envelope_t; | ||
|
|
||
| /** | ||
| * This is a linked list of all the attachments registered via | ||
| * `sentry_options_add_attachment`. | ||
| */ | ||
| typedef struct sentry_attachment_s sentry_attachment_t; | ||
| struct sentry_attachment_s { | ||
| sentry_path_t *path; | ||
| sentry_attachment_t *next; | ||
| }; | ||
|
|
||
| /** | ||
| * Frees the linked list of `attachments`. | ||
| */ | ||
| void sentry__attachments_free(sentry_attachment_t *attachments); | ||
|
|
||
| /** | ||
| * Adds an attachment to the attachments list at `attachments_ptr`. | ||
| */ | ||
| void sentry__attachment_add( | ||
| sentry_attachment_t **attachments_ptr, sentry_path_t *path); | ||
|
|
||
| /** | ||
| * Removes an attachment from the attachments list at `attachments_ptr`. | ||
| */ | ||
| void sentry__attachment_remove( | ||
| sentry_attachment_t **attachments_ptr, sentry_path_t *path); | ||
|
|
||
| /** | ||
| * Reads the attachments from disk and adds them to the `envelope`. | ||
| */ | ||
| void sentry__apply_attachments_to_envelope( | ||
| sentry_envelope_t *envelope, const sentry_attachment_t *attachments); | ||
|
|
||
| #endif |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -364,7 +364,9 @@ sentry_envelope_serialize(const sentry_envelope_t *envelope, size_t *size_out) | |
|
|
||
| sentry__envelope_serialize_into_stringbuilder(envelope, &sb); | ||
|
|
||
| *size_out = sentry__stringbuilder_len(&sb); | ||
| if (size_out) { | ||
|
Contributor
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. nice 😄 |
||
| *size_out = sentry__stringbuilder_len(&sb); | ||
| } | ||
| return sentry__stringbuilder_into_string(&sb); | ||
| } | ||
|
|
||
|
|
||
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.
Does it make sense to explicitly say something about the ownership of
*path? (same for remove attachment)