github.com/apptainer/singularity@v3.1.1+incompatible/cmd/starter/c/message.c (about)

     1  /*
     2   * Copyright (c) 2017-2019, SyLabs, Inc. All rights reserved.
     3   *
     4   * Copyright (c) 2016-2017, The Regents of the University of California,
     5   * through Lawrence Berkeley National Laboratory (subject to receipt of any
     6   * required approvals from the U.S. Dept. of Energy).  All rights reserved.
     7   *
     8   * This software is licensed under a customized 3-clause BSD license.  Please
     9   * consult LICENSE.md file distributed with the sources of this project regarding
    10   * your rights to use or distribute this software.
    11   *
    12   * NOTICE.  This Software was developed under funding from the U.S. Department of
    13   * Energy and the U.S. Government consequently retains certain rights. As such,
    14   * the U.S. Government has been granted for itself and others acting on its
    15   * behalf a paid-up, nonexclusive, irrevocable, worldwide license in the Software
    16   * to reproduce, distribute copies to the public, prepare derivative works, and
    17   * perform publicly and display publicly, and to permit other to do so. 
    18   */
    19  
    20  #define _GNU_SOURCE
    21  
    22  #include <ctype.h>
    23  #include <stdio.h>
    24  #include <stdlib.h>
    25  #include <unistd.h>
    26  #include <string.h>
    27  #include <stdarg.h>
    28  #include <libgen.h>
    29  
    30  #include "include/message.h"
    31  
    32  int messagelevel = -99;
    33  
    34  extern const char *__progname;
    35  
    36  int count_digit(int n) {
    37      int count = 0;
    38      if ( n == 0 ) {
    39          return 1;
    40      }
    41      count = 1;
    42      while ( (n /= 10) ) {
    43          count++;
    44      }
    45      return count;
    46  }
    47  
    48  void _print(int level, const char *function, const char *file_in, char *format, ...) {
    49      const char *file = file_in;
    50      char message[512];
    51      char *prefix = NULL;
    52      char *color = NULL;
    53      va_list args;
    54  
    55      if ( messagelevel == -99 ) {
    56          char *messagelevel_string = getenv("SINGULARITY_MESSAGELEVEL");
    57  
    58          if ( messagelevel_string == NULL ) {
    59              messagelevel = 5;
    60              singularity_message(DEBUG, "SINGULARITY_MESSAGELEVEL undefined, setting level 5 (debug)\n");
    61          } else {
    62              messagelevel = atoi(messagelevel_string);
    63              if ( messagelevel > 9 ) {
    64                  messagelevel = 9;
    65              }
    66              singularity_message(VERBOSE, "Set messagelevel to: %d\n", messagelevel);
    67          }
    68      }
    69  
    70      if ( level == LOG && messagelevel <= INFO ) {
    71          return;
    72      }
    73  
    74      va_start (args, format);
    75  
    76      if (vsnprintf(message, 512, format, args) >= 512) {
    77          memcpy(message+496, "(TRUNCATED...)", 15);
    78          message[511] = '\0';
    79      }
    80  
    81      va_end (args);
    82  
    83      while( ( ! isalpha(file[0]) ) && ( file[0] != '\0') ) {
    84          file++;
    85      }
    86  
    87      switch (level) {
    88          case ABRT:
    89              prefix = "ABORT";
    90              color = ANSI_COLOR_RED;
    91              break;
    92          case ERROR:
    93              prefix = "ERROR";
    94              color = ANSI_COLOR_LIGHTRED;
    95              break;
    96          case WARNING:
    97              prefix = "WARNING";
    98              color = ANSI_COLOR_YELLOW;
    99              break;
   100          case LOG:
   101              prefix = "LOG";
   102              color = ANSI_COLOR_BLUE;
   103              break;
   104          case DEBUG:
   105              prefix = "DEBUG";
   106              color = "";
   107              break;
   108          case INFO:
   109              prefix = "INFO";
   110              color = "";
   111              break;
   112          default:
   113              prefix = "VERBOSE";
   114              color = "";
   115              break;
   116      }
   117  
   118      if ( level <= messagelevel ) {
   119          char header_string[100];
   120  
   121          if ( messagelevel >= DEBUG ) {
   122              int count, funclen, length;
   123              if ( function[0] == '_' ) {
   124                  function++;
   125              }
   126              count = 10 - count_digit(geteuid()) - count_digit(getpid());
   127              if ( count < 0 ) {
   128                  count = 0;
   129              }
   130              funclen = 28 - strlen(function);
   131              if ( funclen < 0 ) {
   132                  funclen = 0;
   133              }
   134              length = snprintf(header_string, 100, "%s%-7s [U=%d,P=%d] %*s %s() %*s", color, prefix, geteuid(), getpid(), count, "", function, funclen, "");
   135              if ( length < 0 ) {
   136                  return;
   137              } else if ( length > 100 ) {
   138                  header_string[99] = '\0';
   139              }
   140              header_string[length-1] = '\0';
   141          } else {
   142              snprintf(header_string, 15, "%s%-7s: ", color, prefix);
   143          }
   144  
   145          if ( level == INFO && messagelevel == INFO ) {
   146              printf("%s" ANSI_COLOR_RESET, message);
   147          } else if ( level == INFO ) {
   148              printf("%s%s" ANSI_COLOR_RESET, header_string, message);
   149          } else {
   150              fprintf(stderr, "%s%s" ANSI_COLOR_RESET, header_string, message);
   151          }
   152  
   153          fflush(stdout);
   154          fflush(stderr);
   155      }
   156      if ( level == ABRT ) {
   157          exit(255);
   158      }
   159  }