LCOV - code coverage report
Current view: top level - /home/ubuntu/zephyrproject/zephyr/subsys/logging - log_core.c (source / functions) Coverage Total Hit
Test: lcov.info Lines: 38.8 % 232 90
Test Date: 2026-11-06 14:43:19 Functions: 31.8 % 44 14
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 27.4 % 84 23

             Branch data     Line data    Source code
       1                 :             : /*
       2                 :             :  * Copyright (c) 2018 Nordic Semiconductor ASA
       3                 :             :  *
       4                 :             :  * SPDX-License-Identifier: Apache-2.0
       5                 :             :  */
       6                 :             : #include <time.h>
       7                 :             : #include <zephyr/logging/log.h>
       8                 :             : #include <zephyr/logging/log_backend.h>
       9                 :             : #include <zephyr/logging/log_ctrl.h>
      10                 :             : #include <zephyr/logging/log_output.h>
      11                 :             : #include <zephyr/logging/log_internal.h>
      12                 :             : #include <zephyr/sys/mpsc_pbuf.h>
      13                 :             : #include <zephyr/logging/log_link.h>
      14                 :             : #include <zephyr/sys/printk.h>
      15                 :             : #include <zephyr/sys/clock.h>
      16                 :             : #include <zephyr/sys/clock.h>
      17                 :             : #include <zephyr/init.h>
      18                 :             : #include <zephyr/sys/__assert.h>
      19                 :             : #include <zephyr/sys/atomic.h>
      20                 :             : #include <zephyr/sys/iterable_sections.h>
      21                 :             : #include <ctype.h>
      22                 :             : #include <zephyr/logging/log_frontend.h>
      23                 :             : #include <zephyr/internal/syscall_handler.h>
      24                 :             : #include <zephyr/logging/log_output_dict.h>
      25                 :             : #include <zephyr/logging/log_output_custom.h>
      26                 :             : #include <zephyr/linker/utils.h>
      27                 :             : #include <zephyr/arch/arch_interface.h>
      28                 :             : 
      29                 :             : #if CONFIG_USERSPACE && CONFIG_LOG_ALWAYS_RUNTIME
      30                 :             : #include <zephyr/app_memory/app_memdomain.h>
      31                 :             : K_APPMEM_PARTITION_DEFINE(k_log_partition);
      32                 :             : #endif
      33                 :             : 
      34                 :             : LOG_MODULE_REGISTER(log);
      35                 :             : 
      36                 :             : #ifndef CONFIG_LOG_PROCESS_THREAD_SLEEP_MS
      37                 :             : #define CONFIG_LOG_PROCESS_THREAD_SLEEP_MS 0
      38                 :             : #endif
      39                 :             : 
      40                 :             : #ifndef CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD
      41                 :             : #define CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD 0
      42                 :             : #endif
      43                 :             : 
      44                 :             : #ifndef CONFIG_LOG_PROCESS_THREAD_STACK_SIZE
      45                 :             : #define CONFIG_LOG_PROCESS_THREAD_STACK_SIZE 1
      46                 :             : #endif
      47                 :             : 
      48                 :             : #ifndef CONFIG_LOG_BLOCK_IN_THREAD_TIMEOUT_MS
      49                 :             : #define CONFIG_LOG_BLOCK_IN_THREAD_TIMEOUT_MS 0
      50                 :             : #endif
      51                 :             : 
      52                 :             : #ifndef CONFIG_LOG_PROCESSING_LATENCY_US
      53                 :             : #define CONFIG_LOG_PROCESSING_LATENCY_US 0
      54                 :             : #endif
      55                 :             : 
      56                 :             : #ifndef CONFIG_LOG_BUFFER_SIZE
      57                 :             : #define CONFIG_LOG_BUFFER_SIZE 4
      58                 :             : #endif
      59                 :             : 
      60                 :             : #ifdef CONFIG_LOG_PROCESS_THREAD_CUSTOM_PRIORITY
      61                 :             : #define LOG_PROCESS_THREAD_PRIORITY CONFIG_LOG_PROCESS_THREAD_PRIORITY
      62                 :             : #else
      63                 :             : #define LOG_PROCESS_THREAD_PRIORITY K_LOWEST_APPLICATION_THREAD_PRIO
      64                 :             : #endif
      65                 :             : 
      66                 :             : #ifndef CONFIG_LOG_TAG_MAX_LEN
      67                 :             : #define CONFIG_LOG_TAG_MAX_LEN 0
      68                 :             : #endif
      69                 :             : 
      70                 :             : #ifndef CONFIG_LOG_FAILURE_REPORT_PERIOD
      71                 :             : #define CONFIG_LOG_FAILURE_REPORT_PERIOD 0
      72                 :             : #endif
      73                 :             : 
      74                 :             : #define LOG_NO_CPU_OWNER (-1)
      75                 :             : 
      76                 :             : #ifndef CONFIG_LOG_ALWAYS_RUNTIME
      77                 :             : BUILD_ASSERT(!IS_ENABLED(CONFIG_NO_OPTIMIZATIONS),
      78                 :             :              "CONFIG_LOG_ALWAYS_RUNTIME must be enabled when "
      79                 :             :              "CONFIG_NO_OPTIMIZATIONS is set");
      80                 :             : BUILD_ASSERT(!IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE),
      81                 :             :              "CONFIG_LOG_ALWAYS_RUNTIME must be enabled when "
      82                 :             :              "CONFIG_LOG_MODE_IMMEDIATE is set");
      83                 :             : #endif
      84                 :             : 
      85                 :             : static const log_format_func_t format_table[] = {
      86                 :             :         [LOG_OUTPUT_TEXT] = IS_ENABLED(CONFIG_LOG_OUTPUT) ?
      87                 :             :                                                 log_output_msg_process : NULL,
      88                 :             :         [LOG_OUTPUT_SYST] = IS_ENABLED(CONFIG_LOG_MIPI_SYST_ENABLE) ?
      89                 :             :                                                 log_output_msg_syst_process : NULL,
      90                 :             :         [LOG_OUTPUT_DICT] = IS_ENABLED(CONFIG_LOG_DICTIONARY_SUPPORT) ?
      91                 :             :                                                 log_dict_output_msg_process : NULL,
      92                 :             :         [LOG_OUTPUT_CUSTOM] = IS_ENABLED(CONFIG_LOG_CUSTOM_FORMAT_SUPPORT) ?
      93                 :             :                                                 log_custom_output_msg_process : NULL,
      94                 :             : };
      95                 :             : 
      96                 :         209 : log_format_func_t log_format_func_t_get(uint32_t log_type)
      97                 :             : {
      98                 :         209 :         return format_table[log_type];
      99                 :             : }
     100                 :             : 
     101                 :           0 : size_t log_format_table_size(void)
     102                 :             : {
     103                 :           0 :         return ARRAY_SIZE(format_table);
     104                 :             : }
     105                 :             : 
     106                 :             : K_SEM_DEFINE(log_process_thread_sem, 0, 1);
     107                 :             : 
     108                 :             : static atomic_t initialized;
     109                 :             : static bool panic_mode;
     110                 :             : static atomic_t backend_attached;
     111                 :             : static atomic_t buffered_cnt;
     112                 :             : static atomic_t dropped_cnt;
     113                 :             : static k_tid_t proc_tid;
     114                 :             : static struct k_timer log_process_thread_timer;
     115                 :             : 
     116                 :             : static log_timestamp_t dummy_timestamp(void);
     117                 :             : static log_timestamp_get_t timestamp_func = dummy_timestamp;
     118                 :             : static uint32_t timestamp_freq;
     119                 :             : static log_timestamp_t proc_latency;
     120                 :             : static log_timestamp_t prev_timestamp;
     121                 :             : static atomic_t unordered_cnt;
     122                 :             : static uint64_t last_failure_report;
     123                 :             : static struct k_spinlock process_lock;
     124                 :             : static atomic_t process_lock_owner_cpu = ATOMIC_INIT(LOG_NO_CPU_OWNER);
     125                 :             : 
     126                 :             : static STRUCT_SECTION_ITERABLE(log_msg_ptr, log_msg_ptr);
     127                 :             : static STRUCT_SECTION_ITERABLE_ALTERNATE(log_mpsc_pbuf, mpsc_pbuf_buffer, log_buffer);
     128                 :             : static struct mpsc_pbuf_buffer *curr_log_buffer;
     129                 :             : 
     130                 :             : #ifdef CONFIG_MPSC_PBUF
     131                 :             : static uint32_t __aligned(Z_LOG_MSG_ALIGNMENT)
     132                 :             :         buf32[CONFIG_LOG_BUFFER_SIZE / sizeof(int)];
     133                 :             : 
     134                 :             : static void z_log_notify_drop(const struct mpsc_pbuf_buffer *buffer,
     135                 :             :                               const union mpsc_pbuf_generic *item);
     136                 :             : 
     137                 :             : static const struct mpsc_pbuf_buffer_config mpsc_config = {
     138                 :             :         .buf = (uint32_t *)buf32,
     139                 :             :         .size = ARRAY_SIZE(buf32),
     140                 :             :         .notify_drop = z_log_notify_drop,
     141                 :             :         .get_wlen = log_msg_generic_get_wlen,
     142                 :             :         .flags = (IS_ENABLED(CONFIG_LOG_MODE_OVERFLOW) ?
     143                 :             :                   MPSC_PBUF_MODE_OVERWRITE : 0) |
     144                 :             :                  (IS_ENABLED(CONFIG_LOG_MEM_UTILIZATION) ?
     145                 :             :                   MPSC_PBUF_MAX_UTILIZATION : 0)
     146                 :             : };
     147                 :             : #endif
     148                 :             : 
     149                 :             : /* Check that default tag can fit in tag buffer. */
     150                 :             : COND_CODE_0(CONFIG_LOG_TAG_MAX_LEN, (),
     151                 :             :         (BUILD_ASSERT(sizeof(CONFIG_LOG_TAG_DEFAULT) <= CONFIG_LOG_TAG_MAX_LEN + 1,
     152                 :             :                       "Default string longer than tag capacity")));
     153                 :             : 
     154                 :             : static char tag[CONFIG_LOG_TAG_MAX_LEN + 1] =
     155                 :             :         COND_CODE_0(CONFIG_LOG_TAG_MAX_LEN, ({}), (CONFIG_LOG_TAG_DEFAULT));
     156                 :             : 
     157                 :             : static void msg_process(union log_msg_generic *msg);
     158                 :             : 
     159                 :           0 : static log_timestamp_t dummy_timestamp(void)
     160                 :             : {
     161                 :           0 :         return 0;
     162                 :             : }
     163                 :             : 
     164                 :           0 : log_timestamp_t z_log_timestamp(void)
     165                 :             : {
     166                 :           0 :         return timestamp_func();
     167                 :             : }
     168                 :             : 
     169                 :             : static void z_log_msg_post_finalize(void)
     170                 :             : {
     171                 :             :         atomic_val_t cnt = atomic_inc(&buffered_cnt);
     172                 :             : 
     173                 :             :         if (panic_mode) {
     174                 :             :                 k_spinlock_key_t key = k_spin_lock(&process_lock);
     175                 :             :                 (void)log_process();
     176                 :             : 
     177                 :             :                 k_spin_unlock(&process_lock, key);
     178                 :             :         } else if (proc_tid != NULL) {
     179                 :             :                 /*
     180                 :             :                  * If CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD == 1,
     181                 :             :                  * timer is never needed. We release the processing
     182                 :             :                  * thread after every message is posted.
     183                 :             :                  */
     184                 :             :                 if (CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD == 1) {
     185                 :             :                         if (cnt == 0) {
     186                 :             :                                 k_sem_give(&log_process_thread_sem);
     187                 :             :                         }
     188                 :             :                 } else {
     189                 :             :                         if (cnt == 0) {
     190                 :             :                                 k_timer_start(&log_process_thread_timer,
     191                 :             :                                               K_MSEC(CONFIG_LOG_PROCESS_THREAD_SLEEP_MS),
     192                 :             :                                               K_NO_WAIT);
     193                 :             :                         } else if (CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD &&
     194                 :             :                                    (cnt + 1) == CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD) {
     195                 :             :                                 k_timer_stop(&log_process_thread_timer);
     196                 :             :                                 k_sem_give(&log_process_thread_sem);
     197                 :             :                         } else {
     198                 :             :                                 /* No action needed. Message processing will be triggered by the
     199                 :             :                                  * timeout or when number of upcoming messages exceeds the
     200                 :             :                                  * threshold.
     201                 :             :                                  */
     202                 :             :                         }
     203                 :             :                 }
     204                 :             :         }
     205                 :             : }
     206                 :             : 
     207                 :           0 : const struct log_backend *log_format_set_all_active_backends(size_t log_type)
     208                 :             : {
     209                 :           0 :         const struct log_backend *failed_backend = NULL;
     210                 :             : 
     211   [ #  #  #  # ]:           0 :         STRUCT_SECTION_FOREACH(log_backend, backend) {
     212         [ #  # ]:           0 :                 if (log_backend_is_active(backend)) {
     213                 :           0 :                         int retCode = log_backend_format_set(backend, log_type);
     214                 :             : 
     215         [ #  # ]:           0 :                         if (retCode != 0) {
     216                 :           0 :                                 failed_backend = backend;
     217                 :             :                         }
     218                 :             :                 }
     219                 :             :         }
     220                 :           0 :         return failed_backend;
     221                 :             : }
     222                 :             : 
     223                 :         209 : void z_log_vprintk(const char *fmt, va_list ap)
     224                 :             : {
     225                 :         209 :         if (!IS_ENABLED(CONFIG_LOG_PRINTK)) {
     226                 :             :                 return;
     227                 :             :         }
     228                 :             : 
     229                 :         209 :         z_log_msg_runtime_vcreate(Z_LOG_LOCAL_DOMAIN_ID, NULL,
     230                 :             :                                    LOG_LEVEL_INTERNAL_RAW_STRING, NULL, 0,
     231                 :             :                                    Z_LOG_MSG_CBPRINTF_FLAGS(0),
     232                 :             :                                    fmt, ap);
     233                 :             : }
     234                 :             : 
     235                 :             : #ifndef CONFIG_LOG_TIMESTAMP_USE_REALTIME
     236                 :         209 : static log_timestamp_t default_get_timestamp(void)
     237                 :             : {
     238                 :         209 :         return IS_ENABLED(CONFIG_LOG_TIMESTAMP_64BIT) ?
     239                 :         209 :                 sys_clock_tick_get() : k_cycle_get_32();
     240                 :             : }
     241                 :             : 
     242                 :             : static log_timestamp_t default_lf_get_timestamp(void)
     243                 :             : {
     244                 :             :         return IS_ENABLED(CONFIG_LOG_TIMESTAMP_64BIT) ?
     245                 :             :                 k_uptime_get() : k_uptime_get_32();
     246                 :             : }
     247                 :             : #else
     248                 :             : static log_timestamp_t default_rt_get_timestamp(void)
     249                 :             : {
     250                 :             :         struct timespec tspec;
     251                 :             : 
     252                 :             :         sys_clock_gettime(SYS_CLOCK_REALTIME, &tspec);
     253                 :             : 
     254                 :             :         return ((uint64_t)tspec.tv_sec * MSEC_PER_SEC) + (tspec.tv_nsec / NSEC_PER_MSEC);
     255                 :             : }
     256                 :             : #endif /* CONFIG_LOG_TIMESTAMP_USE_REALTIME */
     257                 :             : 
     258                 :           1 : void log_core_init(void)
     259                 :             : {
     260                 :           1 :         panic_mode = false;
     261                 :           1 :         dropped_cnt = 0;
     262                 :           1 :         buffered_cnt = 0;
     263                 :             : 
     264                 :           1 :         if (IS_ENABLED(CONFIG_LOG_FRONTEND)) {
     265                 :             :                 log_frontend_init();
     266                 :             : 
     267                 :             :                 if (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
     268                 :             :                         for (uint16_t s = 0; s < log_src_cnt_get(0); s++) {
     269                 :             :                                 log_frontend_filter_set(s, CONFIG_LOG_MAX_LEVEL);
     270                 :             :                         }
     271                 :             :                 }
     272                 :             : 
     273                 :             :                 if (IS_ENABLED(CONFIG_LOG_FRONTEND_ONLY)) {
     274                 :             :                         return;
     275                 :             :                 }
     276                 :             :         }
     277                 :             : 
     278                 :             :         /* Set default timestamp. */
     279                 :             : #ifdef CONFIG_LOG_TIMESTAMP_USE_REALTIME
     280                 :             :         log_set_timestamp_func(default_rt_get_timestamp, 1000U);
     281                 :             : #else
     282                 :           1 :         if (sys_clock_hw_cycles_per_sec() > 1000000) {
     283                 :             :                 log_set_timestamp_func(default_lf_get_timestamp, 1000U);
     284                 :             :         } else {
     285                 :           1 :                 uint32_t freq = IS_ENABLED(CONFIG_LOG_TIMESTAMP_64BIT) ?
     286                 :             :                         CONFIG_SYS_CLOCK_TICKS_PER_SEC : sys_clock_hw_cycles_per_sec();
     287                 :           1 :                 log_set_timestamp_func(default_get_timestamp, freq);
     288                 :             :         }
     289                 :             : #endif /* CONFIG_LOG_TIMESTAMP_USE_REALTIME */
     290                 :             : 
     291                 :           1 :         if (IS_ENABLED(CONFIG_LOG_MODE_DEFERRED)) {
     292                 :             :                 z_log_msg_init();
     293                 :             :         }
     294                 :             : 
     295                 :           1 :         if (IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
     296                 :             :                 z_log_runtime_filters_init();
     297                 :             :         }
     298                 :             : 
     299   [ -  +  +  + ]:           2 :         STRUCT_SECTION_FOREACH(log_backend, backend) {
     300                 :           1 :                 uint32_t id;
     301                 :             :                 /* As first slot in filtering mask is reserved, backend ID has offset.*/
     302                 :           1 :                 id = LOG_FILTER_FIRST_BACKEND_SLOT_IDX;
     303                 :           1 :                 id += backend - log_backend_get(0);
     304                 :           1 :                 log_backend_id_set(backend, id);
     305                 :             :         }
     306                 :             : }
     307                 :             : 
     308                 :           0 : static uint32_t activate_foreach_backend(uint32_t mask)
     309                 :             : {
     310                 :           0 :         uint32_t mask_cpy = mask;
     311                 :             : 
     312         [ #  # ]:           0 :         while (mask_cpy) {
     313                 :           0 :                 uint32_t i = __builtin_ctz(mask_cpy);
     314                 :           0 :                 const struct log_backend *backend = log_backend_get(i);
     315                 :             : 
     316                 :           0 :                 mask_cpy &= ~BIT(i);
     317   [ #  #  #  # ]:           0 :                 if (backend->autostart && (log_backend_is_ready(backend) == 0)) {
     318                 :           0 :                         uint32_t level = CONFIG_LOG_MAX_LEVEL;
     319                 :             : 
     320                 :             : #if defined(CONFIG_LOG_RUNTIME_FILTERING) && defined(CONFIG_LOG_RUNTIME_DEFAULT_LEVEL)
     321                 :             :                         level = CONFIG_LOG_RUNTIME_DEFAULT_LEVEL;
     322                 :             : #endif
     323                 :           0 :                         mask &= ~BIT(i);
     324                 :           0 :                         log_backend_enable(backend, backend->cb->ctx, level);
     325                 :             :                 }
     326                 :             :         }
     327                 :             : 
     328                 :           0 :         return mask;
     329                 :             : }
     330                 :             : 
     331                 :           2 : static uint32_t z_log_init(bool blocking, bool can_sleep)
     332                 :             : {
     333                 :           2 :         uint32_t mask = 0;
     334                 :             : 
     335                 :           2 :         if (IS_ENABLED(CONFIG_LOG_FRONTEND_ONLY)) {
     336                 :             :                 return 0;
     337                 :             :         }
     338                 :             : 
     339         [ -  + ]:           2 :         __ASSERT_NO_MSG(log_backend_count_get() <= LOG_FILTERS_MAX_BACKENDS);
     340                 :             : 
     341         [ +  + ]:           2 :         if (atomic_inc(&initialized) != 0) {
     342                 :             :                 return 0;
     343                 :             :         }
     344                 :             : 
     345                 :             :         if (IS_ENABLED(CONFIG_LOG_MULTIDOMAIN)) {
     346                 :             :                 z_log_links_initiate();
     347                 :             :         }
     348                 :             : 
     349                 :             :         int backend_index = 0;
     350                 :             : 
     351   [ -  +  +  + ]:           2 :         STRUCT_SECTION_FOREACH(log_backend, backend) {
     352                 :             :                 /* Activate autostart backends */
     353         [ -  + ]:           1 :                 if (backend->autostart) {
     354                 :           1 :                         uint32_t level = CONFIG_LOG_MAX_LEVEL;
     355                 :             : 
     356                 :             : #if defined(CONFIG_LOG_RUNTIME_FILTERING) && defined(CONFIG_LOG_RUNTIME_DEFAULT_LEVEL)
     357                 :             :                         level = CONFIG_LOG_RUNTIME_DEFAULT_LEVEL;
     358                 :             : #endif
     359                 :           1 :                         log_backend_init(backend);
     360                 :             : 
     361                 :             :                         /* If backend has activation function then backend is
     362                 :             :                          * not ready until activated.
     363                 :             :                          */
     364         [ +  - ]:           1 :                         if (log_backend_is_ready(backend) == 0) {
     365                 :           1 :                                 log_backend_enable(backend, backend->cb->ctx, level);
     366                 :             :                         } else {
     367                 :           0 :                                 mask |= BIT(backend_index);
     368                 :             :                         }
     369                 :             :                 }
     370                 :             : 
     371                 :           1 :                 ++backend_index;
     372                 :             :         }
     373                 :             : 
     374                 :             :         /* If blocking init, wait until all backends are activated. */
     375         [ -  + ]:           1 :         if (blocking) {
     376         [ #  # ]:           0 :                 while (mask) {
     377                 :           0 :                         mask = activate_foreach_backend(mask);
     378         [ #  # ]:           0 :                         if (IS_ENABLED(CONFIG_MULTITHREADING) && can_sleep) {
     379                 :           0 :                                 k_msleep(10);
     380                 :             :                         }
     381                 :             :                 }
     382                 :             :         }
     383                 :             : 
     384                 :             :         return mask;
     385                 :             : }
     386                 :             : 
     387                 :           0 : void log_init(void)
     388                 :             : {
     389                 :           0 :         (void)z_log_init(true, true);
     390                 :           0 : }
     391                 :             : 
     392                 :           0 : void log_thread_trigger(void)
     393                 :             : {
     394                 :           0 :         if (IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE)) {
     395                 :           0 :                 return;
     396                 :             :         }
     397                 :             : 
     398                 :             :         k_timer_stop(&log_process_thread_timer);
     399                 :             :         k_sem_give(&log_process_thread_sem);
     400                 :             : }
     401                 :             : 
     402                 :           0 : static void thread_set(k_tid_t process_tid)
     403                 :             : {
     404                 :           0 :         proc_tid = process_tid;
     405                 :             : 
     406                 :           0 :         if (IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE)) {
     407                 :           0 :                 return;
     408                 :             :         }
     409                 :             : 
     410                 :             :         if (CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD &&
     411                 :             :             process_tid &&
     412                 :             :             buffered_cnt >= CONFIG_LOG_PROCESS_TRIGGER_THRESHOLD) {
     413                 :             :                 k_sem_give(&log_process_thread_sem);
     414                 :             :         }
     415                 :             : }
     416                 :             : 
     417                 :           0 : void log_thread_set(k_tid_t process_tid)
     418                 :             : {
     419                 :           0 :         if (IS_ENABLED(CONFIG_LOG_PROCESS_THREAD)) {
     420                 :             :                 __ASSERT_NO_MSG(0);
     421                 :             :         } else {
     422                 :           0 :                 thread_set(process_tid);
     423                 :             :         }
     424                 :           0 : }
     425                 :             : 
     426                 :           1 : int log_set_timestamp_func(log_timestamp_get_t timestamp_getter, uint32_t freq)
     427                 :             : {
     428         [ +  - ]:           1 :         if (timestamp_getter == NULL) {
     429                 :             :                 return -EINVAL;
     430                 :             :         }
     431                 :             : 
     432                 :           1 :         timestamp_func = timestamp_getter;
     433                 :           1 :         timestamp_freq = freq;
     434                 :           1 :         if (CONFIG_LOG_PROCESSING_LATENCY_US) {
     435                 :             :                 proc_latency = (freq * CONFIG_LOG_PROCESSING_LATENCY_US) / 1000000;
     436                 :             :         }
     437                 :             : 
     438                 :           1 :         if (IS_ENABLED(CONFIG_LOG_OUTPUT)) {
     439                 :           1 :                 log_output_timestamp_freq_set(freq);
     440                 :             :         }
     441                 :             : 
     442                 :           1 :         return 0;
     443                 :             : }
     444                 :             : 
     445                 :           1 : void z_impl_log_panic(void)
     446                 :             : {
     447         [ +  - ]:           1 :         if (panic_mode) {
     448                 :             :                 return;
     449                 :             :         }
     450                 :             : 
     451                 :             :         /* If panic happened early logger might not be initialized.
     452                 :             :          * Forcing initialization of the logger and auto-starting backends.
     453                 :             :          */
     454                 :           1 :         (void)z_log_init(true, false);
     455                 :             : 
     456                 :           1 :         if (IS_ENABLED(CONFIG_LOG_FRONTEND)) {
     457                 :             :                 log_frontend_panic();
     458                 :             :                 if (IS_ENABLED(CONFIG_LOG_FRONTEND_ONLY)) {
     459                 :             :                         goto out;
     460                 :             :                 }
     461                 :             :         }
     462                 :             : 
     463   [ -  +  +  + ]:           2 :         STRUCT_SECTION_FOREACH(log_backend, backend) {
     464         [ +  - ]:           1 :                 if (log_backend_is_active(backend)) {
     465                 :           1 :                         log_backend_panic(backend);
     466                 :             :                 }
     467                 :             :         }
     468                 :             : 
     469                 :           1 :         if (!IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE)) {
     470                 :             :                 /* Flush */
     471                 :             :                 while (log_process() == true) {
     472                 :             :                 }
     473                 :             :         }
     474                 :             : 
     475                 :           1 : out:
     476                 :           1 :         panic_mode = true;
     477                 :             : }
     478                 :             : 
     479                 :             : #ifdef CONFIG_USERSPACE
     480                 :             : void z_vrfy_log_panic(void)
     481                 :             : {
     482                 :             :         z_impl_log_panic();
     483                 :             : }
     484                 :             : #include <zephyr/syscalls/log_panic_mrsh.c>
     485                 :             : #endif
     486                 :             : 
     487                 :         209 : static bool msg_filter_check(struct log_backend const *backend,
     488                 :             :                              union log_msg_generic *msg)
     489                 :             : {
     490                 :         209 :         if (!z_log_item_is_msg(msg)) {
     491                 :             :                 return true;
     492                 :             :         }
     493                 :             : 
     494                 :         209 :         if (!IS_ENABLED(CONFIG_LOG_RUNTIME_FILTERING)) {
     495                 :         209 :                 return true;
     496                 :             :         }
     497                 :             : 
     498                 :             :         uint32_t backend_level;
     499                 :             :         uint8_t level;
     500                 :             :         uint8_t domain_id;
     501                 :             :         int16_t source_id;
     502                 :             : 
     503                 :             :         level = log_msg_get_level(&msg->log);
     504                 :             :         domain_id = log_msg_get_domain(&msg->log);
     505                 :             :         source_id = log_msg_get_source_id(&msg->log);
     506                 :             : 
     507                 :             :         /* Accept all non-logging messages. */
     508                 :             :         if (level == LOG_LEVEL_NONE) {
     509                 :             :                 return true;
     510                 :             :         }
     511                 :             :         if (source_id >= 0) {
     512                 :             :                 backend_level = log_filter_get(backend, domain_id, source_id, true);
     513                 :             : 
     514                 :             :                 return (level <= backend_level);
     515                 :             :         } else {
     516                 :             :                 return true;
     517                 :             :         }
     518                 :             : }
     519                 :             : 
     520                 :         209 : static void msg_process(union log_msg_generic *msg)
     521                 :             : {
     522   [ -  +  +  + ]:         418 :         STRUCT_SECTION_FOREACH(log_backend, backend) {
     523         [ +  - ]:         209 :                 if (log_backend_is_active(backend) &&
     524         [ +  - ]:         209 :                     msg_filter_check(backend, msg)) {
     525                 :         209 :                         log_backend_msg_process(backend, msg);
     526                 :             :                 }
     527                 :             :         }
     528                 :         209 : }
     529                 :             : 
     530                 :           0 : void dropped_notify(void)
     531                 :             : {
     532                 :           0 :         uint32_t dropped = z_log_dropped_read_and_clear();
     533                 :             : 
     534   [ #  #  #  # ]:           0 :         STRUCT_SECTION_FOREACH(log_backend, backend) {
     535         [ #  # ]:           0 :                 if (log_backend_is_active(backend)) {
     536                 :           0 :                         log_backend_dropped(backend, dropped);
     537                 :             :                 }
     538                 :             :         }
     539                 :           0 : }
     540                 :             : 
     541                 :           0 : void unordered_notify(void)
     542                 :             : {
     543                 :           0 :         uint32_t unordered = atomic_set(&unordered_cnt, 0);
     544                 :             : 
     545                 :           0 :         LOG_WRN("%d unordered messages since last report", unordered);
     546                 :           0 : }
     547                 :             : 
     548                 :           1 : void z_log_notify_backend_enabled(void)
     549                 :             : {
     550                 :           1 :         atomic_val_t was_attached = atomic_set(&backend_attached, 1);
     551                 :             : 
     552                 :             :         /* Wakeup logger thread after attaching first backend. It might be
     553                 :             :          * blocked with log messages pending. Flag is set before giving the
     554                 :             :          * semaphore so that the woken up thread sees the backend as attached
     555                 :             :          * and does not skip processing and go back to sleep.
     556                 :             :          */
     557                 :           1 :         if (IS_ENABLED(CONFIG_LOG_PROCESS_THREAD) && was_attached == 0) {
     558                 :             :                 k_sem_give(&log_process_thread_sem);
     559                 :             :         }
     560                 :           1 : }
     561                 :             : 
     562                 :             : static inline bool z_log_unordered_pending(void)
     563                 :             : {
     564                 :             :         return IS_ENABLED(CONFIG_LOG_MULTIDOMAIN) && unordered_cnt;
     565                 :             : }
     566                 :             : 
     567                 :           0 : bool z_impl_log_process(void)
     568                 :             : {
     569                 :           0 :         if (!IS_ENABLED(CONFIG_LOG_MODE_DEFERRED)) {
     570                 :           0 :                 return false;
     571                 :             :         }
     572                 :             : 
     573                 :             :         k_timeout_t backoff = K_NO_WAIT;
     574                 :             :         union log_msg_generic *msg;
     575                 :             : 
     576                 :             :         if (atomic_get(&backend_attached) == 0) {
     577                 :             :                 return false;
     578                 :             :         }
     579                 :             : 
     580                 :             :         msg = z_log_msg_claim(&backoff);
     581                 :             : 
     582                 :             :         if (msg) {
     583                 :             :                 msg_process(msg);
     584                 :             :                 z_log_msg_free(msg);
     585                 :             :                 atomic_dec(&buffered_cnt);
     586                 :             :         } else if (CONFIG_LOG_PROCESSING_LATENCY_US > 0 && !K_TIMEOUT_EQ(backoff, K_NO_WAIT)) {
     587                 :             :                 /* If backoff is requested, it means that there are pending
     588                 :             :                  * messages but they are too new and processing shall back off
     589                 :             :                  * to allow arrival of newer messages from remote domains.
     590                 :             :                  */
     591                 :             :                 k_timer_start(&log_process_thread_timer, backoff, K_NO_WAIT);
     592                 :             : 
     593                 :             :                 return false;
     594                 :             :         }
     595                 :             : 
     596                 :             :         if (IS_ENABLED(CONFIG_LOG_MODE_DEFERRED)) {
     597                 :             :                 bool dropped_pend = z_log_dropped_pending();
     598                 :             :                 bool unordered_pend = z_log_unordered_pending();
     599                 :             : 
     600                 :             :                 if ((dropped_pend || unordered_pend) &&
     601                 :             :                    (k_uptime_get() - last_failure_report) > CONFIG_LOG_FAILURE_REPORT_PERIOD) {
     602                 :             :                         if (dropped_pend) {
     603                 :             :                                 dropped_notify();
     604                 :             :                         }
     605                 :             : 
     606                 :             :                         if (unordered_pend) {
     607                 :             :                                 unordered_notify();
     608                 :             :                         }
     609                 :             :                 }
     610                 :             : 
     611                 :             :                 last_failure_report += CONFIG_LOG_FAILURE_REPORT_PERIOD;
     612                 :             :         }
     613                 :             : 
     614                 :             :         return msg != NULL && z_log_msg_pending();
     615                 :             : }
     616                 :             : 
     617                 :             : #ifdef CONFIG_USERSPACE
     618                 :             : bool z_vrfy_log_process(void)
     619                 :             : {
     620                 :             :         return z_impl_log_process();
     621                 :             : }
     622                 :             : #include <zephyr/syscalls/log_process_mrsh.c>
     623                 :             : #endif
     624                 :             : 
     625                 :           0 : uint32_t z_impl_log_buffered_cnt(void)
     626                 :             : {
     627                 :           0 :         return buffered_cnt;
     628                 :             : }
     629                 :             : 
     630                 :             : #ifdef CONFIG_USERSPACE
     631                 :             : uint32_t z_vrfy_log_buffered_cnt(void)
     632                 :             : {
     633                 :             :         return z_impl_log_buffered_cnt();
     634                 :             : }
     635                 :             : #include <zephyr/syscalls/log_buffered_cnt_mrsh.c>
     636                 :             : #endif
     637                 :             : 
     638                 :           0 : void z_log_dropped(bool buffered)
     639                 :             : {
     640                 :           0 :         atomic_inc(&dropped_cnt);
     641         [ #  # ]:           0 :         if (buffered) {
     642                 :           0 :                 atomic_dec(&buffered_cnt);
     643                 :             :         }
     644                 :             : 
     645                 :           0 :         if (IS_ENABLED(CONFIG_LOG_PROCESS_THREAD)) {
     646                 :             :                 k_timer_stop(&log_process_thread_timer);
     647                 :             :                 k_sem_give(&log_process_thread_sem);
     648                 :             :         }
     649                 :           0 : }
     650                 :             : 
     651                 :           0 : uint32_t z_log_dropped_read_and_clear(void)
     652                 :             : {
     653                 :           0 :         return atomic_set(&dropped_cnt, 0);
     654                 :             : }
     655                 :             : 
     656                 :           0 : bool z_log_dropped_pending(void)
     657                 :             : {
     658                 :           0 :         return dropped_cnt > 0;
     659                 :             : }
     660                 :             : 
     661                 :           0 : void z_log_msg_init(void)
     662                 :             : {
     663                 :             : #ifdef CONFIG_MPSC_PBUF
     664                 :             :         mpsc_pbuf_init(&log_buffer, &mpsc_config);
     665                 :             :         curr_log_buffer = &log_buffer;
     666                 :             : #endif
     667                 :           0 : }
     668                 :             : 
     669                 :           0 : static struct log_msg *msg_alloc(struct mpsc_pbuf_buffer *buffer, uint32_t wlen)
     670                 :             : {
     671                 :           0 :         if (!IS_ENABLED(CONFIG_LOG_MODE_DEFERRED)) {
     672                 :           0 :                 return NULL;
     673                 :             :         }
     674                 :             : 
     675                 :             :         return (struct log_msg *)mpsc_pbuf_alloc(
     676                 :             :                 buffer, wlen,
     677                 :             :                 (CONFIG_LOG_BLOCK_IN_THREAD_TIMEOUT_MS == -1)
     678                 :             :                         ? K_FOREVER
     679                 :             :                         : K_MSEC(CONFIG_LOG_BLOCK_IN_THREAD_TIMEOUT_MS));
     680                 :             : }
     681                 :             : 
     682                 :             : static inline bool process_lock_required_for_clean_output(int owner_cpu, int curr_cpu)
     683                 :             : {
     684                 :             :         return owner_cpu != curr_cpu;
     685                 :             : }
     686                 :             : 
     687                 :         209 : static bool process_lock_acquire_if_needed(k_spinlock_key_t *key)
     688                 :             : {
     689                 :             : #ifdef CONFIG_SMP
     690                 :             :         int curr_cpu = arch_curr_cpu()->id;
     691                 :             : #else
     692                 :         209 :         int curr_cpu = 0;
     693                 :             : #endif
     694                 :         209 :         int owner_cpu = atomic_get(&process_lock_owner_cpu);
     695                 :             : 
     696                 :         209 :         if (!IS_ENABLED(CONFIG_LOG_IMMEDIATE_CLEAN_OUTPUT) ||
     697                 :             :             !process_lock_required_for_clean_output(owner_cpu, curr_cpu)) {
     698                 :         209 :                 return false;
     699                 :             :         }
     700                 :             : 
     701                 :             :         *key = k_spin_lock(&process_lock);
     702                 :             :         atomic_set(&process_lock_owner_cpu, curr_cpu);
     703                 :             : 
     704                 :             :         return true;
     705                 :             : }
     706                 :             : 
     707                 :             : static void process_lock_release_if_needed(bool lock_acquired, k_spinlock_key_t key)
     708                 :             : {
     709                 :             :         if (IS_ENABLED(CONFIG_LOG_IMMEDIATE_CLEAN_OUTPUT) && lock_acquired) {
     710                 :             :                 atomic_set(&process_lock_owner_cpu, LOG_NO_CPU_OWNER);
     711                 :             :                 k_spin_unlock(&process_lock, key);
     712                 :             :         }
     713                 :             : }
     714                 :             : 
     715                 :           0 : struct log_msg *z_log_msg_alloc(uint32_t wlen)
     716                 :             : {
     717                 :           0 :         return msg_alloc(&log_buffer, wlen);
     718                 :             : }
     719                 :             : 
     720                 :         209 : static void msg_commit(struct mpsc_pbuf_buffer *buffer, struct log_msg *msg)
     721                 :             : {
     722                 :         209 :         union log_msg_generic *m = (union log_msg_generic *)msg;
     723                 :         209 :         bool lock_acquired;
     724                 :             : 
     725                 :         209 :         if (IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE)) {
     726                 :         209 :                 k_spinlock_key_t key = {0};
     727                 :             : 
     728                 :         209 :                 lock_acquired = process_lock_acquire_if_needed(&key);
     729                 :             : 
     730                 :         209 :                 msg_process(m);
     731                 :             : 
     732                 :         209 :                 process_lock_release_if_needed(lock_acquired, key);
     733                 :             : 
     734                 :         209 :                 return;
     735                 :             :         }
     736                 :             : 
     737                 :             : #ifdef CONFIG_MPSC_PBUF
     738                 :             :         mpsc_pbuf_commit(buffer, &m->buf);
     739                 :             : #endif
     740                 :             :         z_log_msg_post_finalize();
     741                 :             : }
     742                 :             : 
     743                 :         209 : void z_log_msg_commit(struct log_msg *msg)
     744                 :             : {
     745                 :         209 :         msg->hdr.timestamp = timestamp_func();
     746                 :         209 :         msg_commit(&log_buffer, msg);
     747                 :         209 : }
     748                 :             : 
     749                 :           0 : union log_msg_generic *z_log_msg_local_claim(void)
     750                 :             : {
     751                 :             : #ifdef CONFIG_MPSC_PBUF
     752                 :             :         return (union log_msg_generic *)mpsc_pbuf_claim(&log_buffer);
     753                 :             : #else
     754                 :           0 :         return NULL;
     755                 :             : #endif
     756                 :             : 
     757                 :             : }
     758                 :             : 
     759                 :             : /* If there are buffers dedicated for each link, claim the oldest message (lowest timestamp). */
     760                 :           0 : union log_msg_generic *z_log_msg_claim_oldest(k_timeout_t *backoff)
     761                 :             : {
     762                 :           0 :         union log_msg_generic *msg = NULL;
     763                 :           0 :         struct log_msg_ptr *chosen = NULL;
     764                 :           0 :         log_timestamp_t t_min = sizeof(log_timestamp_t) > sizeof(uint32_t) ?
     765                 :             :                                 UINT64_MAX : UINT32_MAX;
     766                 :           0 :         int i = 0;
     767                 :             : 
     768                 :             :         /* Else iterate on all available buffers and get the oldest message. */
     769   [ #  #  #  # ]:           0 :         STRUCT_SECTION_FOREACH(log_msg_ptr, msg_ptr) {
     770                 :           0 :                 struct log_mpsc_pbuf *buf;
     771                 :             : 
     772                 :           0 :                 STRUCT_SECTION_GET(log_mpsc_pbuf, i, &buf);
     773                 :             : 
     774                 :             : #ifdef CONFIG_MPSC_PBUF
     775                 :             :                 if (msg_ptr->msg == NULL) {
     776                 :             :                         msg_ptr->msg = (union log_msg_generic *)mpsc_pbuf_claim(&buf->buf);
     777                 :             :                 }
     778                 :             : #endif
     779                 :             : 
     780         [ #  # ]:           0 :                 if (msg_ptr->msg) {
     781                 :           0 :                         log_timestamp_t t = log_msg_get_timestamp(&msg_ptr->msg->log);
     782                 :             : 
     783         [ #  # ]:           0 :                         if (t < t_min) {
     784                 :           0 :                                 t_min = t;
     785                 :           0 :                                 msg = msg_ptr->msg;
     786                 :           0 :                                 chosen = msg_ptr;
     787                 :           0 :                                 curr_log_buffer = &buf->buf;
     788                 :             :                         }
     789                 :             :                 }
     790                 :           0 :                 i++;
     791                 :             :         }
     792                 :             : 
     793         [ #  # ]:           0 :         if (msg) {
     794                 :           0 :                 if (CONFIG_LOG_PROCESSING_LATENCY_US > 0) {
     795                 :             :                         int32_t diff = t_min - (timestamp_func() - proc_latency);
     796                 :             : 
     797                 :             :                         if (diff > 0) {
     798                 :             :                                /* Entry is too new. Back off for sometime to allow new
     799                 :             :                                 * remote messages to arrive which may have been captured
     800                 :             :                                 * earlier (but on other platform). Calculate for how
     801                 :             :                                 * long processing shall back off.
     802                 :             :                                 */
     803                 :             :                                 if (timestamp_freq == sys_clock_hw_cycles_per_sec()) {
     804                 :             :                                         *backoff = K_CYC(diff);
     805                 :             :                                 } else {
     806                 :             :                                         *backoff = K_CYC((diff * sys_clock_hw_cycles_per_sec()) /
     807                 :             :                                                         timestamp_freq);
     808                 :             :                                 }
     809                 :             : 
     810                 :             :                                 return NULL;
     811                 :             :                         }
     812                 :             :                 }
     813                 :             : 
     814                 :           0 :                 (*chosen).msg = NULL;
     815                 :             :         }
     816                 :             : 
     817         [ #  # ]:           0 :         if (t_min < prev_timestamp) {
     818                 :           0 :                 atomic_inc(&unordered_cnt);
     819                 :             :         }
     820                 :             : 
     821                 :           0 :         prev_timestamp = t_min;
     822                 :             : 
     823                 :           0 :         return msg;
     824                 :             : }
     825                 :             : 
     826                 :           0 : union log_msg_generic *z_log_msg_claim(k_timeout_t *backoff)
     827                 :             : {
     828                 :           0 :         size_t len;
     829                 :             : 
     830                 :           0 :         STRUCT_SECTION_COUNT(log_mpsc_pbuf, &len);
     831                 :             : 
     832                 :             :         /* Use only one buffer if others are not registered. */
     833                 :           0 :         if (IS_ENABLED(CONFIG_LOG_MULTIDOMAIN) && len > 1) {
     834                 :             :                 return z_log_msg_claim_oldest(backoff);
     835                 :             :         }
     836                 :             : 
     837                 :           0 :         return z_log_msg_local_claim();
     838                 :             : }
     839                 :             : 
     840                 :             : static void msg_free(struct mpsc_pbuf_buffer *buffer, const union log_msg_generic *msg)
     841                 :             : {
     842                 :             : #ifdef CONFIG_MPSC_PBUF
     843                 :             :         mpsc_pbuf_free(buffer, &msg->buf);
     844                 :             : #endif
     845                 :             : }
     846                 :             : 
     847                 :           0 : void z_log_msg_free(union log_msg_generic *msg)
     848                 :             : {
     849                 :           0 :         msg_free(curr_log_buffer, msg);
     850                 :           0 : }
     851                 :             : 
     852                 :           0 : static bool msg_pending(struct mpsc_pbuf_buffer *buffer)
     853                 :             : {
     854                 :             : #ifdef CONFIG_MPSC_PBUF
     855                 :             :         return mpsc_pbuf_is_pending(buffer);
     856                 :             : #else
     857                 :           0 :         return false;
     858                 :             : #endif
     859                 :             : }
     860                 :             : 
     861                 :           0 : bool z_log_msg_pending(void)
     862                 :             : {
     863                 :           0 :         size_t len;
     864                 :           0 :         int i = 0;
     865                 :             : 
     866                 :           0 :         STRUCT_SECTION_COUNT(log_mpsc_pbuf, &len);
     867                 :             : 
     868                 :           0 :         if (!IS_ENABLED(CONFIG_LOG_MULTIDOMAIN) || (len == 1)) {
     869                 :           0 :                 return msg_pending(&log_buffer);
     870                 :             :         }
     871                 :             : 
     872                 :             :         STRUCT_SECTION_FOREACH(log_msg_ptr, msg_ptr) {
     873                 :             :                 struct log_mpsc_pbuf *buf;
     874                 :             : 
     875                 :             :                 if (msg_ptr->msg) {
     876                 :             :                         return true;
     877                 :             :                 }
     878                 :             : 
     879                 :             :                 STRUCT_SECTION_GET(log_mpsc_pbuf, i, &buf);
     880                 :             : 
     881                 :             :                 if (msg_pending(&buf->buf)) {
     882                 :             :                         return true;
     883                 :             :                 }
     884                 :             : 
     885                 :             :                 i++;
     886                 :             :         }
     887                 :             : 
     888                 :             :         return false;
     889                 :             : }
     890                 :             : 
     891                 :           0 : void z_log_msg_enqueue(const struct log_link *link, const void *data, size_t len)
     892                 :             : {
     893                 :           0 :         size_t wlen = DIV_ROUND_UP(ROUND_UP(len, Z_LOG_MSG_ALIGNMENT), sizeof(int));
     894         [ #  # ]:           0 :         struct mpsc_pbuf_buffer *mpsc_pbuffer = link->mpsc_pbuf ? link->mpsc_pbuf : &log_buffer;
     895                 :           0 :         struct log_msg *local_msg = msg_alloc(mpsc_pbuffer, wlen);
     896                 :             : 
     897         [ #  # ]:           0 :         if (!local_msg) {
     898                 :           0 :                 z_log_dropped(false);
     899                 :           0 :                 return;
     900                 :             :         }
     901                 :             : 
     902                 :           0 :         memcpy((void *)local_msg, data, len);
     903                 :           0 :         local_msg->hdr.desc.valid = 0;
     904                 :           0 :         local_msg->hdr.desc.busy = 0;
     905                 :           0 :         local_msg->hdr.desc.domain += link->ctrl_blk->domain_offset;
     906                 :           0 :         msg_commit(mpsc_pbuffer, local_msg);
     907                 :             : }
     908                 :             : 
     909                 :           0 : const char *z_log_get_tag(void)
     910                 :             : {
     911                 :           0 :         return CONFIG_LOG_TAG_MAX_LEN > 0 ? tag : NULL;
     912                 :             : }
     913                 :             : 
     914                 :           0 : int log_set_tag(const char *str)
     915                 :             : {
     916                 :             : #if CONFIG_LOG_TAG_MAX_LEN > 0
     917                 :             :         if (str == NULL) {
     918                 :             :                 return -EINVAL;
     919                 :             :         }
     920                 :             : 
     921                 :             :         size_t len = strlen(str);
     922                 :             :         size_t cpy_len = MIN(len, CONFIG_LOG_TAG_MAX_LEN);
     923                 :             : 
     924                 :             :         memcpy(tag, str, cpy_len);
     925                 :             :         tag[cpy_len] = '\0';
     926                 :             : 
     927                 :             :         if (cpy_len < len) {
     928                 :             :                 tag[cpy_len - 1] = '~';
     929                 :             :                 return -ENOMEM;
     930                 :             :         }
     931                 :             : 
     932                 :             :         return 0;
     933                 :             : #else
     934                 :           0 :         return -ENOTSUP;
     935                 :             : #endif
     936                 :             : }
     937                 :             : 
     938                 :           0 : int log_mem_get_usage(uint32_t *buf_size, uint32_t *usage)
     939                 :             : {
     940         [ #  # ]:           0 :         __ASSERT_NO_MSG(buf_size != NULL);
     941         [ #  # ]:           0 :         __ASSERT_NO_MSG(usage != NULL);
     942                 :             : 
     943                 :           0 :         if (!IS_ENABLED(CONFIG_LOG_MODE_DEFERRED)) {
     944                 :           0 :                 return -EINVAL;
     945                 :             :         }
     946                 :             : 
     947                 :             :         mpsc_pbuf_get_utilization(&log_buffer, buf_size, usage);
     948                 :             : 
     949                 :             :         return 0;
     950                 :             : }
     951                 :             : 
     952                 :           0 : int log_mem_get_max_usage(uint32_t *max)
     953                 :             : {
     954         [ #  # ]:           0 :         __ASSERT_NO_MSG(max != NULL);
     955                 :             : 
     956                 :           0 :         if (!IS_ENABLED(CONFIG_LOG_MODE_DEFERRED)) {
     957                 :           0 :                 return -EINVAL;
     958                 :             :         }
     959                 :             : 
     960                 :             :         return mpsc_pbuf_get_max_utilization(&log_buffer, max);
     961                 :             : }
     962                 :             : 
     963                 :             : static void log_backend_notify_all(enum log_backend_evt event,
     964                 :             :                                    union log_backend_evt_arg *arg)
     965                 :             : {
     966                 :             :         STRUCT_SECTION_FOREACH(log_backend, backend) {
     967                 :             :                 log_backend_notify(backend, event, arg);
     968                 :             :         }
     969                 :             : }
     970                 :             : 
     971                 :             : static void log_process_thread_timer_expiry_fn(struct k_timer *timer)
     972                 :             : {
     973                 :             :         k_sem_give(&log_process_thread_sem);
     974                 :             : }
     975                 :             : 
     976                 :             : static void log_process_thread_func(void *dummy1, void *dummy2, void *dummy3)
     977                 :             : {
     978                 :             :         __ASSERT_NO_MSG(log_backend_count_get() > 0);
     979                 :             :         uint32_t links_active_mask = 0xFFFFFFFF;
     980                 :             :         uint8_t domain_offset = 0;
     981                 :             :         uint32_t activate_mask = z_log_init(false, false);
     982                 :             :         k_timeout_t timeout;
     983                 :             : 
     984                 :             :         if (IS_ENABLED(CONFIG_LOG_MULTIDOMAIN) || (activate_mask != 0)) {
     985                 :             :                 /* When multidomain is enabled, start in periodic polling
     986                 :             :                  * mode to check for link readiness.
     987                 :             :                  * If some backends are not activated yet, set periodical thread wake up
     988                 :             :                  * to poll backends for readiness. Period is set arbitrarily.
     989                 :             :                  */
     990                 :             :                 timeout = K_MSEC(50);
     991                 :             :         } else {
     992                 :             :                 /* If all backends are ready, periodic wake up is not needed. */
     993                 :             :                 timeout = K_FOREVER;
     994                 :             :         }
     995                 :             :         bool processing = false;
     996                 :             :         thread_set(k_current_get());
     997                 :             : 
     998                 :             :         /* Logging thread is periodically waken up until all backends that
     999                 :             :          * should be autostarted are ready.
    1000                 :             :          */
    1001                 :             :         while (true) {
    1002                 :             :                 if (activate_mask) {
    1003                 :             :                         activate_mask = activate_foreach_backend(activate_mask);
    1004                 :             :                         if (!activate_mask && !IS_ENABLED(CONFIG_LOG_MULTIDOMAIN)) {
    1005                 :             :                                 /* Periodic wake up no longer needed since all
    1006                 :             :                                  * backends are ready.
    1007                 :             :                                  */
    1008                 :             :                                 timeout = K_FOREVER;
    1009                 :             :                         }
    1010                 :             :                 }
    1011                 :             : 
    1012                 :             :                 /* Keep trying to activate links until all links are active. */
    1013                 :             :                 if (IS_ENABLED(CONFIG_LOG_MULTIDOMAIN) && links_active_mask) {
    1014                 :             :                         links_active_mask =
    1015                 :             :                                 z_log_links_activate(links_active_mask, &domain_offset);
    1016                 :             :                         if (!links_active_mask && !activate_mask) {
    1017                 :             :                                 /* Periodic wake up no longer needed since all
    1018                 :             :                                  * backends and links are ready.
    1019                 :             :                                  */
    1020                 :             :                                 timeout = K_FOREVER;
    1021                 :             :                         }
    1022                 :             :                 }
    1023                 :             : 
    1024                 :             : 
    1025                 :             :                 if (!processing && z_log_msg_pending()) {
    1026                 :             :                         processing = true;
    1027                 :             :                         log_backend_notify_all(LOG_BACKEND_EVT_PROCESS_THREAD_START, NULL);
    1028                 :             :                 }
    1029                 :             : 
    1030                 :             :                 if (log_process() == false) {
    1031                 :             :                         if (processing) {
    1032                 :             :                                 processing = false;
    1033                 :             :                                 log_backend_notify_all(LOG_BACKEND_EVT_PROCESS_THREAD_DONE, NULL);
    1034                 :             :                         }
    1035                 :             :                         (void)k_sem_take(&log_process_thread_sem, timeout);
    1036                 :             :                 }
    1037                 :             :         }
    1038                 :             : }
    1039                 :             : 
    1040                 :             : K_KERNEL_STACK_DEFINE(logging_stack, CONFIG_LOG_PROCESS_THREAD_STACK_SIZE);
    1041                 :             : struct k_thread logging_thread;
    1042                 :             : 
    1043                 :           1 : static int enable_logger(void)
    1044                 :             : {
    1045                 :           1 :         if (IS_ENABLED(CONFIG_LOG_PROCESS_THREAD)) {
    1046                 :             :                 k_timer_init(&log_process_thread_timer,
    1047                 :             :                                 log_process_thread_timer_expiry_fn, NULL);
    1048                 :             :                 /* start logging thread */
    1049                 :             :                 k_thread_create(&logging_thread, logging_stack,
    1050                 :             :                                 K_KERNEL_STACK_SIZEOF(logging_stack),
    1051                 :             :                                 log_process_thread_func, NULL, NULL, NULL,
    1052                 :             :                                 LOG_PROCESS_THREAD_PRIORITY, 0,
    1053                 :             :                                 COND_CODE_1(CONFIG_LOG_PROCESS_THREAD,
    1054                 :             :                                         K_MSEC(CONFIG_LOG_PROCESS_THREAD_STARTUP_DELAY_MS),
    1055                 :             :                                         K_NO_WAIT));
    1056                 :             :                 k_thread_name_set(&logging_thread, "logging");
    1057                 :             :         } else {
    1058                 :           1 :                 (void)z_log_init(false, false);
    1059                 :             :         }
    1060                 :             : 
    1061                 :           1 :         return 0;
    1062                 :             : }
    1063                 :             : 
    1064                 :             : #ifdef CONFIG_LOG_MODE_DEFERRED
    1065                 :             : void log_flush(void)
    1066                 :             : {
    1067                 :             :         if (IS_ENABLED(CONFIG_LOG_PROCESS_THREAD)) {
    1068                 :             :                 while (atomic_get(&buffered_cnt)) {
    1069                 :             :                         log_thread_trigger();
    1070                 :             :                         k_sleep(K_USEC(CONFIG_LOG_FLUSH_SLEEP_US));
    1071                 :             :                 }
    1072                 :             :         } else {
    1073                 :             :                 while (LOG_PROCESS()) {
    1074                 :             :                 }
    1075                 :             :         }
    1076                 :             : }
    1077                 :             : #endif
    1078                 :             : 
    1079                 :             : SYS_INIT(enable_logger, POST_KERNEL, CONFIG_LOG_CORE_INIT_PRIORITY);
        

Generated by: LCOV version 2.0-1