CLog.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //-----------------------------------------------------------------------------
  2. // (c) 2006 by Basler Vision Technologies
  3. // Section: Vision Components
  4. // Project: GenApi
  5. // Author: Fritz Dierks
  6. // $Header$
  7. //
  8. // License: This file is published under the license of the EMVA GenICam Standard Group.
  9. // A text file describing the legal terms is included in your installation as 'GenICam_license.pdf'.
  10. // If for some reason you are missing this file please contact the EMVA or visit the website
  11. // (http://www.genicam.org) for a full copy.
  12. //
  13. // THIS SOFTWARE IS PROVIDED BY THE EMVA GENICAM STANDARD GROUP "AS IS"
  14. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE EMVA GENICAM STANDARD GROUP
  17. // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  20. // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. // POSSIBILITY OF SUCH DAMAGE.
  24. //-----------------------------------------------------------------------------
  25. /**
  26. \file
  27. \brief Implementation of CLog.
  28. \ingroup Log_PublicUtilities
  29. */
  30. #ifndef LOG_CLOG_H_
  31. #define LOG_CLOG_H_
  32. #pragma warning (push, 3)
  33. #pragma warning(disable:4706) // assignment within conditional expression
  34. #include <log4cpp/Portability.hh>
  35. #include <log4cpp/Priority.hh>
  36. namespace LOG4CPP_NS
  37. {
  38. class Category;
  39. class Appender;
  40. }
  41. #include <stdio.h>
  42. #include <Base/GCBase.h>
  43. #include <Log/LogDll.h>
  44. namespace GENICAM_NAMESPACE
  45. {
  46. /**
  47. \brief Helper class encapsulating log4cpp
  48. \ingroup Log_PublicUtilities
  49. */
  50. class LOG_DECL CLog
  51. {
  52. public:
  53. //! Retrieves the root category
  54. static LOG4CPP_NS::Category& GetRootLogger( void );
  55. //! Retrieves (and if necessary create) a category by name
  56. static LOG4CPP_NS::Category& GetLogger( const gcstring &LoggerName );
  57. static LOG4CPP_NS::Category& GetLogger( const char LoggerName[] );
  58. //! Checks if a category exists
  59. static bool Exists( const gcstring &LoggerName );
  60. static bool Exists( const char LoggerName[] );
  61. //! Push nested diagnostic context
  62. static void PushNDC( const gcstring &ContextName );
  63. static void PushNDC( const char ContextName[] );
  64. //! Pop nested diagnostic context
  65. static void PopNDC( void );
  66. //! initializes log4cpp
  67. static void Initialize( void );
  68. //! de-initializes log4cpp
  69. static void ShutDown( void );
  70. //! Configures log4cpp to output messages >=ERROR on the Windows debug console
  71. static void ConfigureDefault();
  72. //! Configures log4cpp from a file
  73. static bool ConfigureFromFile( const gcstring &FileName );
  74. static bool ConfigureFromFile( const char FileName[] );
  75. //! Configures log4cpp from a file whose name is given by the environment variable GENICAM_LOG_CONFIG_VERSION
  76. static bool ConfigureFromEnvironment( void );
  77. //! Configures log4cpp from a string
  78. static bool ConfigureFromString( const gcstring &ConfigData );
  79. static bool ConfigureFromString( const char ConfigData[] );
  80. //! removes all appenders from all existing categories
  81. static void RemoveAllAppenders(void);
  82. //! Creates a new file appender (used by some test modules)
  83. static LOG4CPP_NS::Appender *CreateFileAppender( const gcstring &aName, const gcstring &aPath, bool aAppend = false, const gcstring &aPattern = "" );
  84. //! Add/remove an appender to/from a category
  85. static void AddAppender( LOG4CPP_NS::Category *aCategory, LOG4CPP_NS::Appender *aAppender );
  86. static void RemoveAppender( LOG4CPP_NS::Category *aCategory, LOG4CPP_NS::Appender *aAppender );
  87. //! Set priority for a category
  88. static void SetPriorityInfo( LOG4CPP_NS::Category *aCategory );
  89. static void SetPriorityError( LOG4CPP_NS::Category *aCategory );
  90. //! Check whether logging is possible and enabled for a specific category
  91. static bool IsInfoEnabled( LOG4CPP_NS::Category *aCategory );
  92. static bool IsWarnEnabled( LOG4CPP_NS::Category *aCategory );
  93. static bool IsDebugEnabled( LOG4CPP_NS::Category *aCategory );
  94. //! Logging functions
  95. static void LogPush( LOG4CPP_NS::Category *aCategory, LOG4CPP_NS::Priority::Value aPriority, const char *aStringFormat, ... );
  96. static void LogPop( LOG4CPP_NS::Category *aCategory, LOG4CPP_NS::Priority::Value aPriority, const char *aStringFormat, ... );
  97. static void Log( LOG4CPP_NS::Category *aCategory, LOG4CPP_NS::Priority::Value aPriority, const char *aStringFormat, ... );
  98. static void LogVA( LOG4CPP_NS::Category *aCategory, LOG4CPP_NS::Priority::Value aPriority, const char *aStringFormat, va_list arg );
  99. private:
  100. //! Makes sure log4cpp has been found.
  101. static void MakeSureLoggerHasBeenFound( void );
  102. //! Reference counter for Initialize/Shutdown
  103. static int g_RefCount;
  104. //! A wrapper which bridges log4cpp and this class.
  105. static const void *g_pLog4cpp;
  106. //! A type corresponds to a library handle which can be platform specific.
  107. #if defined (_WIN32)
  108. typedef HMODULE lib_handle_t;
  109. #else
  110. typedef void * lib_handle_t;
  111. #endif
  112. //! A handle to an associated logger library.
  113. static lib_handle_t g_pLibHandle;
  114. //! Opens a library.
  115. static lib_handle_t OpenLibrary( const gcstring Name );
  116. //! Finds a symbol and return its pointer.
  117. static void *FindSymbol( lib_handle_t Handle, const gcstring Name );
  118. //! A truth value of a proposition "Has found log4cpp."
  119. static bool g_HasFoundLogger;
  120. };
  121. }
  122. // Logging macros (can be replaced by real functions for compilers not supporting it?)
  123. #define GCLOGINFO( cat, ... ) if(cat != NULL) GENICAM_NAMESPACE::CLog::Log( cat, LOG4CPP_NS::Priority::INFO, ##__VA_ARGS__ )
  124. #define GCLOGINFOPUSH( cat, ... ) if(cat != NULL) GENICAM_NAMESPACE::CLog::LogPush( cat, LOG4CPP_NS::Priority::INFO, ##__VA_ARGS__ )
  125. #define GCLOGINFOPOP( cat, ... ) if(cat != NULL) GENICAM_NAMESPACE::CLog::LogPop( cat, LOG4CPP_NS::Priority::INFO, ##__VA_ARGS__ )
  126. #define GCLOGWARN( cat, ... ) if(cat != NULL) GENICAM_NAMESPACE::CLog::Log( cat, LOG4CPP_NS::Priority::WARN, ##__VA_ARGS__ )
  127. #define GCLOGERROR( cat, ... ) if(cat != NULL) GENICAM_NAMESPACE::CLog::Log( cat, LOG4CPP_NS::Priority::ERROR, ##__VA_ARGS__ )
  128. #define GCLOGDEBUG( cat, ... ) if(cat != NULL) GENICAM_NAMESPACE::CLog::Log( cat, LOG4CPP_NS::Priority::DEBUG, ##__VA_ARGS__ )
  129. #endif // LOG_CLOG_H_