LCOV - code coverage report
Current view: top level - spdk/lib/log - log_flags.c (source / functions) Hit Total Coverage
Test: Combined Lines: 58 62 93.5 %
Date: 2024-07-12 13:53:39 Functions: 9 9 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 43 58 74.1 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2017 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/stdinc.h"
       7                 :            : 
       8                 :            : #include "spdk/log.h"
       9                 :            : 
      10                 :            : static TAILQ_HEAD(spdk_log_flag_head,
      11                 :            :                   spdk_log_flag) g_log_flags = TAILQ_HEAD_INITIALIZER(g_log_flags);
      12                 :            : 
      13                 :            : static struct spdk_log_flag *
      14                 :  101453352 : get_log_flag(const char *name)
      15                 :            : {
      16                 :            :         struct spdk_log_flag *flag;
      17                 :            : 
      18         [ +  + ]: 2900744969 :         TAILQ_FOREACH(flag, &g_log_flags, tailq) {
      19   [ +  +  -  +  : 2900567005 :                 if (strcasecmp(name, flag->name) == 0) {
                   +  + ]
      20                 :  101275318 :                         return flag;
      21                 :            :                 }
      22                 :            :         }
      23                 :            : 
      24                 :     178034 :         return NULL;
      25                 :            : }
      26                 :            : 
      27                 :            : void
      28                 :     178034 : spdk_log_register_flag(const char *name, struct spdk_log_flag *flag)
      29                 :            : {
      30                 :            :         struct spdk_log_flag *iter;
      31                 :            : 
      32   [ +  -  -  + ]:     178034 :         if (name == NULL || flag == NULL) {
      33                 :          0 :                 SPDK_ERRLOG("missing spdk_log_flag parameters\n");
      34                 :          0 :                 assert(false);
      35                 :            :                 return;
      36                 :            :         }
      37                 :            : 
      38         [ -  + ]:     178034 :         if (get_log_flag(name)) {
      39                 :          0 :                 SPDK_ERRLOG("duplicate spdk_log_flag '%s'\n", name);
      40                 :          0 :                 assert(false);
      41                 :            :                 return;
      42                 :            :         }
      43                 :            : 
      44         [ +  + ]:    2936395 :         TAILQ_FOREACH(iter, &g_log_flags, tailq) {
      45   [ +  +  -  +  :    2913732 :                 if (strcasecmp(iter->name, flag->name) > 0) {
                   +  + ]
      46                 :     155371 :                         TAILQ_INSERT_BEFORE(iter, flag, tailq);
      47                 :     155371 :                         return;
      48                 :            :                 }
      49                 :            :         }
      50                 :            : 
      51                 :      22663 :         TAILQ_INSERT_TAIL(&g_log_flags, flag, tailq);
      52                 :            : }
      53                 :            : 
      54                 :            : bool
      55                 :  101275318 : spdk_log_get_flag(const char *name)
      56                 :            : {
      57                 :  101275318 :         struct spdk_log_flag *flag = get_log_flag(name);
      58                 :            : 
      59   [ +  -  +  +  :  101275318 :         if (flag && flag->enabled) {
                   +  + ]
      60                 :        661 :                 return true;
      61                 :            :         }
      62                 :            : 
      63                 :  101274657 :         return false;
      64                 :            : }
      65                 :            : 
      66                 :            : static int
      67                 :        398 : log_set_flag(const char *name, bool value)
      68                 :            : {
      69                 :            :         struct spdk_log_flag *flag;
      70                 :        398 :         int rc = -EINVAL;
      71                 :            : 
      72   [ -  +  +  + ]:        398 :         if (strcasecmp(name, "all") == 0) {
      73         [ +  + ]:        118 :                 TAILQ_FOREACH(flag, &g_log_flags, tailq) {
      74                 :        110 :                         flag->enabled = value;
      75                 :            :                 }
      76                 :          8 :                 return 0;
      77                 :            :         }
      78                 :            : 
      79         [ +  + ]:      25037 :         TAILQ_FOREACH(flag, &g_log_flags, tailq) {
      80         [ +  + ]:      24647 :                 if (fnmatch(name, flag->name, FNM_CASEFOLD) == 0) {
      81                 :        390 :                         flag->enabled = value;
      82                 :        390 :                         rc = 0;
      83                 :            :                 }
      84                 :            :         }
      85                 :            : 
      86                 :        390 :         return rc;
      87                 :            : }
      88                 :            : 
      89                 :            : int
      90                 :        392 : spdk_log_set_flag(const char *name)
      91                 :            : {
      92                 :        392 :         return log_set_flag(name, true);
      93                 :            : }
      94                 :            : 
      95                 :            : int
      96                 :          6 : spdk_log_clear_flag(const char *name)
      97                 :            : {
      98                 :          6 :         return log_set_flag(name, false);
      99                 :            : }
     100                 :            : 
     101                 :            : struct spdk_log_flag *
     102                 :          6 : spdk_log_get_first_flag(void)
     103                 :            : {
     104                 :          6 :         return TAILQ_FIRST(&g_log_flags);
     105                 :            : }
     106                 :            : 
     107                 :            : struct spdk_log_flag *
     108                 :        402 : spdk_log_get_next_flag(struct spdk_log_flag *flag)
     109                 :            : {
     110                 :        402 :         return TAILQ_NEXT(flag, tailq);
     111                 :            : }
     112                 :            : 
     113                 :            : void
     114                 :         41 : spdk_log_usage(FILE *f, const char *log_arg)
     115                 :            : {
     116                 :            : #define LINE_PREFIX                     "                           "
     117                 :            : #define ENTRY_SEPARATOR                 ", "
     118                 :            : #define MAX_LINE_LENGTH                 100
     119                 :         41 :         uint64_t prefix_len = strlen(LINE_PREFIX);
     120                 :         41 :         uint64_t separator_len = strlen(ENTRY_SEPARATOR);
     121                 :         41 :         const char *first_entry = "--logflag <flag>      enable log flag (all, ";
     122                 :            :         uint64_t curr_line_len;
     123                 :            :         uint64_t curr_entry_len;
     124                 :            :         struct spdk_log_flag *flag;
     125                 :         41 :         char first_line[MAX_LINE_LENGTH] = {};
     126                 :            : 
     127                 :         41 :         snprintf(first_line, sizeof(first_line), " %s, %s", log_arg, first_entry);
     128         [ -  + ]:         41 :         fprintf(f, "%s", first_line);
     129                 :         41 :         curr_line_len = strlen(first_line);
     130                 :            : 
     131         [ +  - ]:        961 :         TAILQ_FOREACH(flag, &g_log_flags, tailq) {
     132         [ -  + ]:        961 :                 curr_entry_len = strlen(flag->name);
     133         [ +  + ]:        961 :                 if ((curr_line_len + curr_entry_len + separator_len) > MAX_LINE_LENGTH) {
     134         [ -  + ]:        133 :                         fprintf(f, "\n%s", LINE_PREFIX);
     135                 :        133 :                         curr_line_len = prefix_len;
     136                 :            :                 }
     137                 :            : 
     138   [ -  +  -  + ]:        961 :                 fprintf(f, "%s", flag->name);
     139                 :        961 :                 curr_line_len += curr_entry_len;
     140                 :            : 
     141         [ +  + ]:        961 :                 if (TAILQ_LAST(&g_log_flags, spdk_log_flag_head) == flag) {
     142                 :         41 :                         break;
     143                 :            :                 }
     144                 :            : 
     145         [ -  + ]:        920 :                 fprintf(f, "%s", ENTRY_SEPARATOR);
     146                 :        920 :                 curr_line_len += separator_len;
     147                 :            :         }
     148                 :            : 
     149         [ -  + ]:         41 :         fprintf(f, ")\n");
     150                 :         41 : }

Generated by: LCOV version 1.14