Belle II Software  release-05-02-19
LogConnectionConsole.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010-2018 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 
13 #include <framework/logging/LogConnectionBase.h>
14 #include <string>
15 
16 namespace Belle2 {
26  class LogConnectionConsole final: public LogConnectionBase {
27  public:
32  LogConnectionConsole(int outputFD, bool color);
33 
37  explicit LogConnectionConsole(int outputFD): LogConnectionConsole(outputFD, terminalSupportsColors(outputFD)) {}
38 
40  static bool terminalSupportsColors(int fileDescriptor);
41 
43  ~LogConnectionConsole() override;
44 
49  bool sendMessage(const LogMessage& message) override;
50 
52  void write(const std::string& message);
53 
55  bool isConnected() override;
56 
58  static bool getPythonLoggingEnabled() { return s_pythonLoggingEnabled; }
60  static void setPythonLoggingEnabled(bool enabled) { s_pythonLoggingEnabled = enabled; }
62  static bool getEscapeNewlinesEnabled() { return s_escapeNewlinesEnabled; }
64  static void setEscapeNewlinesEnabled(bool enabled) { s_escapeNewlinesEnabled = enabled; }
66  void finalizeOnAbort() override;
67  private:
68  int m_fd;
69  bool m_color;
70  static bool s_pythonLoggingEnabled;
71  static bool s_escapeNewlinesEnabled;
72  };
73 
75 } // end namespace Belle2
Belle2::LogConnectionConsole::terminalSupportsColors
static bool terminalSupportsColors(int fileDescriptor)
Returns true if the given file descriptor is a tty and supports colors.
Definition: LogConnectionConsole.cc:42
Belle2::LogConnectionConsole::finalizeOnAbort
void finalizeOnAbort() override
Make sure output is flushed on abort.
Definition: LogConnectionConsole.cc:100
Belle2::LogConnectionConsole::m_fd
int m_fd
The output stream used for sending the log message.
Definition: LogConnectionConsole.h:76
Belle2::LogConnectionConsole::getEscapeNewlinesEnabled
static bool getEscapeNewlinesEnabled()
Check whether we want to escape newlines on console.
Definition: LogConnectionConsole.h:70
Belle2::LogConnectionConsole::LogConnectionConsole
LogConnectionConsole(int outputFD, bool color)
Constructor.
Definition: LogConnectionConsole.cc:25
Belle2::LogConnectionConsole::~LogConnectionConsole
~LogConnectionConsole() override
Destructor.
Definition: LogConnectionConsole.cc:32
Belle2::LogConnectionConsole::sendMessage
bool sendMessage(const LogMessage &message) override
Sends a log message.
Definition: LogConnectionConsole.cc:66
Belle2::LogConnectionConsole::isConnected
bool isConnected() override
Returns true if the connection to the io stream could be established.
Definition: LogConnectionConsole.cc:37
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::LogConnectionConsole::s_escapeNewlinesEnabled
static bool s_escapeNewlinesEnabled
Flag to indicate whether newlines should be replaced by ' ' in the output.
Definition: LogConnectionConsole.h:79
Belle2::LogConnectionConsole::setEscapeNewlinesEnabled
static void setEscapeNewlinesEnabled(bool enabled)
Set whether we want to escape newlines on console.
Definition: LogConnectionConsole.h:72
Belle2::LogConnectionConsole::write
void write(const std::string &message)
Send a preformatted string message to the connected output.
Definition: LogConnectionConsole.cc:55
Belle2::LogConnectionConsole::setPythonLoggingEnabled
static void setPythonLoggingEnabled(bool enabled)
Set whether console logging via pyhthon is enabled.
Definition: LogConnectionConsole.h:68
Belle2::LogConnectionConsole::getPythonLoggingEnabled
static bool getPythonLoggingEnabled()
Check whether console logging via python is enabled.
Definition: LogConnectionConsole.h:66
Belle2::LogConnectionConsole::s_pythonLoggingEnabled
static bool s_pythonLoggingEnabled
Flag to indicate whether log messages should be sent to python sys.stdout.
Definition: LogConnectionConsole.h:78
Belle2::LogConnectionConsole::m_color
bool m_color
Flag for color output.
Definition: LogConnectionConsole.h:77