LCOV - code coverage report
Current view: top level - spdk/lib/log - log.c (source / functions) Hit Total Coverage
Test: Combined Lines: 122 152 80.3 %
Date: 2024-12-15 21:47:49 Functions: 13 16 81.2 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 126 200 63.0 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2016 Intel Corporation.
       3                 :            :  *   All rights reserved.
       4                 :            :  */
       5                 :            : 
       6                 :            : #include "spdk/stdinc.h"
       7                 :            : 
       8                 :            : #include "spdk/log.h"
       9                 :            : 
      10                 :            : static const char *const spdk_level_names[] = {
      11                 :            :         [SPDK_LOG_ERROR]        = "ERROR",
      12                 :            :         [SPDK_LOG_WARN]         = "WARNING",
      13                 :            :         [SPDK_LOG_NOTICE]       = "NOTICE",
      14                 :            :         [SPDK_LOG_INFO]         = "INFO",
      15                 :            :         [SPDK_LOG_DEBUG]        = "DEBUG",
      16                 :            : };
      17                 :            : 
      18                 :            : #define MAX_TMPBUF 1024
      19                 :            : 
      20                 :            : static logfunc *g_log = NULL;
      21                 :            : static bool g_log_timestamps = true;
      22                 :            : 
      23                 :            : enum spdk_log_level g_spdk_log_level;
      24                 :            : enum spdk_log_level g_spdk_log_print_level;
      25                 :            : 
      26                 :       4096 : SPDK_LOG_REGISTER_COMPONENT(log)
      27                 :            : 
      28                 :            : void
      29                 :       3023 : spdk_log_set_level(enum spdk_log_level level)
      30                 :            : {
      31   [ +  +  #  # ]:       3023 :         assert(level >= SPDK_LOG_DISABLED);
      32   [ +  +  #  # ]:       3023 :         assert(level <= SPDK_LOG_DEBUG);
      33                 :       3023 :         g_spdk_log_level = level;
      34                 :       3023 : }
      35                 :            : 
      36                 :            : enum spdk_log_level
      37                 :         65 : spdk_log_get_level(void) {
      38                 :         65 :         return g_spdk_log_level;
      39                 :            : }
      40                 :            : 
      41                 :            : void
      42                 :       3065 : spdk_log_set_print_level(enum spdk_log_level level)
      43                 :            : {
      44   [ +  +  #  # ]:       3065 :         assert(level >= SPDK_LOG_DISABLED);
      45   [ +  +  #  # ]:       3065 :         assert(level <= SPDK_LOG_DEBUG);
      46                 :       3065 :         g_spdk_log_print_level = level;
      47                 :       3065 : }
      48                 :            : 
      49                 :            : enum spdk_log_level
      50                 :         30 : spdk_log_get_print_level(void) {
      51                 :         30 :         return g_spdk_log_print_level;
      52                 :            : }
      53                 :            : 
      54                 :            : void
      55                 :       3005 : spdk_log_open(logfunc *logf)
      56                 :            : {
      57         [ +  + ]:       3005 :         if (logf) {
      58                 :          6 :                 g_log = logf;
      59                 :          1 :         } else {
      60   [ -  +  +  - ]:       2999 :                 openlog("spdk", LOG_PID, LOG_LOCAL7);
      61                 :            :         }
      62                 :       3005 : }
      63                 :            : 
      64                 :            : void
      65                 :       2999 : spdk_log_close(void)
      66                 :            : {
      67         [ +  - ]:       2999 :         if (!g_log) {
      68                 :       2999 :                 closelog();
      69                 :        108 :         }
      70                 :       2999 : }
      71                 :            : 
      72                 :            : void
      73                 :          0 : spdk_log_enable_timestamps(bool value)
      74                 :            : {
      75         [ #  # ]:          0 :         g_log_timestamps = value;
      76                 :          0 : }
      77                 :            : 
      78                 :            : static void
      79                 :   10550775 : get_timestamp_prefix(char *buf, int buf_size)
      80                 :            : {
      81                 :       1205 :         struct tm *info;
      82                 :      31629 :         char date[24];
      83                 :      31629 :         struct timespec ts;
      84                 :       1205 :         long usec;
      85                 :            : 
      86   [ +  +  +  + ]:   10550775 :         if (!g_log_timestamps) {
      87   [ #  #  #  # ]:          0 :                 buf[0] = '\0';
      88                 :          0 :                 return;
      89                 :            :         }
      90                 :            : 
      91         [ +  + ]:   10550775 :         clock_gettime(CLOCK_REALTIME, &ts);
      92                 :   10550775 :         info = localtime(&ts.tv_sec);
      93   [ +  -  +  - ]:   10550775 :         usec = ts.tv_nsec / 1000;
      94         [ +  + ]:   10550775 :         if (info == NULL) {
      95         [ #  # ]:          0 :                 snprintf(buf, buf_size, "[%s.%06ld] ", "unknown date", usec);
      96                 :          0 :                 return;
      97                 :            :         }
      98                 :            : 
      99   [ +  +  +  +  :   10550775 :         strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", info);
                   +  + ]
     100         [ -  + ]:   10550775 :         snprintf(buf, buf_size, "[%s.%06ld] ", date, usec);
     101         [ -  + ]:       7161 : }
     102                 :            : 
     103                 :            : void
     104                 :   11318708 : spdk_log(enum spdk_log_level level, const char *file, const int line, const char *func,
     105                 :            :          const char *format, ...)
     106                 :            : {
     107                 :      40993 :         va_list ap;
     108                 :            : 
     109                 :   11318708 :         va_start(ap, format);
     110                 :   11318708 :         spdk_vlog(level, file, line, func, format, ap);
     111                 :   11318708 :         va_end(ap);
     112                 :   11318708 : }
     113                 :            : 
     114                 :            : int
     115                 :   10550810 : spdk_log_to_syslog_level(enum spdk_log_level level)
     116                 :            : {
     117   [ +  +  +  +  :   10550810 :         switch (level) {
                   -  + ]
     118                 :      12250 :         case SPDK_LOG_DEBUG:
     119                 :            :         case SPDK_LOG_INFO:
     120                 :      12251 :                 return LOG_INFO;
     121                 :    7063696 :         case SPDK_LOG_NOTICE:
     122                 :    7069396 :                 return LOG_NOTICE;
     123                 :        457 :         case SPDK_LOG_WARN:
     124                 :        461 :                 return LOG_WARNING;
     125                 :    3467232 :         case SPDK_LOG_ERROR:
     126                 :    3468702 :                 return LOG_ERR;
     127                 :          0 :         case SPDK_LOG_DISABLED:
     128                 :          0 :                 return -1;
     129                 :          0 :         default:
     130                 :          0 :                 break;
     131                 :            :         }
     132                 :            : 
     133                 :          0 :         return LOG_INFO;
     134                 :       7175 : }
     135                 :            : 
     136                 :            : void
     137                 :   11319828 : spdk_vlog(enum spdk_log_level level, const char *file, const int line, const char *func,
     138                 :            :           const char *format, va_list ap)
     139                 :            : {
     140                 :   11319828 :         int severity = LOG_INFO;
     141                 :      40993 :         char buf[MAX_TMPBUF];
     142                 :      40993 :         char timestamp[64];
     143                 :            : 
     144         [ +  + ]:   11319828 :         if (g_log) {
     145   [ -  +  +  - ]:         30 :                 g_log(level, file, line, func, format, ap);
     146                 :     759596 :                 return;
     147                 :            :         }
     148                 :            : 
     149   [ +  +  +  + ]:   11319798 :         if (level > g_spdk_log_print_level && level > g_spdk_log_level) {
     150                 :     769023 :                 return;
     151                 :            :         }
     152                 :            : 
     153                 :   10550775 :         severity = spdk_log_to_syslog_level(level);
     154         [ +  + ]:   10550775 :         if (severity < 0) {
     155                 :          0 :                 return;
     156                 :            :         }
     157                 :            : 
     158         [ -  + ]:   10550775 :         vsnprintf(buf, sizeof(buf), format, ap);
     159                 :            : 
     160         [ +  + ]:   10550775 :         if (level <= g_spdk_log_print_level) {
     161                 :   10550775 :                 get_timestamp_prefix(timestamp, sizeof(timestamp));
     162         [ +  + ]:   10550775 :                 if (file) {
     163   [ +  +  +  +  :   10543913 :                         fprintf(stderr, "%s%s:%4d:%s: *%s*: %s", timestamp, file, line, func, spdk_level_names[level], buf);
                   +  + ]
     164                 :       6953 :                 } else {
     165   [ -  +  -  + ]:       6862 :                         fprintf(stderr, "%s%s", timestamp, buf);
     166                 :            :                 }
     167                 :       7161 :         }
     168                 :            : 
     169         [ +  + ]:   10550775 :         if (level <= g_spdk_log_level) {
     170         [ +  + ]:   10537755 :                 if (file) {
     171   [ +  +  +  +  :   10531895 :                         syslog(severity, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf);
                   +  + ]
     172                 :       6953 :                 } else {
     173                 :       5860 :                         syslog(severity, "%s", buf);
     174                 :            :                 }
     175                 :       7161 :         }
     176         [ -  + ]:       8460 : }
     177                 :            : 
     178                 :            : void
     179                 :          0 : spdk_vflog(FILE *fp, const char *file, const int line, const char *func,
     180                 :            :            const char *format, va_list ap)
     181                 :            : {
     182                 :          0 :         char buf[MAX_TMPBUF];
     183                 :          0 :         char timestamp[64];
     184                 :            : 
     185         [ #  # ]:          0 :         vsnprintf(buf, sizeof(buf), format, ap);
     186                 :            : 
     187                 :          0 :         get_timestamp_prefix(timestamp, sizeof(timestamp));
     188                 :            : 
     189         [ #  # ]:          0 :         if (file) {
     190   [ #  #  #  # ]:          0 :                 fprintf(fp, "%s%s:%4d:%s: %s", timestamp, file, line, func, buf);
     191                 :          0 :         } else {
     192   [ #  #  #  # ]:          0 :                 fprintf(fp, "%s%s", timestamp, buf);
     193                 :            :         }
     194                 :            : 
     195                 :          0 :         fflush(fp);
     196                 :          0 : }
     197                 :            : 
     198                 :            : void
     199                 :          0 : spdk_flog(FILE *fp, const char *file, const int line, const char *func,
     200                 :            :           const char *format, ...)
     201                 :            : {
     202                 :          0 :         va_list ap;
     203                 :            : 
     204                 :          0 :         va_start(ap, format);
     205                 :          0 :         spdk_vflog(fp, file, line, func, format, ap);
     206                 :          0 :         va_end(ap);
     207                 :          0 : }
     208                 :            : 
     209                 :            : static void
     210                 :         25 : fdump(FILE *fp, const char *label, const uint8_t *buf, size_t len)
     211                 :            : {
     212                 :         18 :         char tmpbuf[MAX_TMPBUF];
     213                 :         18 :         char buf16[16 + 1];
     214                 :          3 :         size_t total;
     215                 :          3 :         unsigned int idx;
     216                 :            : 
     217         [ -  + ]:         25 :         fprintf(fp, "%s\n", label);
     218                 :            : 
     219         [ +  - ]:         25 :         memset(buf16, 0, sizeof buf16);
     220                 :         25 :         total = 0;
     221         [ +  + ]:        492 :         for (idx = 0; idx < len; idx++) {
     222   [ +  +  +  +  :        467 :                 if (idx != 0 && idx % 16 == 0) {
                   +  + ]
     223   [ +  -  +  - ]:         14 :                         snprintf(tmpbuf + total, sizeof tmpbuf - total,
     224                 :          1 :                                  " %s", buf16);
     225         [ +  - ]:         14 :                         memset(buf16, 0, sizeof buf16);
     226         [ -  + ]:         14 :                         fprintf(fp, "%s\n", tmpbuf);
     227                 :         14 :                         total = 0;
     228                 :          1 :                 }
     229   [ +  +  +  + ]:        467 :                 if (idx % 16 == 0) {
     230   [ +  -  +  - ]:         39 :                         total += snprintf(tmpbuf + total, sizeof tmpbuf - total,
     231                 :          4 :                                           "%08x ", idx);
     232                 :          4 :                 }
     233   [ +  +  +  + ]:        467 :                 if (idx % 8 == 0) {
     234   [ +  -  +  - ]:         65 :                         total += snprintf(tmpbuf + total, sizeof tmpbuf - total,
     235                 :            :                                           "%s", " ");
     236                 :          6 :                 }
     237   [ +  -  +  - ]:        894 :                 total += snprintf(tmpbuf + total, sizeof tmpbuf - total,
     238   [ +  -  +  - ]:        467 :                                   "%2.2x ", buf[idx] & 0xff);
     239   [ +  +  +  -  :        467 :                 buf16[idx % 16] = isprint(buf[idx]) ? buf[idx] : '.';
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     240                 :         40 :         }
     241   [ +  +  +  + ]:        182 :         for (; idx % 16 != 0; idx++) {
     242         [ +  + ]:        157 :                 if (idx == 8) {
     243   [ +  -  +  - ]:          7 :                         total += snprintf(tmpbuf + total, sizeof tmpbuf - total,
     244                 :            :                                           " ");
     245                 :          1 :                 }
     246                 :            : 
     247   [ +  -  +  - ]:        157 :                 total += snprintf(tmpbuf + total, sizeof tmpbuf - total, "   ");
     248                 :         24 :         }
     249   [ +  -  +  - ]:         25 :         snprintf(tmpbuf + total, sizeof tmpbuf - total, "  %s", buf16);
     250         [ -  + ]:         25 :         fprintf(fp, "%s\n", tmpbuf);
     251                 :         25 :         fflush(fp);
     252                 :         25 : }
     253                 :            : 
     254                 :            : void
     255                 :         25 : spdk_log_dump(FILE *fp, const char *label, const void *buf, size_t len)
     256                 :            : {
     257                 :         25 :         fdump(fp, label, buf, len);
     258                 :         25 : }

Generated by: LCOV version 1.14