LCOV - code coverage report
Current view: top level - spdk/lib/log - log_flags.c (source / functions) Hit Total Coverage
Test: Combined Lines: 43 47 91.5 %
Date: 2024-07-10 21:01:08 Functions: 9 9 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 39 52 75.0 %

           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) g_log_flags = TAILQ_HEAD_INITIALIZER(g_log_flags);
      11                 :            : 
      12                 :            : static struct spdk_log_flag *
      13                 :  157520100 : get_log_flag(const char *name)
      14                 :            : {
      15                 :            :         struct spdk_log_flag *flag;
      16                 :            : 
      17         [ +  + ]: 4813569096 :         TAILQ_FOREACH(flag, &g_log_flags, tailq) {
      18   [ +  +  -  +  : 4813401917 :                 if (strcasecmp(name, flag->name) == 0) {
                   +  + ]
      19                 :  157352951 :                         return flag;
      20                 :            :                 }
      21                 :            :         }
      22                 :            : 
      23                 :     167153 :         return NULL;
      24                 :            : }
      25                 :            : 
      26                 :            : void
      27                 :     167153 : spdk_log_register_flag(const char *name, struct spdk_log_flag *flag)
      28                 :            : {
      29                 :            :         struct spdk_log_flag *iter;
      30                 :            : 
      31   [ +  -  -  + ]:     167153 :         if (name == NULL || flag == NULL) {
      32                 :          0 :                 SPDK_ERRLOG("missing spdk_log_flag parameters\n");
      33                 :          0 :                 assert(false);
      34                 :            :                 return;
      35                 :            :         }
      36                 :            : 
      37         [ -  + ]:     167153 :         if (get_log_flag(name)) {
      38                 :          0 :                 SPDK_ERRLOG("duplicate spdk_log_flag '%s'\n", name);
      39                 :          0 :                 assert(false);
      40                 :            :                 return;
      41                 :            :         }
      42                 :            : 
      43         [ +  + ]:    2569033 :         TAILQ_FOREACH(iter, &g_log_flags, tailq) {
      44   [ +  +  -  +  :    2546016 :                 if (strcasecmp(iter->name, flag->name) > 0) {
                   +  + ]
      45                 :     144136 :                         TAILQ_INSERT_BEFORE(iter, flag, tailq);
      46                 :     144136 :                         return;
      47                 :            :                 }
      48                 :            :         }
      49                 :            : 
      50                 :      23017 :         TAILQ_INSERT_TAIL(&g_log_flags, flag, tailq);
      51                 :            : }
      52                 :            : 
      53                 :            : bool
      54                 :  157352951 : spdk_log_get_flag(const char *name)
      55                 :            : {
      56                 :  157352951 :         struct spdk_log_flag *flag = get_log_flag(name);
      57                 :            : 
      58   [ +  -  +  +  :  157352951 :         if (flag && flag->enabled) {
                   +  + ]
      59                 :        649 :                 return true;
      60                 :            :         }
      61                 :            : 
      62                 :  157352302 :         return false;
      63                 :            : }
      64                 :            : 
      65                 :            : static int
      66                 :        271 : log_set_flag(const char *name, bool value)
      67                 :            : {
      68                 :            :         struct spdk_log_flag *flag;
      69                 :        271 :         int rc = -EINVAL;
      70                 :            : 
      71   [ -  +  +  + ]:        271 :         if (strcasecmp(name, "all") == 0) {
      72         [ +  + ]:        120 :                 TAILQ_FOREACH(flag, &g_log_flags, tailq) {
      73                 :        112 :                         flag->enabled = value;
      74                 :            :                 }
      75                 :          8 :                 return 0;
      76                 :            :         }
      77                 :            : 
      78         [ +  + ]:      15279 :         TAILQ_FOREACH(flag, &g_log_flags, tailq) {
      79         [ +  + ]:      15016 :                 if (fnmatch(name, flag->name, FNM_CASEFOLD) == 0) {
      80                 :        263 :                         flag->enabled = value;
      81                 :        263 :                         rc = 0;
      82                 :            :                 }
      83                 :            :         }
      84                 :            : 
      85                 :        263 :         return rc;
      86                 :            : }
      87                 :            : 
      88                 :            : int
      89                 :        263 : spdk_log_set_flag(const char *name)
      90                 :            : {
      91                 :        263 :         return log_set_flag(name, true);
      92                 :            : }
      93                 :            : 
      94                 :            : int
      95                 :          8 : spdk_log_clear_flag(const char *name)
      96                 :            : {
      97                 :          8 :         return log_set_flag(name, false);
      98                 :            : }
      99                 :            : 
     100                 :            : struct spdk_log_flag *
     101                 :          6 : spdk_log_get_first_flag(void)
     102                 :            : {
     103                 :          6 :         return TAILQ_FIRST(&g_log_flags);
     104                 :            : }
     105                 :            : 
     106                 :            : struct spdk_log_flag *
     107                 :        402 : spdk_log_get_next_flag(struct spdk_log_flag *flag)
     108                 :            : {
     109                 :        402 :         return TAILQ_NEXT(flag, tailq);
     110                 :            : }
     111                 :            : 
     112                 :            : void
     113                 :         48 : spdk_log_usage(FILE *f, const char *log_arg)
     114                 :            : {
     115                 :            :         struct spdk_log_flag *flag;
     116   [ -  +  -  + ]:         48 :         fprintf(f, " %s, --logflag <flag>    enable log flag (all", log_arg);
     117                 :            : 
     118         [ +  + ]:       1075 :         TAILQ_FOREACH(flag, &g_log_flags, tailq) {
     119   [ -  +  -  + ]:       1027 :                 fprintf(f, ", %s", flag->name);
     120                 :            :         }
     121                 :            : 
     122   [ -  +  -  + ]:         48 :         fprintf(f, ")\n");
     123                 :         48 : }

Generated by: LCOV version 1.14