Teamcenter C++ API Reference  2312
mld/logging/Logger.hxx
Go to the documentation of this file.
1 // Copyright 2022 Siemens Digital Industries Software
2 // ==================================================
3 // Copyright 2011.
4 // Siemens Product Lifecycle Management Software Inc.
5 // All Rights Reserved.
6 // ==================================================
7 // Copyright 2022 Siemens Digital Industries Software
82 #ifndef TEAMCENTER_LOGGING_LOGGER_HXX
83 #define TEAMCENTER_LOGGING_LOGGER_HXX
84 
85 #include <string>
86 #include <vector>
87 #include <cstdarg>
88 #include <string_view>
89 #include <common/tc_deprecation_macros.h>
90 
91 #include <mld/libmld_exports.h>
92 
93 // The string representation of the allowed appenders.
94 #define APPENDER_SYSLOG_STR "syslog"
95 #define APPENDER_STDOUT_STR "stdout"
96 #define APPENDER_STDERR_STR "stderr"
97 
98 namespace Teamcenter
99 {
100  namespace Logging
101  {
102  class MLD_API Logger
103  {
104  public:
105  friend class LoggerTree;
106  friend class Node;
107 
111  enum level{LOG_TRACE=1,LOG_DEBUG=2,LOG_INFO=3,LOG_WARN=4,LOG_ERROR=5,LOG_FATAL=6,LOG_NOTE=7,LOG_OFF=8};
112 
116  enum appender{APPENDER_SYSLOG=0, APPENDER_STDOUT=1, APPENDER_STDERR=2};
117 
123  std::string getLoggerName() const { return loggerName; };
124 
130  void setLogLevel(level lvl);
136  level getLogLevel() const { return logLevel; };
137 
143  bool isTraceEnabled() const { return ( LOG_TRACE >= logLevel ); };
149  bool isDebugEnabled() const { return ( LOG_DEBUG >= logLevel ); };
150 
156  bool isInfoEnabled() const { return ( LOG_INFO >= logLevel ); };
157 
163  bool isWarnEnabled() const { return ( LOG_WARN >= logLevel ); };
164 
170  bool isErrorEnabled() const { return ( LOG_ERROR >= logLevel ); };
171 
177  bool isFatalEnabled() const { return ( LOG_FATAL >= logLevel ); };
178 
184  void fatal(const std::string &msg) { log(LOG_FATAL, msg); };
185 
192  void fatal(const int errorCode, const std::string &msg) { log(LOG_FATAL, errorCode, msg); };
193 
202  void fatal( const char* file, const int line, const int errorCode, const std::string &msg) { log(LOG_FATAL, file, line, errorCode, msg); };
203 
214  void fatal( const char* file, const int line, const int errorCode, const char* control_string, ... );
215 
221  void error(const std::string &msg) { log(LOG_ERROR, msg); };
222 
229  void error(const int errorCode, const std::string &msg) { log(LOG_ERROR, errorCode, msg); };
230 
240  void error( const char* file, const int line, const int errorCode, const std::string &msg) { log(LOG_ERROR, file, line, errorCode, msg); };
251  void error( const char* file, const int line, const int errorCode, const char* control_string, ... );
252 
258  void warn(const std::string &msg) { log(LOG_WARN, msg); };
259 
266  void warn(const int errorCode, const std::string &msg) { log(LOG_WARN, errorCode, msg); };
267 
276  void warn ( const char* file, const int line, const int errorCode, const std::string &msg) { log(LOG_WARN, file, line, errorCode, msg); };
277 
288  void warn( const char* file, const int line, const int errorCode, const char* control_string, ... );
289 
295  void info(const std::string &msg) { log(LOG_INFO, msg); };
296 
304  void info ( const char* file, const int line, const std::string &msg) { log(LOG_INFO, file, line, msg); };
305 
314  void info ( const char* file, const int line, const int errorCode, const std::string &msg) { log(LOG_INFO, file, line, errorCode, msg); };
315 
325  void info( const char* file, const int line, const char *control_string, ... );
326 
332  void debug(const std::string &msg) { log(LOG_DEBUG, msg); };
333 
340  void debug( const int errorCode, const std::string& msg ) { log( LOG_DEBUG, errorCode, msg ); };
341 
349  void debug( const char* file, const int line, const std::string &msg) { log(LOG_DEBUG, file, line, msg); };
350 
359  void debug ( const char* file, const int line, const int errorCode, const std::string &msg) { log(LOG_DEBUG, file, line, errorCode, msg); };
360 
370  void debug( const char* file, const int line, const char* control_string, ... );
371 
382  void debug( const char* file, const int line, const int errorCode, const char* control_string, ... );
388  void trace(const std::string &msg) { log(LOG_TRACE, msg); };
389 
397  void trace( const char* file, const int line, const std::string &msg) { log(LOG_TRACE, file, line, msg); };
398 
407  void trace ( const char* file, const int line, const int errorCode, const std::string &msg) { log(LOG_TRACE, file, line, errorCode, msg); };
408 
418  void trace( const char* file, const int line, const char *control_string, ... );
419 
430  TC_DEPRECATED_NO_REPLACEMENT( "11.4", "Teamcenter::Logging::Logger::note" )
431  void note(const std::string &msg) { log(LOG_NOTE, msg); };
432 
447  TC_DEPRECATED_NO_REPLACEMENT( "11.4", "Teamcenter::Logging::Logger::note" )
448  void note( const char* file, const int line, const char *control_string, ... );
449 
457  void log( const Logger::level lvl, const std::string& msg );
458 
467  void log( const Logger::level lvl, const std::string errorCode, const std::string& msg );
468 
477  void log( const Logger::level lvl, const int errorCode, const std::string& msg );
478 
491  void log( const Logger::level lvl, const char* file, const int line, const int errorCode, const std::string& msg );
492 
504  void log ( const Logger::level lvl, const char *file, const int line, const std::string& msg);
505 
517  void logNoStackTrace ( const Logger::level lvl, const char *file, const int line, const int errorCode, const std::string &msg);
518 
526  bool isEnabled (const Logger::level lvl) const { return ( lvl >= logLevel ); };
535  static Logger* getLogger(const std::string &key);
536 
545  void logf( const Logger::level lvl, const int errorCode, const char *control_string, ... ) ;
546 
557  void logf( const Logger::level lvl, const char *file, const int line, const int errorCode, const char *control_string, ... );
558 
567  void printf( const Logger::level lvl, const char *control_string, ... );
568 
576  void printf( const char *control_string, ... );
577 
583  void printString( std::string_view str ) const;
584 
590  static Logger* getLoggerObject(const std::string &key);
591 
592  // this method sets the given name to the logger.
593  void setLoggerName(const std::string &name) { loggerName=name; };
594 
595  //Returns all the child loggers for a logger.
596  static std::vector<Logger *> getListOfLoggers(std::string key);
597 
598  // returns the log level inheritance flag
599  bool isLogLevelInherited () const { return logLevelInherited; };
600 
601  // sets the log level inheritance flag
602  void setInheritanceLevel ( bool isInherited) { logLevelInherited = isInherited; };
603 
604  // set the logLevelInherited member variable
605  // if set to true log level will be inherited from parent.
606  // if set to false log level will not be inherited from parent.
607  void setLogLevelInheritance (bool isInherited);
608 
617  void logAlways( const Logger::level lvl, int errorCode, std::string_view msg );
618 
631  void logAlways( const Logger::level lvl, const char* file, const int line, const int errorCode, std::string_view msg );
632 
633  private:
634 
635  // unique name for each logger.
636  std::string loggerName;
637 
638  // logger level
640 
641  // We used a static data member to remember the choice of the appender, which is defined in the logger property file: logger.properties and logger.debug.properties
642  // Please see documentation on environment variable 'UGII_CHECKING_LEVEL' on when each file is used for logging.
643  // The syntax is as the following:
644  // logging.rootLogger={INFO|DEBUG|OFF}{,syslog|stderr|stdout}
645  // If there is no appender specified, or if the value is invalid, 'syslog' will be the default appender. Currently we only support a single choice of appender defined inside the rootLogger.
646  // For example:
647  // "logging.rootLogger=INFO,stdout" will send output the the console window and not to the syslog.
648  // All loggers will inherit the same appender as the rootLogger.
649  // We may consider supporting any individual logger setting its own appender, and even allowing multiple appenders for a single loggers.
650  static int appender;
651 
652  // flag to check whether log level is inherited from parent.
654 
655  // Copy constructor and assignment operator are disabled and unimplemented
656  // Since a Logger need not exist outside a LoggerTree
657  Logger( const Logger& );
658  Logger& operator=( const Logger& );
659 
666  explicit Logger(const std::string &name);
667 
675  Logger(const std::string &name, level lvl);
676 
681  std::string getTime() const;
682 
683 
688  std::string formatLogEntry ( const Logger::level lvl, const std::string_view errCode ) const;
689  std::string formatLogEntry ( const Logger::level lvl ) const;
690  std::string formatLogEntry( const Logger::level lvl, const int errCode, bool includLoggerName ) const;
691 
695  FILE* getLogFilePointer() const;
696 
700  void writeToLogAndPublish( const std::string& str );
701 
702 
711  bool isErrorSuppression(const Logger::level lvl, const int errorCode);
712 
719  void processErrorSuppressionAndLog( std::string& logLine );
720 
725  void appendFileAndLine(std::string &str, const char* file, const int line) const;
726 
727  ~Logger();
728 
729  Logger();
730 
736  void setLogLevelAttribute(level lvl) { logLevel = lvl; logLevelInherited = false; };
737 
744  std::string substituteForInvalidArgs ( const char* control_string, va_list ap1 );
745 
755  int checkForNullPointer ( const char* control_string, va_list ap1);
756 
760  void logv( const Logger::level lvl, const char* file, const int line, const int errorCode, const char* control_string, std::va_list va );
761 
767  void stackTrace( const Logger::level lvl );
768 
778  void appendFileNameForCentralizedLogging(std::string &str) const;
779 
785  static std::string getLeafName(std::string absolutePath );
786  };
787  }
788 }
789 
790 #include <mld/libmld_undef.h>
791 #endif