@@ -678,47 +678,62 @@ construct_log(sentry_level_t level, const char *message, va_list args)
678678 sentry_value_t attributes = sentry_value_new_object ();
679679
680680 SENTRY_WITH_OPTIONS (options ) {
681+ // Extract custom attributes if the option is enabled
681682 if (sentry_options_get_logs_with_attributes (options )) {
682- sentry_value_set_by_key (
683- log , "body" , sentry_value_new_string (message ));
684683 va_list args_copy ;
685684 va_copy (args_copy , args );
686685 sentry_value_t custom_attributes
687686 = va_arg (args_copy , sentry_value_t );
688687 va_end (args_copy );
689- // TODO find out if this is legal
690- sentry_value_decref (attributes );
691- attributes = custom_attributes ;
692- } else {
693- va_list args_copy_1 , args_copy_2 , args_copy_3 ;
694- va_copy (args_copy_1 , args );
695- va_copy (args_copy_2 , args );
696- va_copy (args_copy_3 , args );
697- int len = vsnprintf (NULL , 0 , message , args_copy_1 ) + 1 ;
698- va_end (args_copy_1 );
699- size_t size = (size_t )len ;
700- char * fmt_message = sentry_malloc (size );
701- if (!fmt_message ) {
702- va_end (args_copy_2 );
703- va_end (args_copy_3 );
704- return sentry_value_new_null ();
688+ // TODO is this enough to check whether this is a valid
689+ // sentry_value_t object?
690+ if (sentry_value_get_type (custom_attributes )
691+ == SENTRY_VALUE_TYPE_OBJECT ) {
692+ SENTRY_DEBUG ("Discarded custom attributes on log: non-object "
693+ "sentry_value_t passed in" );
694+ sentry_value_decref (attributes );
695+ attributes = custom_attributes ;
705696 }
697+ }
706698
707- vsnprintf (fmt_message , size , message , args_copy_2 );
708- va_end (args_copy_2 );
699+ // Format the message with remaining args (or all args if not using
700+ // custom attributes)
701+ va_list args_copy_1 , args_copy_2 , args_copy_3 ;
702+ va_copy (args_copy_1 , args );
703+ va_copy (args_copy_2 , args );
704+ va_copy (args_copy_3 , args );
709705
710- sentry_value_set_by_key (
711- log , "body" , sentry_value_new_string (fmt_message ));
712- sentry_free (fmt_message );
713-
714- // Parse variadic arguments and add them to attributes
715- if (populate_message_parameters (attributes , message , args_copy_3 )) {
716- // only add message template if we have parameters
717- add_attribute (attributes , sentry_value_new_string (message ),
718- "string" , "sentry.message.template" );
719- }
706+ // Skip the first argument (attributes) if using custom attributes
707+ if (sentry_options_get_logs_with_attributes (options )) {
708+ va_arg (args_copy_1 , sentry_value_t );
709+ va_arg (args_copy_2 , sentry_value_t );
710+ va_arg (args_copy_3 , sentry_value_t );
711+ }
712+
713+ int len = vsnprintf (NULL , 0 , message , args_copy_1 ) + 1 ;
714+ va_end (args_copy_1 );
715+ size_t size = (size_t )len ;
716+ char * fmt_message = sentry_malloc (size );
717+ if (!fmt_message ) {
718+ va_end (args_copy_2 );
720719 va_end (args_copy_3 );
720+ return sentry_value_new_null ();
721+ }
722+
723+ vsnprintf (fmt_message , size , message , args_copy_2 );
724+ va_end (args_copy_2 );
725+
726+ sentry_value_set_by_key (
727+ log , "body" , sentry_value_new_string (fmt_message ));
728+ sentry_free (fmt_message );
729+
730+ // Parse variadic arguments and add them to attributes
731+ if (populate_message_parameters (attributes , message , args_copy_3 )) {
732+ // only add message template if we have parameters
733+ add_attribute (attributes , sentry_value_new_string (message ),
734+ "string" , "sentry.message.template" );
721735 }
736+ va_end (args_copy_3 );
722737 }
723738
724739 sentry_value_set_by_key (
0 commit comments