Class Helpers

Synopsis

#include <src/easylogging++.h>

class Helpers : base::StaticClass

Description

Static helpers for developers.

Mentioned in

Inheritance

Ancestors: StaticClass

Methods

commandLineArgsReturns command line arguments (pointer) provided to easylogging++.
convertTemplateToStdStringConverts template to std::string - useful for loggable classes to log containers within log(std::ostream&) const.
crashAbortAbort due to crash with signal in parameter.
getThreadName
hasCustomFormatSpecifierReturns true if custom format specifier is installed.
installCustomFormatSpecifierInstalls user defined format specifier and handler.
installLogDispatchCallbackInstalls post log dispatch callback, this callback is triggered when log is dispatched.
installPerformanceTrackingCallbackInstalls post performance tracking callback, this callback is triggered when performance tracking is finished.
installPreRollOutCallbackInstalls pre rollout callback, this callback is triggered when log file is about to be rolled out (can be useful for backing up)
logCrashReasonLogs reason of crash as per sig.
logDispatchCallback

Mentioned in

performanceTrackingCallback

Mentioned in

reserveCustomFormatSpecifiersReserve space for custom format specifiers for performance.
setArgs overloadSets application arguments and figures out whats active for logging and whats not.
setCrashHandlerOverrides default crash handler and installs custom handler.
setStorageShares logging repository (base::Storage)
setThreadNameSets thread name for current thread. Requires std::thread.
storageReturns: Main storage repository
uninstallCustomFormatSpecifierUninstalls user defined format specifier and handler.
uninstallLogDispatchCallbackUninstalls log dispatch callback.
uninstallPerformanceTrackingCallbackUninstalls post performance tracking handler.
uninstallPreRollOutCallbackUninstalls pre rollout callback.
validateFileRolling

Mentioned in

Source

Lines 3657-3785 in src/easylogging++.h.

class Helpers : base::StaticClass {
 public:
  /// @brief Shares logging repository (base::Storage)
  static inline void setStorage(base::type::StoragePointer storage) {
    ELPP = storage;
  }
  /// @return Main storage repository
  static inline base::type::StoragePointer storage() {
    return ELPP;
  }
  /// @brief Sets application arguments and figures out whats active for logging and whats not.
  static inline void setArgs(int argc, char** argv) {
    ELPP->setApplicationArguments(argc, argv);
  }
  /// @copydoc setArgs(int argc, char** argv)
  static inline void setArgs(int argc, const char** argv) {
    ELPP->setApplicationArguments(argc, const_cast<char**>(argv));
  }
  /// @brief Sets thread name for current thread. Requires std::thread
  static inline void setThreadName(const std::string& name) {
    ELPP->setThreadName(name);
  }
  static inline std::string getThreadName() {
    return ELPP->getThreadName(base::threading::getCurrentThreadId());
  }
#if defined(ELPP_FEATURE_ALL) || defined(ELPP_FEATURE_CRASH_LOG)
  /// @brief Overrides default crash handler and installs custom handler.
  /// @param crashHandler A functor with no return type that takes single int argument.
  ///        Handler is a typedef with specification: void (*Handler)(int)
  static inline void setCrashHandler(const el::base::debug::CrashHandler::Handler& crashHandler) {
    el::elCrashHandler.setHandler(crashHandler);
  }
  /// @brief Abort due to crash with signal in parameter
  /// @param sig Crash signal
  static void crashAbort(int sig, const char* sourceFile = "", unsigned int long line = 0);
  /// @brief Logs reason of crash as per sig
  /// @param sig Crash signal
  /// @param stackTraceIfAvailable Includes stack trace if available
  /// @param level Logging level
  /// @param logger Logger to use for logging
  static void logCrashReason(int sig, bool stackTraceIfAvailable = false,
                             Level level = Level::Fatal, const char* logger = base::consts::kDefaultLoggerId);
#endif // defined(ELPP_FEATURE_ALL) || defined(ELPP_FEATURE_CRASH_LOG)
  /// @brief Installs pre rollout callback, this callback is triggered when log file is about to be rolled out
  ///        (can be useful for backing up)
  static inline void installPreRollOutCallback(const PreRollOutCallback& callback) {
    ELPP->setPreRollOutCallback(callback);
  }
  /// @brief Uninstalls pre rollout callback
  static inline void uninstallPreRollOutCallback(void) {
    ELPP->unsetPreRollOutCallback();
  }
  /// @brief Installs post log dispatch callback, this callback is triggered when log is dispatched
  template <typename T>
  static inline bool installLogDispatchCallback(const std::string& id) {
    return ELPP->installLogDispatchCallback<T>(id);
  }
  /// @brief Uninstalls log dispatch callback
  template <typename T>
  static inline void uninstallLogDispatchCallback(const std::string& id) {
    ELPP->uninstallLogDispatchCallback<T>(id);
  }
  template <typename T>
  static inline T* logDispatchCallback(const std::string& id) {
    return ELPP->logDispatchCallback<T>(id);
  }
#if defined(ELPP_FEATURE_ALL) || defined(ELPP_FEATURE_PERFORMANCE_TRACKING)
  /// @brief Installs post performance tracking callback, this callback is triggered when performance tracking is finished
  template <typename T>
  static inline bool installPerformanceTrackingCallback(const std::string& id) {
    return ELPP->installPerformanceTrackingCallback<T>(id);
  }
  /// @brief Uninstalls post performance tracking handler
  template <typename T>
  static inline void uninstallPerformanceTrackingCallback(const std::string& id) {
    ELPP->uninstallPerformanceTrackingCallback<T>(id);
  }
  template <typename T>
  static inline T* performanceTrackingCallback(const std::string& id) {
    return ELPP->performanceTrackingCallback<T>(id);
  }
#endif // defined(ELPP_FEATURE_ALL) || defined(ELPP_FEATURE_PERFORMANCE_TRACKING)
  /// @brief Converts template to std::string - useful for loggable classes to log containers within log(std::ostream&) const
  template <typename T>
  static std::string convertTemplateToStdString(const T& templ) {
    el::Logger* logger =
      ELPP->registeredLoggers()->get(el::base::consts::kDefaultLoggerId);
    if (logger == nullptr) {
      return std::string();
    }
    base::MessageBuilder b;
    b.initialize(logger);
    logger->acquireLock();
    b << templ;
#if defined(ELPP_UNICODE)
    std::string s = std::string(logger->stream().str().begin(), logger->stream().str().end());
#else
    std::string s = logger->stream().str();
#endif  // defined(ELPP_UNICODE)
    logger->stream().str(ELPP_LITERAL(""));
    logger->releaseLock();
    return s;
  }
  /// @brief Returns command line arguments (pointer) provided to easylogging++
  static inline const el::base::utils::CommandLineArgs* commandLineArgs(void) {
    return ELPP->commandLineArgs();
  }
  /// @brief Reserve space for custom format specifiers for performance
  /// @see std::vector::reserve
  static inline void reserveCustomFormatSpecifiers(std::size_t size) {
    ELPP->m_customFormatSpecifiers.reserve(size);
  }
  /// @brief Installs user defined format specifier and handler
  static inline void installCustomFormatSpecifier(const CustomFormatSpecifier& customFormatSpecifier) {
    ELPP->installCustomFormatSpecifier(customFormatSpecifier);
  }
  /// @brief Uninstalls user defined format specifier and handler
  static inline bool uninstallCustomFormatSpecifier(const char* formatSpecifier) {
    return ELPP->uninstallCustomFormatSpecifier(formatSpecifier);
  }
  /// @brief Returns true if custom format specifier is installed
  static inline bool hasCustomFormatSpecifier(const char* formatSpecifier) {
    return ELPP->hasCustomFormatSpecifier(formatSpecifier);
  }
  static inline void validateFileRolling(Logger* logger, Level level) {
    if (ELPP == nullptr || logger == nullptr) return;
    logger->m_typedConfigurations->validateFileRolling(level, ELPP->preRollOutCallback());
  }
};





Add Discussion

Log in to comment