Belle II Software light-2406-ragdoll
LogConnectionConsole Class Referencefinal

Implements a log connection to an IO Stream. More...

#include <LogConnectionConsole.h>

Inheritance diagram for LogConnectionConsole:
Collaboration diagram for LogConnectionConsole:

Public Member Functions

 LogConnectionConsole (int outputFD, bool color)
 Constructor.
 
 LogConnectionConsole (int outputFD)
 Constructor which automatically enables color if the file descriptor is a terminal and supports colors.
 
 ~LogConnectionConsole () override
 Destructor.
 
bool sendMessage (const LogMessage &message) override
 Sends a log message.
 
void write (const std::string &message)
 Send a preformatted string message to the connected output.
 
bool isConnected () override
 Returns true if the connection to the io stream could be established.
 
void finalizeOnAbort () override
 Make sure output is flushed on abort.
 

Static Public Member Functions

static bool terminalSupportsColors (int fileDescriptor)
 Returns true if the given file descriptor is a tty and supports colors.
 
static bool getPythonLoggingEnabled ()
 Check whether console logging via python is enabled.
 
static void setPythonLoggingEnabled (bool enabled)
 Set whether console logging via pyhthon is enabled.
 
static bool getEscapeNewlinesEnabled ()
 Check whether we want to escape newlines on console.
 
static void setEscapeNewlinesEnabled (bool enabled)
 Set whether we want to escape newlines on console.
 

Private Attributes

int m_fd
 The output stream used for sending the log message.
 
bool m_color
 Flag for color output.
 

Static Private Attributes

static bool s_pythonLoggingEnabled {false}
 Flag to indicate whether log messages should be sent to python sys.stdout.
 
static bool s_escapeNewlinesEnabled {false}
 Flag to indicate whether newlines should be replaced by '
' in the output.
 

Detailed Description

Implements a log connection to an IO Stream.

Inherits from the abstract base class LogConnectionBase.

Definition at line 24 of file LogConnectionConsole.h.

Constructor & Destructor Documentation

◆ LogConnectionConsole() [1/2]

LogConnectionConsole ( int  outputFD,
bool  color 
)

Constructor.

Parameters
outputFDThe output file descriptor to write to
colorwhether color should be used for output

Definition at line 23 of file LogConnectionConsole.cc.

23 :
24 m_fd(dup(outputFD)), m_color(color)
25{
26 // check fd
27 if (m_fd < 0) throw std::runtime_error(std::string("Error duplicating file descriptor: ") + std::strerror(errno));
28}
bool m_color
Flag for color output.
int m_fd
The output stream used for sending the log message.

◆ LogConnectionConsole() [2/2]

LogConnectionConsole ( int  outputFD)
inlineexplicit

Constructor which automatically enables color if the file descriptor is a terminal and supports colors.

Parameters
outputFDThe output file descriptor to write to.

Definition at line 35 of file LogConnectionConsole.h.

35: LogConnectionConsole(outputFD, terminalSupportsColors(outputFD)) {}
LogConnectionConsole(int outputFD, bool color)
Constructor.
static bool terminalSupportsColors(int fileDescriptor)
Returns true if the given file descriptor is a tty and supports colors.

◆ ~LogConnectionConsole()

~LogConnectionConsole ( )
override

Destructor.

Definition at line 30 of file LogConnectionConsole.cc.

31{
32 if (m_fd > 0) close(m_fd);
33}

Member Function Documentation

◆ finalizeOnAbort()

void finalizeOnAbort ( )
overridevirtual

Make sure output is flushed on abort.

Reimplemented from LogConnectionBase.

Definition at line 98 of file LogConnectionConsole.cc.

99{
100 // If python logging is enabled we need to give jupyter some time to flush
101 // the output as this happens only in the output thread. Seems flushing again is fine :D
103 boost::python::import("sys").attr("stdout").attr("flush")();
104 }
105}
static bool getPythonLoggingEnabled()
Check whether console logging via python is enabled.

◆ getEscapeNewlinesEnabled()

static bool getEscapeNewlinesEnabled ( )
inlinestatic

Check whether we want to escape newlines on console.

Definition at line 60 of file LogConnectionConsole.h.

static bool s_escapeNewlinesEnabled
Flag to indicate whether newlines should be replaced by ' ' in the output.

◆ getPythonLoggingEnabled()

static bool getPythonLoggingEnabled ( )
inlinestatic

Check whether console logging via python is enabled.

Definition at line 56 of file LogConnectionConsole.h.

56{ return s_pythonLoggingEnabled; }
static bool s_pythonLoggingEnabled
Flag to indicate whether log messages should be sent to python sys.stdout.

◆ isConnected()

bool isConnected ( )
overridevirtual

Returns true if the connection to the io stream could be established.

Implements LogConnectionBase.

Definition at line 35 of file LogConnectionConsole.cc.

36{
37 return s_pythonLoggingEnabled || m_fd >= 0;
38}

◆ sendMessage()

bool sendMessage ( const LogMessage message)
overridevirtual

Sends a log message.

Parameters
messageThe log message object.
Returns
true if the message could be send.

Implements LogConnectionBase.

Definition at line 64 of file LogConnectionConsole.cc.

65{
66 if (!isConnected()) return false;
67 // format message
68 std::stringstream stream;
69 if (m_color) {
70 const std::string color_str[] = {
71 "\x1b[32m", // Debug : green
72 "", // Info : terminal default
73 "\x1b[34m", // Result : blue
74 "\x1b[33m", // Warning: yellow
75 "\x1b[31m", // Error : red
76 "\x1b[07m\x1b[31m" // Fatal : red reversed
77 };
78 const std::string& c{color_str[message.getLogLevel()]};
79 stream << c;
80 }
81 stream << message;
82 std::string messagestr = stream.str();
84 // remove trailing whitespace
85 boost::trim_right_if(messagestr, boost::is_any_of(" \t\n\r"));
86 // escape all remaining newlines
87 boost::replace_all(messagestr, "\n", "\\n");
88 // and make sure we end in an actual newline
89 messagestr += "\n";
90 }
91 if (m_color) {
92 messagestr += "\x1b[m";
93 }
94 write(messagestr);
95 return true;
96}
bool isConnected() override
Returns true if the connection to the io stream could be established.
void write(const std::string &message)
Send a preformatted string message to the connected output.

◆ setEscapeNewlinesEnabled()

static void setEscapeNewlinesEnabled ( bool  enabled)
inlinestatic

Set whether we want to escape newlines on console.

Definition at line 62 of file LogConnectionConsole.h.

62{ s_escapeNewlinesEnabled = enabled; }

◆ setPythonLoggingEnabled()

static void setPythonLoggingEnabled ( bool  enabled)
inlinestatic

Set whether console logging via pyhthon is enabled.

Definition at line 58 of file LogConnectionConsole.h.

58{ s_pythonLoggingEnabled = enabled; }

◆ terminalSupportsColors()

bool terminalSupportsColors ( int  fileDescriptor)
static

Returns true if the given file descriptor is a tty and supports colors.

Definition at line 40 of file LogConnectionConsole.cc.

41{
42 //enable color for TTYs with color support (list taken from gtest)
43 const bool isTTY = isatty(fileDescriptor);
44 const std::string termName = getenv("TERM") ? getenv("TERM") : "";
45 const bool useColor = isTTY and
46 (termName == "xterm" or termName == "xterm-color" or termName == "xterm-256color" or
47 termName == "sceen" or termName == "screen-256color" or termName == "tmux" or
48 termName == "tmux-256color" or termName == "rxvt-unicode" or
49 termName == "rxvt-unicode-256color" or termName == "linux" or termName == "cygwin");
50 return useColor;
51}

◆ write()

void write ( const std::string &  message)

Send a preformatted string message to the connected output.

Definition at line 53 of file LogConnectionConsole.cc.

54{
56 auto pymessage = boost::python::import("sys").attr("stdout");
57 pymessage.attr("write")(message);
58 pymessage.attr("flush")();
59 } else {
60 ::write(m_fd, message.data(), message.size());
61 }
62}

Member Data Documentation

◆ m_color

bool m_color
private

Flag for color output.

Definition at line 67 of file LogConnectionConsole.h.

◆ m_fd

int m_fd
private

The output stream used for sending the log message.

Definition at line 66 of file LogConnectionConsole.h.

◆ s_escapeNewlinesEnabled

bool s_escapeNewlinesEnabled {false}
staticprivate

Flag to indicate whether newlines should be replaced by '
' in the output.

Definition at line 69 of file LogConnectionConsole.h.

◆ s_pythonLoggingEnabled

bool s_pythonLoggingEnabled {false}
staticprivate

Flag to indicate whether log messages should be sent to python sys.stdout.

Definition at line 68 of file LogConnectionConsole.h.


The documentation for this class was generated from the following files: