Compare commits

..

8 Commits

Author SHA1 Message Date
e6b2944c67 build for all OS classes 2021-04-01 14:47:36 +02:00
ea8873becd Remove out of date comment 2021-02-25 15:43:29 +00:00
cf7e7bd6ee Add <unistd.h> for non-windows systems 2021-02-23 17:15:27 +00:00
e36ee60ba7 Remove errlog.h include 2021-02-23 15:26:50 +00:00
e7f36a71af Use streamDebugColored IOC variable instead 2021-02-23 15:24:24 +00:00
ce4b14c611 Check for NULL as well as INVALID_HANDLE_VALUE 2021-02-23 01:09:50 +00:00
7c55d7bdfa Add missing space character 2021-02-23 01:00:28 +00:00
6afa4828eb Allow configuring of coloured console output with STREAM_DEBUG_COLOR
The STREAM_DEBUG_COLOR environment variable can be set to:
    yes  - always generate ANSI colour escape codes on debug/error output
    no   - use plain text output for debug/error
    auto - (default) output ANSI codes if terminal supports it
2021-02-22 22:07:52 +00:00
7 changed files with 69 additions and 23 deletions

View File

@ -5,7 +5,7 @@ else
include /ioc/tools/driver.makefile
EXCLUDE_VERSIONS = 3.13.2
PROJECT=stream
BUILDCLASSES += Linux
BUILDCLASSES += vxWorks Linux WIN32
DOCUDIR = docs

View File

@ -341,7 +341,10 @@ StreamBuffer StreamBuffer::expand(ssize_t start, ssize_t length) const
{
c = buffer[i];
if (c < 0x20 || c >= 0x7f)
result.print("\033[7m<%02x>\033[27m", c & 0xff);
result.print("%s<%02x>%s",
ansiEscape(ANSI_REVERSE_VIDEO),
c & 0xff,
ansiEscape(ANSI_NOT_REVERSE_VIDEO));
else
result.append(c);
}
@ -354,18 +357,21 @@ dump() const
StreamBuffer result;
size_t i;
result.print("%" P "d,%" P "d,%" P "d:", offs, len, cap);
if (offs) result.print("\033[47m");
if (offs) result.print(ansiEscape(ANSI_BG_WHITE));
char c;
for (i = 0; i < cap; i++)
{
c = buffer[i];
if (offs && i == offs) result.append("\033[0m");
if (offs && i == offs) result.append(ansiEscape(ANSI_RESET));
if (c < 0x20 || c >= 0x7f)
result.print("\033[7m<%02x>\033[27m", c & 0xff);
result.print("%s<%02x>%s",
ansiEscape(ANSI_REVERSE_VIDEO),
c & 0xff,
ansiEscape(ANSI_NOT_REVERSE_VIDEO));
else
result.append(c);
if (i == offs+len-1) result.append("\033[47m");
if (i == offs+len-1) result.append(ansiEscape(ANSI_BG_WHITE));
}
result.append("\033[0m");
result.append(ansiEscape(ANSI_RESET));
return result;
}

View File

@ -72,9 +72,9 @@ printCommands(StreamBuffer& buffer, const char* c)
buffer.append(" disconnect;\n");
break;
default:
buffer.append("\033[31;1mGARBAGE: ");
buffer.append(ansiEscape(ANSI_RED_BOLD)).append("GARBAGE: ");
c = StreamProtocolParser::printString(buffer, c-1);
buffer.append("\033[0m\n");
buffer.append(ansiEscape(ANSI_RESET)).append("\n");
}
}
}

View File

@ -198,6 +198,7 @@ public:
extern "C" { // needed for Windows
epicsExportAddress(int, streamDebug);
epicsExportAddress(int, streamError);
epicsExportAddress(int, streamDebugColored);
}
// for subroutine record

View File

@ -23,7 +23,10 @@
#include "StreamError.h"
#ifdef _WIN32
#include <windows.h>
#endif
#include <io.h>
#else
#include <unistd.h>
#endif /* _WIN32 */
#include <string.h>
#include <time.h>
#include <stdio.h>
@ -46,18 +49,36 @@ FILE *StreamDebugFile = NULL;
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
/* Enable ANSI colors in Windows console */
static int win_console_init() {
DWORD dwMode = 0;
HANDLE hCons = GetStdHandle(STD_ERROR_HANDLE);
GetConsoleMode(hCons, &dwMode);
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hCons, dwMode);
return 0;
/* Enable ANSI color support in Windows console */
static bool win_console_init() {
HANDLE hCons[] = { GetStdHandle(STD_ERROR_HANDLE),
GetStdHandle(STD_OUTPUT_HANDLE) };
for(int i=0; i < sizeof(hCons) / sizeof(HANDLE); ++i)
{
DWORD dwMode = 0;
if (hCons[i] == NULL ||
hCons[i] == INVALID_HANDLE_VALUE ||
!GetConsoleMode(hCons[i], &dwMode))
{
return false;
}
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
if (!SetConsoleMode(hCons[i], dwMode))
{
return false;
}
}
return true;
}
static int s = win_console_init();
#endif
/* do isatty() call second as always want to run win_console_init() */
int streamDebugColored = win_console_init() && _isatty(_fileno(stdout));
#else
int streamDebugColored = isatty(fileno(stdout));
#endif /* _WIN32 */
/* You can globally change the printTimestamp function
by setting the StreamPrintTimestampFunction variable
@ -106,14 +127,13 @@ void StreamVError(int line, const char* file, const char* fmt, va_list args)
va_end(args2);
}
#endif
fprintf(stderr, "\033[31;1m");
fprintf(stderr, "%s ", timestamp);
fprintf(stderr, "%s%s ", ansiEscape(ANSI_RED_BOLD), timestamp);
if (file)
{
fprintf(stderr, "%s line %d: ", file, line);
}
vfprintf(stderr, fmt, args);
fprintf(stderr, "\033[0m");
fprintf(stderr, "%s", ansiEscape(ANSI_RESET));
}
int StreamDebugClass::
@ -133,3 +153,13 @@ print(const char* fmt, ...)
va_end(args);
return 1;
}
/**
* Return an ANSI escape code if coloured debug output is enabled
*/
const char* ansiEscape(AnsiMode mode)
{
static const char* AnsiEscapes[] = { "\033[7m", "\033[27m", "\033[47m",
"\033[0m", "\033[31;1m" };
return streamDebugColored ? AnsiEscapes[mode] : "";
}

View File

@ -32,6 +32,7 @@
extern int streamDebug;
extern int streamError;
extern int streamDebugColored;
extern void (*StreamPrintTimestampFunction)(char* buffer, size_t size);
void StreamError(int line, const char* file, const char* fmt, ...)
@ -66,4 +67,11 @@ StreamDebugObject(const char* file, int line)
#define error StreamError
#define debug (!streamDebug)?0:StreamDebugObject(__FILE__,__LINE__).print
/*
* ANSI escape sequences for terminal output
*/
enum AnsiMode { ANSI_REVERSE_VIDEO, ANSI_NOT_REVERSE_VIDEO, ANSI_BG_WHITE,
ANSI_RESET, ANSI_RED_BOLD };
extern const char* ansiEscape(AnsiMode mode);
#endif

View File

@ -32,6 +32,7 @@ if (@ARGV[0] eq "-3.13") {
} else {
print "variable(streamDebug, int)\n";
print "variable(streamError, int)\n";
print "variable(streamDebugColored, int)\n";
print "registrar(streamRegistrar)\n";
if ($asyn) { print "registrar(AsynDriverInterfaceRegistrar)\n"; }
}