@@ -162,44 +162,59 @@ void HAP_debug_runtime(int level, const char *file, int line,
162162 const char * format , ...) {
163163 int len = 0 ;
164164 va_list argp ;
165- char * buf = NULL , * log = NULL ;
166-
167- /*
168- * Adding logs to persist buffer when level is set to
169- * RUNTIME_RPC_CRITICAL and fastrpc_log mask is disabled.
170- */
171- if (((1 << level ) & (fastrpc_logmask )) ||
172- ((level == HAP_LEVEL_RPC_CRITICAL ) && persist_buf .buf ) ||
173- log_userspace_file_fd != NULL ) {
174- buf = (char * )malloc (sizeof (char ) * MAX_FARF_LEN );
175- if (buf == NULL ) {
176- return ;
177- }
178- va_start (argp , format );
179- len = vsnprintf (buf , MAX_FARF_LEN , format , argp );
180- va_end (argp );
181- log = (char * )malloc (sizeof (char ) * MAX_FARF_LEN );
182- if (log == NULL ) {
183- return ;
184- }
185- snprintf (log , MAX_FARF_LEN , "%d:%d:%s:%s:%d: %s" , getpid (), gettid (),
186- __progname , file , line , buf );
165+ char * buf = NULL ;
166+ const uint32_t level_mask = (1 << level );
167+ const bool log_enabled = (level_mask & fastrpc_logmask );
168+ const bool is_critical = (level == HAP_LEVEL_RPC_CRITICAL );
169+ const bool need_persist = is_critical && persist_buf .buf ;
170+ const bool need_file_log = (log_userspace_file_fd != NULL );
171+
172+ /* Early return if no logging is needed */
173+ if (!log_enabled && !need_persist && !need_file_log ) {
174+ return ;
187175 }
188176
189- print_dbgbuf_data (log , len );
190- if (((1 << level ) & (fastrpc_logmask ))) {
191- if (log_userspace_file_fd != NULL ) {
192- fputs (log , log_userspace_file_fd );
193- fputs ("\n" , log_userspace_file_fd );
194- }
195- HAP_debug (buf , level , file , line );
177+ /* Allocate buffer only when needed */
178+ buf = (char * )malloc (sizeof (char ) * MAX_FARF_LEN );
179+ if (buf == NULL ) {
180+ return ;
196181 }
197- if (buf ) {
182+
183+ /* Format the message once */
184+ va_start (argp , format );
185+ len = vsnprintf (buf , MAX_FARF_LEN , format , argp );
186+ va_end (argp );
187+
188+ /* Validate length */
189+ if (len <= 0 || len >= MAX_FARF_LEN ) {
198190 free (buf );
191+ return ;
199192 }
200- if (log ) {
201- free (log );
193+
194+ /* Handle persist buffer for critical logs */
195+ if (need_persist || (log_enabled && IS_PERSIST_BUF_DATA (len , level ))) {
196+ print_dbgbuf_data (buf , len );
202197 }
198+
199+ /* Handle regular logging when enabled */
200+ if (log_enabled ) {
201+ /* File logging */
202+ if (need_file_log ) {
203+ char * filelog = (char * )malloc (sizeof (char ) * MAX_FARF_LEN );
204+ if (filelog ) {
205+ if (snprintf (filelog , MAX_FARF_LEN , "%d:%d:%s:%s:%d: %s" ,
206+ getpid (), gettid (), __progname , file , line , buf ) > 0 ) {
207+ fputs (filelog , log_userspace_file_fd );
208+ fputs ("\n" , log_userspace_file_fd );
209+ }
210+ free (filelog );
211+ }
212+ }
213+ /* Debug output */
214+ HAP_debug (buf , level , file , line );
215+ }
216+
217+ free (buf );
203218}
204219
205220#ifdef __LE_TVM__
@@ -279,28 +294,14 @@ void HAP_debug(const char *msg, int level, const char *filename, int line) {
279294}
280295
281296void fastrpc_log_init () {
282- bool debug_build_type = false;
283297 int nErr = AEE_SUCCESS , fd = -1 ;
284298 char build_type [PROPERTY_VALUE_MAX ];
285299 char * logfilename ;
286300
287301 pthread_mutex_init (& persist_buf .mut , 0 );
288302 pthread_mutex_lock (& persist_buf .mut );
289- /*
290- * Get build type by reading the target properties,
291- * if buuid type is eng or userdebug allocate 1 MB persist buf.
292- */
293- if (fastrpc_get_property_string (FASTRPC_BUILD_TYPE , build_type , NULL )) {
294- #if !defined(LE_ENABLE )
295- if (!strncmp (build_type , "eng" , PROPERTY_VALUE_MAX ) ||
296- !strncmp (build_type , "userdebug" , PROPERTY_VALUE_MAX ))
297- debug_build_type = true;
298- #else
299- if (atoi (build_type ))
300- debug_build_type = true;
301- #endif
302- }
303- if (persist_buf .buf == NULL && debug_build_type ) {
303+
304+ if (persist_buf .buf == NULL ) {
304305 /* Create a debug buffer to append DEBUG FARF level message. */
305306 persist_buf .buf = (char * )rpcmem_alloc_internal (
306307 RPCMEM_HEAP_ID_SYSTEM , RPCMEM_DEFAULT_FLAGS | RPCMEM_TRY_MAP_STATIC ,
0 commit comments