LCOV - code coverage report
Current view: top level - spdk/lib/log - log.c (source / functions) Hit Total Coverage
Test: Combined Lines: 106 135 78.5 %
Date: 2024-07-16 01:05:53 Functions: 12 15 80.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 57 96 59.4 %

           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                 :            : void
      27                 :       2676 : spdk_log_set_level(enum spdk_log_level level)
      28                 :            : {
      29         [ -  + ]:       2676 :         assert(level >= SPDK_LOG_DISABLED);
      30         [ -  + ]:       2676 :         assert(level <= SPDK_LOG_DEBUG);
      31                 :       2676 :         g_spdk_log_level = level;
      32                 :       2676 : }
      33                 :            : 
      34                 :            : enum spdk_log_level
      35                 :         36 : spdk_log_get_level(void) {
      36                 :         36 :         return g_spdk_log_level;
      37                 :            : }
      38                 :            : 
      39                 :            : void
      40                 :       2705 : spdk_log_set_print_level(enum spdk_log_level level)
      41                 :            : {
      42         [ -  + ]:       2705 :         assert(level >= SPDK_LOG_DISABLED);
      43         [ -  + ]:       2705 :         assert(level <= SPDK_LOG_DEBUG);
      44                 :       2705 :         g_spdk_log_print_level = level;
      45                 :       2705 : }
      46                 :            : 
      47                 :            : enum spdk_log_level
      48                 :         15 : spdk_log_get_print_level(void) {
      49                 :         15 :         return g_spdk_log_print_level;
      50                 :            : }
      51                 :            : 
      52                 :            : void
      53                 :       2667 : spdk_log_open(logfunc *logf)
      54                 :            : {
      55         [ +  + ]:       2667 :         if (logf) {
      56                 :          3 :                 g_log = logf;
      57                 :            :         } else {
      58                 :       2664 :                 openlog("spdk", LOG_PID, LOG_LOCAL7);
      59                 :            :         }
      60                 :       2667 : }
      61                 :            : 
      62                 :            : void
      63                 :       2683 : spdk_log_close(void)
      64                 :            : {
      65         [ +  - ]:       2683 :         if (!g_log) {
      66                 :       2683 :                 closelog();
      67                 :            :         }
      68                 :       2683 : }
      69                 :            : 
      70                 :            : void
      71                 :          0 : spdk_log_enable_timestamps(bool value)
      72                 :            : {
      73                 :          0 :         g_log_timestamps = value;
      74                 :          0 : }
      75                 :            : 
      76                 :            : static void
      77                 :    6702962 : get_timestamp_prefix(char *buf, int buf_size)
      78                 :            : {
      79                 :            :         struct tm *info;
      80                 :      34928 :         char date[24];
      81                 :      34928 :         struct timespec ts;
      82                 :            :         long usec;
      83                 :            : 
      84   [ -  +  -  + ]:    6702962 :         if (!g_log_timestamps) {
      85                 :          0 :                 buf[0] = '\0';
      86                 :          0 :                 return;
      87                 :            :         }
      88                 :            : 
      89         [ -  + ]:    6702962 :         clock_gettime(CLOCK_REALTIME, &ts);
      90                 :    6702962 :         info = localtime(&ts.tv_sec);
      91                 :    6702962 :         usec = ts.tv_nsec / 1000;
      92         [ -  + ]:    6702962 :         if (info == NULL) {
      93         [ #  # ]:          0 :                 snprintf(buf, buf_size, "[%s.%06ld] ", "unknown date", usec);
      94                 :          0 :                 return;
      95                 :            :         }
      96                 :            : 
      97         [ -  + ]:    6702962 :         strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", info);
      98         [ -  + ]:    6702962 :         snprintf(buf, buf_size, "[%s.%06ld] ", date, usec);
      99                 :            : }
     100                 :            : 
     101                 :            : void
     102                 :    7100694 : spdk_log(enum spdk_log_level level, const char *file, const int line, const char *func,
     103                 :            :          const char *format, ...)
     104                 :            : {
     105                 :      42552 :         va_list ap;
     106                 :            : 
     107                 :    7100694 :         va_start(ap, format);
     108                 :    7100694 :         spdk_vlog(level, file, line, func, format, ap);
     109                 :    7100694 :         va_end(ap);
     110                 :    7100694 : }
     111                 :            : 
     112                 :            : int
     113                 :    6702983 : spdk_log_to_syslog_level(enum spdk_log_level level)
     114                 :            : {
     115   [ +  +  +  +  :    6702983 :         switch (level) {
                   -  - ]
     116                 :      35519 :         case SPDK_LOG_DEBUG:
     117                 :            :         case SPDK_LOG_INFO:
     118                 :      35519 :                 return LOG_INFO;
     119                 :    2584941 :         case SPDK_LOG_NOTICE:
     120                 :    2584941 :                 return LOG_NOTICE;
     121                 :        507 :         case SPDK_LOG_WARN:
     122                 :        507 :                 return LOG_WARNING;
     123                 :    4082016 :         case SPDK_LOG_ERROR:
     124                 :    4082016 :                 return LOG_ERR;
     125                 :          0 :         case SPDK_LOG_DISABLED:
     126                 :          0 :                 return -1;
     127                 :          0 :         default:
     128                 :          0 :                 break;
     129                 :            :         }
     130                 :            : 
     131                 :          0 :         return LOG_INFO;
     132                 :            : }
     133                 :            : 
     134                 :            : void
     135                 :    7101830 : spdk_vlog(enum spdk_log_level level, const char *file, const int line, const char *func,
     136                 :            :           const char *format, va_list ap)
     137                 :            : {
     138                 :    7101830 :         int severity = LOG_INFO;
     139                 :    7101830 :         char *buf, _buf[MAX_TMPBUF], *ext_buf = NULL;
     140                 :      42552 :         char timestamp[64];
     141                 :            :         int rc;
     142                 :            : 
     143         [ +  + ]:    7101830 :         if (g_log) {
     144                 :         15 :                 g_log(level, file, line, func, format, ap);
     145                 :     391259 :                 return;
     146                 :            :         }
     147                 :            : 
     148   [ +  +  +  - ]:    7101815 :         if (level > g_spdk_log_print_level && level > g_spdk_log_level) {
     149                 :     398853 :                 return;
     150                 :            :         }
     151                 :            : 
     152                 :    6702962 :         severity = spdk_log_to_syslog_level(level);
     153         [ -  + ]:    6702962 :         if (severity < 0) {
     154                 :          0 :                 return;
     155                 :            :         }
     156                 :            : 
     157                 :    6702962 :         buf = _buf;
     158                 :            : 
     159         [ -  + ]:    6702962 :         rc = vsnprintf(_buf, sizeof(_buf), format, ap);
     160         [ +  + ]:    6702962 :         if (rc > MAX_TMPBUF) {
     161                 :            :                 /* The output including the terminating was more than MAX_TMPBUF bytes.
     162                 :            :                  * Try allocating memory large enough to hold the output.
     163                 :            :                  */
     164                 :          3 :                 rc = vasprintf(&ext_buf, format, ap);
     165         [ +  - ]:          3 :                 if (rc < 0) {
     166                 :            :                         /* Failed to allocate memory. Allow output to be truncated. */
     167                 :            :                 } else {
     168                 :          3 :                         buf = ext_buf;
     169                 :            :                 }
     170                 :            :         }
     171                 :            : 
     172         [ +  - ]:    6702962 :         if (level <= g_spdk_log_print_level) {
     173                 :    6702962 :                 get_timestamp_prefix(timestamp, sizeof(timestamp));
     174         [ +  + ]:    6702962 :                 if (file) {
     175   [ -  +  -  + ]:    6696772 :                         fprintf(stderr, "%s%s:%4d:%s: *%s*: %s", timestamp, file, line, func, spdk_level_names[level], buf);
     176                 :            :                 } else {
     177   [ -  +  -  + ]:       6190 :                         fprintf(stderr, "%s%s", timestamp, buf);
     178                 :            :                 }
     179                 :            :         }
     180                 :            : 
     181         [ +  + ]:    6702962 :         if (level <= g_spdk_log_level) {
     182         [ +  + ]:    6666875 :                 if (file) {
     183                 :    6661667 :                         syslog(severity, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf);
     184                 :            :                 } else {
     185                 :       5208 :                         syslog(severity, "%s", buf);
     186                 :            :                 }
     187                 :            :         }
     188                 :            : 
     189                 :    6702962 :         free(ext_buf);
     190                 :            : }
     191                 :            : 
     192                 :            : void
     193                 :          0 : spdk_vflog(FILE *fp, const char *file, const int line, const char *func,
     194                 :            :            const char *format, va_list ap)
     195                 :            : {
     196                 :          0 :         char buf[MAX_TMPBUF];
     197                 :          0 :         char timestamp[64];
     198                 :            : 
     199         [ #  # ]:          0 :         vsnprintf(buf, sizeof(buf), format, ap);
     200                 :            : 
     201                 :          0 :         get_timestamp_prefix(timestamp, sizeof(timestamp));
     202                 :            : 
     203         [ #  # ]:          0 :         if (file) {
     204   [ #  #  #  # ]:          0 :                 fprintf(fp, "%s%s:%4d:%s: %s", timestamp, file, line, func, buf);
     205                 :            :         } else {
     206   [ #  #  #  # ]:          0 :                 fprintf(fp, "%s%s", timestamp, buf);
     207                 :            :         }
     208                 :            : 
     209                 :          0 :         fflush(fp);
     210                 :          0 : }
     211                 :            : 
     212                 :            : void
     213                 :          0 : spdk_flog(FILE *fp, const char *file, const int line, const char *func,
     214                 :            :           const char *format, ...)
     215                 :            : {
     216                 :          0 :         va_list ap;
     217                 :            : 
     218                 :          0 :         va_start(ap, format);
     219                 :          0 :         spdk_vflog(fp, file, line, func, format, ap);
     220                 :          0 :         va_end(ap);
     221                 :          0 : }
     222                 :            : 
     223                 :            : static void
     224                 :       5224 : fdump(FILE *fp, const char *label, const uint8_t *buf, size_t len)
     225                 :            : {
     226                 :         12 :         char tmpbuf[MAX_TMPBUF];
     227                 :         12 :         char buf16[16 + 1];
     228                 :            :         size_t total;
     229                 :            :         unsigned int idx;
     230                 :            : 
     231         [ -  + ]:       5224 :         fprintf(fp, "%s\n", label);
     232                 :            : 
     233                 :       5224 :         memset(buf16, 0, sizeof buf16);
     234                 :       5224 :         total = 0;
     235         [ +  + ]:    3868355 :         for (idx = 0; idx < len; idx++) {
     236   [ +  +  +  + ]:    3863131 :                 if (idx != 0 && idx % 16 == 0) {
     237                 :     236227 :                         snprintf(tmpbuf + total, sizeof tmpbuf - total,
     238                 :            :                                  " %s", buf16);
     239                 :     236227 :                         memset(buf16, 0, sizeof buf16);
     240         [ -  + ]:     236227 :                         fprintf(fp, "%s\n", tmpbuf);
     241                 :     236227 :                         total = 0;
     242                 :            :                 }
     243         [ +  + ]:    3863131 :                 if (idx % 16 == 0) {
     244                 :     241451 :                         total += snprintf(tmpbuf + total, sizeof tmpbuf - total,
     245                 :            :                                           "%08x ", idx);
     246                 :            :                 }
     247         [ +  + ]:    3863131 :                 if (idx % 8 == 0) {
     248                 :     482895 :                         total += snprintf(tmpbuf + total, sizeof tmpbuf - total,
     249                 :            :                                           "%s", " ");
     250                 :            :                 }
     251                 :    7726222 :                 total += snprintf(tmpbuf + total, sizeof tmpbuf - total,
     252                 :    3863131 :                                   "%2.2x ", buf[idx] & 0xff);
     253         [ +  + ]:    3863131 :                 buf16[idx % 16] = isprint(buf[idx]) ? buf[idx] : '.';
     254                 :            :         }
     255         [ +  + ]:       5309 :         for (; idx % 16 != 0; idx++) {
     256         [ +  + ]:         85 :                 if (idx == 8) {
     257                 :          4 :                         total += snprintf(tmpbuf + total, sizeof tmpbuf - total,
     258                 :            :                                           " ");
     259                 :            :                 }
     260                 :            : 
     261                 :         85 :                 total += snprintf(tmpbuf + total, sizeof tmpbuf - total, "   ");
     262                 :            :         }
     263                 :       5224 :         snprintf(tmpbuf + total, sizeof tmpbuf - total, "  %s", buf16);
     264         [ -  + ]:       5224 :         fprintf(fp, "%s\n", tmpbuf);
     265                 :       5224 :         fflush(fp);
     266                 :       5224 : }
     267                 :            : 
     268                 :            : void
     269                 :       5224 : spdk_log_dump(FILE *fp, const char *label, const void *buf, size_t len)
     270                 :            : {
     271                 :       5224 :         fdump(fp, label, buf, len);
     272                 :       5224 : }

Generated by: LCOV version 1.14