Skip to content

Commit c20c91a

Browse files
committed
logs take ownership of custom attributes
1 parent 1168604 commit c20c91a

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

examples/example.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,20 +524,23 @@ main(int argc, char **argv)
524524
sentry_value_new_object(), "no");
525525
// TODO add test that shows we still keep default attributes if
526526
// passed-in value is accidentally not an object
527+
// + (not) overwriting default attributes
527528
sentry_log_warn("logging with %s custom attributes",
528529
sentry_value_new_null(), "new_null as");
529530
sentry_value_t param_attributes = sentry_value_new_object();
530531
sentry_value_t param_attr = sentry_value_new_attribute(
531532
sentry_value_new_string("parameter"), NULL);
532533
sentry_value_set_by_key(
533534
param_attributes, "message.parameter.0", param_attr);
535+
// incref because we want to use it twice
536+
sentry_value_incref(param_attributes);
537+
sentry_value_incref(param_attributes);
534538
sentry_log_fatal(
535539
"logging with a custom parameter attributes", param_attributes);
536-
sentry_value_t param_attributes_2 = sentry_value_new_object();
537-
sentry_value_t param_attr_2 = sentry_value_new_attribute(
538-
sentry_value_new_string("parameter"), NULL);
539540
sentry_log_fatal("logging with a custom parameter %s attributes",
540541
param_attributes, "and format-string");
542+
sentry_log_warn("logging once again with a custom parameter attribute",
543+
param_attributes);
541544
}
542545

543546
if (has_arg(argc, argv, "attachment")) {

include/sentry.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2006,17 +2006,27 @@ SENTRY_EXPERIMENTAL_API int sentry_options_get_propagate_traceparent(
20062006

20072007
/**
20082008
* Enables or disables the structured logging feature.
2009-
* When disabled, all calls to sentry_logger_X() are no-ops.
2009+
* When disabled, all calls to `sentry_log_X()` are no-ops.
20102010
*/
20112011
SENTRY_EXPERIMENTAL_API void sentry_options_set_enable_logs(
20122012
sentry_options_t *opts, int enable_logs);
20132013
SENTRY_EXPERIMENTAL_API int sentry_options_get_enable_logs(
20142014
const sentry_options_t *opts);
20152015

2016+
/**
2017+
* Enables or disables custom attributes parsing for structured logging.
2018+
*
2019+
* When enabled, all `sentry_log_X()` functions expect a `sentry_value_t` object
2020+
* as the first variadic argument for custom log attributes. Remaining
2021+
* arguments are used for format string substitution.
2022+
*
2023+
* Disabled by default.
2024+
*/
20162025
SENTRY_EXPERIMENTAL_API void sentry_options_set_logs_with_attributes(
20172026
sentry_options_t *opts, int logs_with_attributes);
20182027
SENTRY_EXPERIMENTAL_API int sentry_options_get_logs_with_attributes(
20192028
const sentry_options_t *opts);
2029+
20202030
/**
20212031
* The potential returns of calling any of the sentry_log_X functions
20222032
* - Success means a log was enqueued
@@ -2054,6 +2064,15 @@ typedef enum {
20542064
*
20552065
* Flags, width, and precision specifiers are parsed but currently ignored for
20562066
* parameter extraction purposes.
2067+
*
2068+
* When the option `logs_with_attributes` is enabled, the first varg is parsed
2069+
* as a `sentry_value_t` object containing the initial attributes for the log.
2070+
* You can pass `sentry_value_new_null()` to logs which don't need attributes.
2071+
* TODO Default log attributes will (currently) overwrite passed-in attributes.
2072+
* Ownership of the attributes is transferred to the log function.
2073+
*
2074+
* To re-use the same attributes, call `sentry_value_incref` on it
2075+
* before passing the attributes to the log function.
20572076
*/
20582077
SENTRY_EXPERIMENTAL_API log_return_value_t sentry_log_trace(
20592078
const char *message, ...);

src/sentry_logs.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,11 +687,14 @@ construct_log(sentry_level_t level, const char *message, va_list args)
687687
va_end(args_copy);
688688
if (sentry_value_get_type(custom_attributes)
689689
== SENTRY_VALUE_TYPE_OBJECT) {
690-
SENTRY_DEBUG("Discarded custom attributes on log: non-object "
691-
"sentry_value_t passed in");
690+
// TODO do we want to inspect that the object is attribute-like?
692691
sentry_value_decref(attributes);
693692
attributes = sentry__value_clone(custom_attributes);
693+
} else {
694+
SENTRY_DEBUG("Discarded custom attributes on log: non-object "
695+
"sentry_value_t passed in");
694696
}
697+
sentry_value_decref(custom_attributes);
695698
}
696699

697700
// Format the message with remaining args (or all args if not using

0 commit comments

Comments
 (0)