ff_i_console

Server console and logging functions.

ANSI-colored server console output and timestamped log entry helpers.

CONSOLE_HELP_PAD
CONSOLE_COLOR_NONE

Reset console color to terminal defaults.

CONSOLE_COLOR_RED

Console bright red.

CONSOLE_COLOR_GREEN

Console bright green.

CONSOLE_COLOR_YELLOW

Console bright yellow.

CONSOLE_COLOR_BLUE

Console bright blue.

CONSOLE_COLOR_PURPLE

Console bright purple.

CONSOLE_COLOR_CYAN

Console bright cyan.

CONSOLE_COLOR_WHITE

Console white.

int IsATTY()

Return true if console is a TTY.

string GetConsoleColorString(string sString, string sConsoleColor)
Parameters:
  • sString – String to colorize

  • sConsoleColor – A CONSOLE_COLOR_*

Returns:

Colorized string

Return a Linux shell color control code CONSOLE_COLOR_* if TTY, otherwise return the uncolored string.

void ConsoleClearScreen()

Clear the screen.

void WriteTimestampedLogEntryEx(string sPrefix, string sLogEntry, string sConsoleColor = CONSOLE_COLOR_NONE)
Parameters:
  • sPrefix – Add this prefix to the string

  • sLogEntry – Print this log entry

  • sConsoleColor – A CONSOLE_COLOR_*

Write sLogEntry as a timestamped entry into the log file.

void LogTrace(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_PURPLE)
Parameters:
  • sLogEntry – Print this log entry

  • sConsoleColor – A CONSOLE_COLOR_*

Write sLogEntry as a timestamped entry into the log file.

void LogDebug(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_BLUE)
Parameters:
  • sLogEntry – Print this log entry

  • sConsoleColor – A CONSOLE_COLOR_*

Write sLogEntry as a timestamped entry into the log file.

void LogInfo(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_NONE)
Parameters:
  • sLogEntry – Print this log entry

  • sConsoleColor – A CONSOLE_COLOR_*

Write sLogEntry as a prefixed timestamped entry into the log file.

void LogNotice(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_GREEN)
Parameters:
  • sLogEntry – Print this log entry

  • sConsoleColor – A CONSOLE_COLOR_*

Write sLogEntry as a prefixed timestamped entry into the log file, in color.

void LogWarning(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_YELLOW)
Parameters:
  • sLogEntry – Print this log entry

  • sConsoleColor – A CONSOLE_COLOR_*

Write sLogEntry as a prefixed timestamped entry into the log file, in color.

void LogError(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_RED)
Parameters:
  • sLogEntry – Print this log entry

  • sConsoleColor – A CONSOLE_COLOR_*

Write sLogEntry as a prefixed timestamped entry into the log file, in color.

void LogFatal(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_RED)
Parameters:
  • sLogEntry – Print this log entry

  • sConsoleColor – A CONSOLE_COLOR_*

Write sLogEntry as a prefixed timestamped entry into the log file, in color; then shutdown.

Source code

// @code

#include "ff_i_string"
#include "ff_i_debug"

#include "nwnx_admin"


// Left pad all commands in help message
const int CONSOLE_HELP_PAD = 24;

// Linux console ANSI color codes

//! @brief Reset console color to terminal defaults
const string CONSOLE_COLOR_NONE   = "[00m";

//! @brief Console bright red
const string CONSOLE_COLOR_RED    = "[01;31m";

//! @brief Console bright green
const string CONSOLE_COLOR_GREEN  = "[01;32m";

//! @brief Console bright yellow
const string CONSOLE_COLOR_YELLOW = "[01;33m";

//! @brief Console bright blue
const string CONSOLE_COLOR_BLUE   = "[01;34m";

//! @brief Console bright purple
const string CONSOLE_COLOR_PURPLE = "[01;35m";

//! @brief Console bright cyan
const string CONSOLE_COLOR_CYAN   = "[01;36m";

//! @brief Console white
const string CONSOLE_COLOR_WHITE  = "[01;37m";


// -----------------------------------------------------------------------------

//! @brief Return true if console is a TTY
int IsATTY();
int IsATTY()
{
  return NWNX_Util_GetTTY() == "" ? FALSE : TRUE;
}


// -----------------------------------------------------------------------------

//! @brief Return a Linux shell color control code CONSOLE_COLOR_* if TTY, otherwise return the uncolored string.
//! @param sString String to colorize
//! @param sConsoleColor A CONSOLE_COLOR_*
//! @returns Colorized string
string GetConsoleColorString(string sString, string sConsoleColor);
string GetConsoleColorString(string sString, string sConsoleColor)
{
  if (!IsATTY())
    return sString;

  return   GetASCIIEscape() + sConsoleColor
         + sString
         + GetASCIIEscape() + CONSOLE_COLOR_NONE;
}


// -----------------------------------------------------------------------------

//! @brief Clear the screen
void ConsoleClearScreen();
void ConsoleClearScreen()
{
  PrintString(GetASCIIEscape() + "c");
}


// -----------------------------------------------------------------------------

//! @brief Write sLogEntry as a timestamped entry into the log file.
//! @param sPrefix Add this prefix to the string
//! @param sLogEntry Print this log entry
//! @param sConsoleColor A CONSOLE_COLOR_*
void WriteTimestampedLogEntryEx(string sPrefix, string sLogEntry, string sConsoleColor = CONSOLE_COLOR_NONE)
{
  string sString = "";
  if (GetStringLength(sPrefix))
    sString = sPrefix + " ";

  sString += sLogEntry;

  if (sConsoleColor != CONSOLE_COLOR_NONE)
    sString = GetConsoleColorString(sString, sConsoleColor);

  WriteTimestampedLogEntry(sString);
}


// -----------------------------------------------------------------------------

//! @brief Write sLogEntry as a timestamped entry into the log file.
//! @param sLogEntry     Print this log entry
//! @param sConsoleColor A CONSOLE_COLOR_*
void LogTrace(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_PURPLE);
void LogTrace(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_PURPLE)
{
  WriteTimestampedLogEntryEx("trace:", sLogEntry, sConsoleColor);
}


// -----------------------------------------------------------------------------

//! @brief Write sLogEntry as a timestamped entry into the log file.
//! @param sLogEntry     Print this log entry
//! @param sConsoleColor A CONSOLE_COLOR_*
void LogDebug(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_BLUE);
void LogDebug(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_BLUE)
{
  if (GetIsDebug())
    WriteTimestampedLogEntryEx("debug:", sLogEntry, sConsoleColor);
}


// -----------------------------------------------------------------------------

//! @brief Write sLogEntry as a prefixed timestamped entry into the log file.
//! @param sLogEntry     Print this log entry
//! @param sConsoleColor A CONSOLE_COLOR_*
void LogInfo(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_NONE);
void LogInfo(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_NONE)
{
  WriteTimestampedLogEntryEx("info:", sLogEntry, sConsoleColor);
}


// -----------------------------------------------------------------------------

//! @brief Write sLogEntry as a prefixed timestamped entry into the log file, in color.
//! @param sLogEntry     Print this log entry
//! @param sConsoleColor A CONSOLE_COLOR_*
void LogNotice(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_GREEN);
void LogNotice(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_GREEN)
{
  WriteTimestampedLogEntryEx("notice:", sLogEntry, sConsoleColor);
}


// -----------------------------------------------------------------------------

//! @brief Write sLogEntry as a prefixed timestamped entry into the log file, in color.
//! @param sLogEntry     Print this log entry
//! @param sConsoleColor A CONSOLE_COLOR_*
void LogWarning(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_YELLOW);
void LogWarning(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_YELLOW)
{
  WriteTimestampedLogEntryEx("warning:", sLogEntry, sConsoleColor);
}


// -----------------------------------------------------------------------------

//! @brief Write sLogEntry as a prefixed timestamped entry into the log file, in color.
//! @param sLogEntry     Print this log entry
//! @param sConsoleColor A CONSOLE_COLOR_*
void LogError(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_RED);
void LogError(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_RED)
{
  WriteTimestampedLogEntryEx("error:", sLogEntry, sConsoleColor);
}


// -----------------------------------------------------------------------------

//! @brief Write sLogEntry as a prefixed timestamped entry into the log file, in color; then shutdown.
//! @param sLogEntry     Print this log entry
//! @param sConsoleColor A CONSOLE_COLOR_*
void LogFatal(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_RED);
void LogFatal(string sLogEntry, string sConsoleColor = CONSOLE_COLOR_RED)
{
  LogError(sLogEntry, sConsoleColor);
  LogStackTrace(__FILE__, __FUNCTION__, __LINE__);
  SetLocalInt(GetModule(), NWN_SHUTDOWN, 1);
  NWNX_Administration_ShutdownServer();
}