NachOS/code/threads/utility.cc

115 lines
3.4 KiB
C++

// utility.cc
// Debugging routines. Allows users to control whether to
// print DEBUG statements, based on a command line argument.
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved. See copyright.h for copyright notice and limitation
// of liability and disclaimer of warranty provisions.
#include "copyright.h"
#include "utility.h"
#include <unistd.h>
// this seems to be dependent on how the compiler is configured.
// if you have problems with va_start, try both of these alternatives
#if defined(HOST_SNAKE) || defined(HOST_SPARC) || defined(HOST_i386) || defined(HOST_PPC) || defined(HOST_x86_64)
#include <stdarg.h>
#else
#include "/usr/include/stdarg.h"
#endif
static const char *enableFlags = NULL; // controls which DEBUG messages are printed
//----------------------------------------------------------------------
// SetColor
// Set the color for subsequent printouts
//
// This assumes that the TTY recognizes ANSI colors
//----------------------------------------------------------------------
void
SetColor (FILE *output, enum AnsiColor color)
{
if (isatty(fileno(output)))
fprintf(output, "\e[%dm", 30 + color);
}
//----------------------------------------------------------------------
// SetBold
// Set bold attribute for subsequent printouts
//
// This assumes that the TTY recognizes ANSI colors
//----------------------------------------------------------------------
void
SetBold (FILE *output)
{
if (isatty(fileno(output)))
fprintf(output, "\e[1m");
}
//----------------------------------------------------------------------
// ClearColor
// Clear the color to default for subsequent printouts
//
// This assumes that the TTY recognizes ANSI colors
//----------------------------------------------------------------------
void
ClearColor (FILE *output)
{
if (isatty(fileno(output)))
fprintf(output, "\e[0m");
}
//----------------------------------------------------------------------
// DebugInit
// Initialize so that only DEBUG messages with a flag in flagList
// will be printed.
//
// If the flag is "+", we enable all DEBUG messages.
//
// "flagList" is a string of characters for whose DEBUG messages are
// to be enabled.
//----------------------------------------------------------------------
void
DebugInit (const char *flagList)
{
enableFlags = flagList;
}
//----------------------------------------------------------------------
// DebugIsEnabled
// Return TRUE if DEBUG messages with "flag" are to be printed.
//----------------------------------------------------------------------
bool
DebugIsEnabled (char flag)
{
if (enableFlags != NULL)
return (strchr (enableFlags, flag) != 0)
|| (strchr (enableFlags, '+') != 0);
else
return FALSE;
}
//----------------------------------------------------------------------
// DEBUG
// Print a debug message, if flag is enabled. Like printf,
// only with an extra argument on the front.
//----------------------------------------------------------------------
void
DEBUG (char flag, const char *format, ...)
{
if (DebugIsEnabled (flag))
{
va_list ap;
// You will get an unused variable message here -- ignore it.
va_start (ap, format);
SetColor(stdout, ColorMagenta);
vfprintf (stdout, format, ap);
ClearColor(stdout);
va_end (ap);
fflush (stdout);
}
}