diff --git a/src/Base/Console.cpp b/src/Base/Console.cpp index e68f1cb353..d214ced15c 100644 --- a/src/Base/Console.cpp +++ b/src/Base/Console.cpp @@ -218,9 +218,8 @@ bool ConsoleSingleton::IsMsgTypeEnabled(const char* sObs, FreeCAD_ConsoleMsgType return false; } } - else { - return false; - } + + return false; } void ConsoleSingleton::SetConnectionMode(ConnectionMode mode) diff --git a/src/Base/Console.h b/src/Base/Console.h index a4299e6e16..6530de9f96 100644 --- a/src/Base/Console.h +++ b/src/Base/Console.h @@ -458,215 +458,215 @@ typedef unsigned int ConsoleMsgFlags; namespace Base { #ifndef FC_LOG_NO_TIMING - inline FC_DURATION GetDuration(FC_TIME_POINT &t) - { - auto tnow = std::chrono::FC_TIME_CLOCK::now(); - auto d = std::chrono::duration_cast(tnow-t); - t = tnow; - return d; - } +inline FC_DURATION GetDuration(FC_TIME_POINT &t) +{ + auto tnow = std::chrono::FC_TIME_CLOCK::now(); + auto d = std::chrono::duration_cast(tnow-t); + t = tnow; + return d; +} #endif - /** Used to identify log level*/ - enum class LogStyle{ - Warning, - Message, - Error, - Log +/** Used to identify log level*/ +enum class LogStyle{ + Warning, + Message, + Error, + Log +}; + +/** The Logger Interface + * This class describes an Interface for logging within FreeCAD. If you want to add a new + * "sink" to FreeCAD's logging mechanism, then inherit this class. You'll also need to + * register your derived class with ConsoleSingleton. + * + * @see ConsoleSingleton + */ +class BaseExport ILogger +{ +public: + ILogger() + :bErr(true),bMsg(true),bLog(true),bWrn(true){} + virtual ~ILogger() = 0; + + /** Used to send a Log message at the given level. + */ + virtual void SendLog(const std::string& msg, LogStyle level) = 0; + + virtual const char *Name(){return nullptr;} + bool bErr,bMsg,bLog,bWrn; +}; + + +/** The console class + * This class manage all the stdio stuff. This includes + * Messages, Warnings, Log entries and Errors. The incoming + * Messages are distributed with the FCConsoleObserver. The + * FCConsole class itself makes no IO, it's more like a manager. + * \par + * ConsoleSingleton is a singleton! That means you can access the only + * instance of the class from every where in c++ by simply using: + * \code + * #include + * Base::Console().Log("Stage: %d",i); + * \endcode + * \par + * ConsoleSingleton is able to switch between several modes to, e.g. switch + * the logging on or off, or treat Warnings as Errors, and so on... + * @see ConsoleObserver + */ +class BaseExport ConsoleSingleton +{ + +public: + static const unsigned int BufferSize = 4024; + // exported functions goes here +++++++++++++++++++++++++++++++++++++++ + /// Prints a Message + virtual void Message ( const char * pMsg, ... ); + /// Prints a warning Message + virtual void Warning ( const char * pMsg, ... ); + /// Prints a error Message + virtual void Error ( const char * pMsg, ... ); + /// Prints a log Message + virtual void Log ( const char * pMsg, ... ); + + // observer processing + void NotifyMessage(const char *sMsg); + void NotifyWarning(const char *sMsg); + void NotifyError (const char *sMsg); + void NotifyLog (const char *sMsg); + + /// Attaches an Observer to FCConsole + void AttachObserver(ILogger *pcObserver); + /// Detaches an Observer from FCConsole + void DetachObserver(ILogger *pcObserver); + /// enumaration for the console modes + enum ConsoleMode{ + Verbose = 1, // suppress Log messages + }; + enum ConnectionMode { + Direct = 0, + Queued =1 }; - /** The Logger Interface - * This class describes an Interface for logging within FreeCAD. If you want to add a new - * "sink" to FreeCAD's logging mechanism, then inherit this class. You'll also need to - * register your derived class with ConsoleSingleton. - * - * @see ConsoleSingleton - */ - class BaseExport ILogger - { - public: - ILogger() - :bErr(true),bMsg(true),bLog(true),bWrn(true){}; - virtual ~ILogger() = 0; - - /** Used to send a Log message at the given level. - */ - virtual void SendLog(const std::string& msg, LogStyle level) = 0; - - virtual const char *Name(void){return 0L;} - bool bErr,bMsg,bLog,bWrn; + enum FreeCAD_ConsoleMsgType { + MsgType_Txt = 1, + MsgType_Log = 2, // ConsoleObserverStd sends this and higher to stderr + MsgType_Wrn = 4, + MsgType_Err = 8 }; + /// Change mode + void SetConsoleMode(ConsoleMode m); + /// Change mode + void UnsetConsoleMode(ConsoleMode m); + /// Enables or disables message types of a certain console observer + ConsoleMsgFlags SetEnabledMsgType(const char* sObs, ConsoleMsgFlags type, bool b); + /// Enables or disables message types of a certain console observer + bool IsMsgTypeEnabled(const char* sObs, FreeCAD_ConsoleMsgType type) const; + void SetConnectionMode(ConnectionMode mode); - /** The console class - * This class manage all the stdio stuff. This includes - * Messages, Warnings, Log entries and Errors. The incoming - * Messages are distributed with the FCConsoleObserver. The - * FCConsole class itself makes no IO, it's more like a manager. - * \par - * ConsoleSingleton is a singleton! That means you can access the only - * instance of the class from every where in c++ by simply using: - * \code - * #include - * Base::Console().Log("Stage: %d",i); - * \endcode - * \par - * ConsoleSingleton is able to switch between several modes to, e.g. switch - * the logging on or off, or treat Warnings as Errors, and so on... - * @see ConsoleObserver - */ - class BaseExport ConsoleSingleton - { + int *GetLogLevel(const char *tag, bool create=true); - public: - static const unsigned int BufferSize = 4024; - // exported functions goes here +++++++++++++++++++++++++++++++++++++++ - /// Prints a Message - virtual void Message ( const char * pMsg, ... ); - /// Prints a warning Message - virtual void Warning ( const char * pMsg, ... ); - /// Prints a error Message - virtual void Error ( const char * pMsg, ... ); - /// Prints a log Message - virtual void Log ( const char * pMsg, ... ); - - // observer processing - void NotifyMessage(const char *sMsg); - void NotifyWarning(const char *sMsg); - void NotifyError (const char *sMsg); - void NotifyLog (const char *sMsg); - - /// Attaches an Observer to FCConsole - void AttachObserver(ILogger *pcObserver); - /// Detaches an Observer from FCConsole - void DetachObserver(ILogger *pcObserver); - /// enumaration for the console modes - enum ConsoleMode{ - Verbose = 1, // suppress Log messages - }; - enum ConnectionMode { - Direct = 0, - Queued =1 - }; - - enum FreeCAD_ConsoleMsgType { - MsgType_Txt = 1, - MsgType_Log = 2, // ConsoleObserverStd sends this and higher to stderr - MsgType_Wrn = 4, - MsgType_Err = 8 - }; - - /// Change mode - void SetConsoleMode(ConsoleMode m); - /// Change mode - void UnsetConsoleMode(ConsoleMode m); - /// Enables or disables message types of a certain console observer - ConsoleMsgFlags SetEnabledMsgType(const char* sObs, ConsoleMsgFlags type, bool b); - /// Enables or disables message types of a certain console observer - bool IsMsgTypeEnabled(const char* sObs, FreeCAD_ConsoleMsgType type) const; - void SetConnectionMode(ConnectionMode mode); - - int *GetLogLevel(const char *tag, bool create=true); - - void SetDefaultLogLevel(int level) { - _defaultLogLevel = level; - } - - inline int LogLevel(int level) const{ - return level<0?_defaultLogLevel:level; - } - - /// singleton - static ConsoleSingleton &Instance(void); - - // retrieval of an observer by name - ILogger *Get(const char *Name) const; - - static PyMethodDef Methods[]; - - void Refresh(); - void EnableRefresh(bool enable); - - protected: - // python exports goes here +++++++++++++++++++++++++++++++++++++++++++ - // static python wrapper of the exported functions - static PyObject *sPyLog (PyObject *self,PyObject *args); - static PyObject *sPyMessage (PyObject *self,PyObject *args); - static PyObject *sPyWarning (PyObject *self,PyObject *args); - static PyObject *sPyError (PyObject *self,PyObject *args); - static PyObject *sPySetStatus(PyObject *self,PyObject *args); - static PyObject *sPyGetStatus(PyObject *self,PyObject *args); - - bool _bVerbose; - bool _bCanRefresh; - ConnectionMode connectionMode; - - // Singleton! - ConsoleSingleton(void); - virtual ~ConsoleSingleton(); - - private: - // singleton - static void Destruct(void); - static ConsoleSingleton *_pcSingleton; - - // observer list - std::set _aclObservers; - - std::map _logLevels; - int _defaultLogLevel; - - friend class ConsoleOutput; - }; - - /** Access to the Console - * This method is used to gain access to the one and only instance of - * the ConsoleSingleton class. - */ - inline ConsoleSingleton &Console(void){ - return ConsoleSingleton::Instance(); + void SetDefaultLogLevel(int level) { + _defaultLogLevel = level; } - class BaseExport ConsoleRefreshDisabler { - public: - ConsoleRefreshDisabler() { - Console().EnableRefresh(false); - } + inline int LogLevel(int level) const{ + return level<0?_defaultLogLevel:level; + } - ~ConsoleRefreshDisabler() { - Console().EnableRefresh(true); - } - }; + /// singleton + static ConsoleSingleton &Instance(); + + // retrieval of an observer by name + ILogger *Get(const char *Name) const; + + static PyMethodDef Methods[]; + + void Refresh(); + void EnableRefresh(bool enable); + +protected: + // python exports goes here +++++++++++++++++++++++++++++++++++++++++++ + // static python wrapper of the exported functions + static PyObject *sPyLog (PyObject *self,PyObject *args); + static PyObject *sPyMessage (PyObject *self,PyObject *args); + static PyObject *sPyWarning (PyObject *self,PyObject *args); + static PyObject *sPyError (PyObject *self,PyObject *args); + static PyObject *sPySetStatus(PyObject *self,PyObject *args); + static PyObject *sPyGetStatus(PyObject *self,PyObject *args); + + bool _bVerbose; + bool _bCanRefresh; + ConnectionMode connectionMode; + + // Singleton! + ConsoleSingleton(); + virtual ~ConsoleSingleton(); + +private: + // singleton + static void Destruct(); + static ConsoleSingleton *_pcSingleton; + + // observer list + std::set _aclObservers; + + std::map _logLevels; + int _defaultLogLevel; + + friend class ConsoleOutput; +}; + +/** Access to the Console + * This method is used to gain access to the one and only instance of + * the ConsoleSingleton class. + */ +inline ConsoleSingleton &Console(){ + return ConsoleSingleton::Instance(); +} + +class BaseExport ConsoleRefreshDisabler { +public: + ConsoleRefreshDisabler() { + Console().EnableRefresh(false); + } + + ~ConsoleRefreshDisabler() { + Console().EnableRefresh(true); + } +}; - /** LogLevel helper class */ - class BaseExport LogLevel { - public: - std::string tag; - int &lvl; - bool print_tag; - int print_src; - bool print_time; - bool add_eol; - bool refresh; +/** LogLevel helper class */ +class BaseExport LogLevel { +public: + std::string tag; + int &lvl; + bool print_tag; + int print_src; + bool print_time; + bool add_eol; + bool refresh; - LogLevel(const char *tag, bool print_tag=true, int print_src=0, - bool print_time=false, bool add_eol=true, bool refresh=false) - :tag(tag),lvl(*Console().GetLogLevel(tag)) - ,print_tag(print_tag),print_src(print_src),print_time(print_time) - ,add_eol(add_eol),refresh(refresh) - {} + LogLevel(const char *tag, bool print_tag=true, int print_src=0, + bool print_time=false, bool add_eol=true, bool refresh=false) + :tag(tag),lvl(*Console().GetLogLevel(tag)) + ,print_tag(print_tag),print_src(print_src),print_time(print_time) + ,add_eol(add_eol),refresh(refresh) + {} - bool isEnabled(int l) { - return l<=level(); - } + bool isEnabled(int l) { + return l<=level(); + } - int level() const { - return Console().LogLevel(lvl); - } + int level() const { + return Console().LogLevel(lvl); + } - std::stringstream &prefix(std::stringstream &str, const char *src, int line); - }; + std::stringstream &prefix(std::stringstream &str, const char *src, int line); +}; } // namespace Base