LCOV - code coverage report
Current view: top level - home/fuchsto/workspaces/dash-development/dart-impl/base/include/dash/dart/base - logging.h (source / functions) Hit Total Coverage
Test: coverage.info.cleaned Lines: 3 3 100.0 %
Date: 2016-06-29 12:30:40 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /**
       2             :  *  \file logging.h
       3             :  *
       4             :  *  Logging macros.
       5             :  */
       6             : #ifndef DART__BASE__LOGGING_H__
       7             : #define DART__BASE__LOGGING_H__
       8             : 
       9             : #include <sys/types.h>
      10             : #include <unistd.h>
      11             : #include <string.h>
      12             : #include <stdio.h>
      13             : 
      14             : #include <dash/dart/if/dart_types.h>
      15             : #include <dash/dart/if/dart_config.h>
      16             : #include <dash/dart/if/dart_team_group.h>
      17             : 
      18             : #ifdef DART_ENABLE_ASSERTIONS
      19             : #include <assert.h>
      20             : #endif
      21             : 
      22             : 
      23             : /* Width of unit id field in log messages in number of characters */
      24             : #define DASH__DART_LOGGING__UNIT__WIDTH 4
      25             : /* Width of process id field in log messages in number of characters */
      26             : #define DASH__DART_LOGGING__PROC__WIDTH 5
      27             : /* Width of file name field in log messages in number of characters */
      28             : #define DASH__DART_LOGGING__FILE__WIDTH 25
      29             : /* Width of line number field in log messages in number of characters */
      30             : #define DASH__DART_LOGGING__LINE__WIDTH 4
      31             : /* Maximum length of a single log message in number of characters */
      32             : #define DASH__DART_LOGGING__MAX_MESSAGE_LENGTH 256;
      33             : 
      34             : #ifdef DART_LOG_OUTPUT_STDERR
      35             : #define DART_LOG_OUTPUT_TARGET stderr
      36             : #else
      37             : #define DART_LOG_OUTPUT_TARGET stdout
      38             : #endif
      39             : 
      40             : /* GNU variant of basename.3 */
      41           1 : inline char * dart_base_logging_basename(char *path) {
      42           1 :     char *base = strrchr(path, '/');
      43           1 :     return base ? base+1 : path;
      44             : }
      45             : 
      46             : //
      47             : // Always log error messages:
      48             : //
      49             : #define DART_LOG_ERROR(...) \
      50             :   do { \
      51             :     const int maxlen = DASH__DART_LOGGING__MAX_MESSAGE_LENGTH; \
      52             :     int       sn_ret; \
      53             :     char      msg_buf[maxlen]; \
      54             :     pid_t     pid = getpid(); \
      55             :     sn_ret = snprintf(msg_buf, maxlen, __VA_ARGS__); \
      56             :     if (sn_ret < 0 || sn_ret >= maxlen) { \
      57             :       break; \
      58             :     } \
      59             :     dart_unit_t unit_id = -1; \
      60             :     dart_myid(&unit_id); \
      61             :     fprintf(DART_LOG_OUTPUT_TARGET, \
      62             :       "[ %*d ERROR ] [ %*d ] %-*s:%-*d !!! DART: %s\n", \
      63             :       DASH__DART_LOGGING__UNIT__WIDTH, unit_id, \
      64             :       DASH__DART_LOGGING__PROC__WIDTH, pid, \
      65             :       DASH__DART_LOGGING__FILE__WIDTH, dart_base_logging_basename(__FILE__), \
      66             :       DASH__DART_LOGGING__LINE__WIDTH, __LINE__, \
      67             :       msg_buf); \
      68             :   } while (0)
      69             : 
      70             : //
      71             : // Debug, Info, and Trace log messages:
      72             : //
      73             : #ifdef DART_ENABLE_LOGGING
      74             : 
      75             : #define DART_LOG_TRACE(...) \
      76             :   do { \
      77             :     dart_config_t * dart_cfg;  \
      78             :     dart_config(&dart_cfg);    \
      79             :     if (!dart_cfg->log_enabled) { \
      80             :       break; \
      81             :     } \
      82             :     const int maxlen = DASH__DART_LOGGING__MAX_MESSAGE_LENGTH; \
      83             :     int       sn_ret; \
      84             :     char      msg_buf[maxlen]; \
      85             :     pid_t     pid = getpid(); \
      86             :     sn_ret = snprintf(msg_buf, maxlen, __VA_ARGS__); \
      87             :     if (sn_ret < 0 || sn_ret >= maxlen) { \
      88             :       break; \
      89             :     } \
      90             :     dart_unit_t unit_id = -1; \
      91             :     dart_myid(&unit_id); \
      92             :     fprintf(DART_LOG_OUTPUT_TARGET, \
      93             :       "[ %*d TRACE ] [ %*d ] %-*s:%-*d :   DART: %s\n", \
      94             :       DASH__DART_LOGGING__UNIT__WIDTH, unit_id, \
      95             :       DASH__DART_LOGGING__PROC__WIDTH, pid, \
      96             :       DASH__DART_LOGGING__FILE__WIDTH, dart_base_logging_basename(__FILE__), \
      97             :       DASH__DART_LOGGING__LINE__WIDTH, __LINE__, \
      98             :       msg_buf); \
      99             :   } while (0)
     100             : 
     101             : #define DART_LOG_DEBUG(...) \
     102             :   do { \
     103             :     dart_config_t * dart_cfg;  \
     104             :     dart_config(&dart_cfg);    \
     105             :     if (!dart_cfg->log_enabled) { \
     106             :       break; \
     107             :     } \
     108             :     const int maxlen = DASH__DART_LOGGING__MAX_MESSAGE_LENGTH; \
     109             :     int       sn_ret; \
     110             :     char      msg_buf[maxlen]; \
     111             :     pid_t     pid = getpid(); \
     112             :     sn_ret = snprintf(msg_buf, maxlen, __VA_ARGS__); \
     113             :     if (sn_ret < 0 || sn_ret >= maxlen) { \
     114             :       break; \
     115             :     } \
     116             :     dart_unit_t unit_id = -1; \
     117             :     dart_myid(&unit_id); \
     118             :     fprintf(DART_LOG_OUTPUT_TARGET, \
     119             :       "[ %*d DEBUG ] [ %*d ] %-*s:%-*d :   DART: %s\n", \
     120             :       DASH__DART_LOGGING__UNIT__WIDTH, unit_id, \
     121             :       DASH__DART_LOGGING__PROC__WIDTH, pid, \
     122             :       DASH__DART_LOGGING__FILE__WIDTH, dart_base_logging_basename(__FILE__), \
     123             :       DASH__DART_LOGGING__LINE__WIDTH, __LINE__, \
     124             :       msg_buf); \
     125             :   } while (0)
     126             : 
     127             : #define DART_LOG_INFO(...) \
     128             :   do { \
     129             :     dart_config_t * dart_cfg;  \
     130             :     dart_config(&dart_cfg);    \
     131             :     if (!dart_cfg->log_enabled) { \
     132             :       break; \
     133             :     } \
     134             :     const int maxlen = DASH__DART_LOGGING__MAX_MESSAGE_LENGTH; \
     135             :     int       sn_ret; \
     136             :     char      msg_buf[maxlen]; \
     137             :     pid_t     pid = getpid(); \
     138             :     sn_ret = snprintf(msg_buf, maxlen, __VA_ARGS__); \
     139             :     if (sn_ret < 0 || sn_ret >= maxlen) { \
     140             :       break; \
     141             :     } \
     142             :     dart_unit_t unit_id = -1; \
     143             :     dart_myid(&unit_id); \
     144             :     fprintf(DART_LOG_OUTPUT_TARGET, \
     145             :       "[ %*d INFO  ] [ %*d ] %-*s:%-*d :   DART: %s\n", \
     146             :       DASH__DART_LOGGING__UNIT__WIDTH, unit_id, \
     147             :       DASH__DART_LOGGING__PROC__WIDTH, pid, \
     148             :       DASH__DART_LOGGING__FILE__WIDTH, dart_base_logging_basename(__FILE__), \
     149             :       DASH__DART_LOGGING__LINE__WIDTH, __LINE__, \
     150             :       msg_buf); \
     151             :   } while (0)
     152             : 
     153             : #else /* DART_ENABLE_LOGGING */
     154             : 
     155             : #define DART_LOG_TRACE(...) do { } while(0)
     156             : #define DART_LOG_DEBUG(...) do { } while(0)
     157             : #define DART_LOG_INFO(...)  do { } while(0)
     158             : 
     159             : #endif /* DART_ENABLE_LOGGING */
     160             : 
     161             : #endif /* DART__BASE__LOGGING_H__ */

Generated by: LCOV version 1.12