Plan 9 from Bell Labs’s /usr/web/sources/contrib/de0u/root/sys/src/cmd/divergefs/debug.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <u.h>
#include <libc.h>
#include "common.h"
#include "debug.h"


enum
{
  DEBUG_NOISE_LEVEL   = 5,
  DEBUG_INFO_LEVEL    = 4,
  DEBUG_WARNING_LEVEL = 3,
  DEBUG_ERROR_LEVEL   = 2,
  DEBUG_FATAL_LEVEL   = 1
};

enum 
{ 
  DEBUG_DEBUG = true,
  DEBUG_OUTPUT_FD = 2,
  DEBUG_DEFAULT_LEVEL = DEBUG_WARNING_LEVEL
};

typedef struct Debug
{
  uint level;
} Debug;

static Debug debugInstance =
{
  .level = DEBUG_WARNING_LEVEL
};

static Debug *debug_instance(void)
{
  return &debugInstance;
}

void debug_init(void)
{
  debug_set_level(DEBUG_DEFAULT_LEVEL);
}

void debug_set_level(uint level)
{
  if(level > DEBUG_NOISE_LEVEL)
  {
    ERROR(DEBUG_DEBUG, "debug_set_level out of range: %ud (%ud-%ud)", 
      level, DEBUG_FATAL_LEVEL, DEBUG_NOISE_LEVEL);
    level = DEBUG_NOISE_LEVEL;
  }
  debug_instance()->level = level;
}

#define return_not_flagged() if(!flag) return

#define debug_function(level) \
  va_list args; \
  return_not_flagged(); \
  assert_valid(format); \
  va_start(args, format); \
  debug_vprint(level, format, args); \
  va_end(args)

static char *debugLevels[] =
{
  [DEBUG_NOISE_LEVEL]     = "NOISE",
  [DEBUG_INFO_LEVEL]      = "INFO",
  [DEBUG_WARNING_LEVEL]   = "WARNING",
  [DEBUG_ERROR_LEVEL]     = "ERROR",
  [DEBUG_FATAL_LEVEL]     = "FATAL"
};

static void debug_vprint(uint level, char *format, va_list args)
{
  if(level <= debug_instance()->level)
  {
    fprint(DEBUG_OUTPUT_FD, "fs: %s: ", debugLevels[level]);
    vfprint(DEBUG_OUTPUT_FD, format, args);
    fprint(DEBUG_OUTPUT_FD, "\n");
  }
}

void debug_print(char *level, char *format, ...)
{
  va_list args;
  assert_valid(level);
  assert_valid(format);

  va_start(args, format);
  fprint(DEBUG_OUTPUT_FD, "%s: ", level);
  vfprint(DEBUG_OUTPUT_FD, format, args);
  fprint(DEBUG_OUTPUT_FD, "\n");
  va_end(args);
}

void debug_noise(bool flag, char *format, ...)
{
  debug_function(DEBUG_NOISE_LEVEL);
}

void debug_info(bool flag, char *format, ...)
{
  debug_function(DEBUG_INFO_LEVEL);
}

void debug_warning(bool flag, char *format, ...)
{
  debug_function(DEBUG_WARNING_LEVEL);
}

void debug_error(bool flag, char *format, ...)
{
  debug_function(DEBUG_ERROR_LEVEL);
}

void debug_fatal(bool flag, char *format, ...)
{
  debug_function(DEBUG_FATAL_LEVEL);
}




Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.