| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- /*
- * CLogger.h, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
- #pragma once
- class CGLogger;
- struct LogRecord;
- class ILogTarget;
- namespace ELogLevel
- {
- /**
- * The log level enum holds various log level definitions.
- */
- enum ELogLevel
- {
- NOT_SET = 0,
- TRACE,
- DEBUG,
- INFO,
- WARN,
- ERROR
- };
- }
- /**
- * The logger domain provides convenient access to super domains from a sub domain.
- */
- class DLL_LINKAGE CLoggerDomain
- {
- public:
- /**
- * Constructor.
- *
- * @param domain The domain name. Sub-domains can be specified by separating domains by a dot, e.g. "ai.battle". The global domain is named "global".
- */
- CLoggerDomain(const std::string & name);
- /**
- * Gets the parent logger domain.
- *
- * @return the parent logger domain or the same domain logger if this method has been called on the global domain
- */
- CLoggerDomain getParent() const;
- /**
- * Returns true if this domain is the global domain.
- *
- * @return true if this is the global domain or false if not
- */
- bool isGlobalDomain() const;
- /**
- * Gets the name of the domain.
- *
- * @return the name of the domain
- */
- std::string getName() const;
- /** Constant to the global domain name. */
- static const std::string DOMAIN_GLOBAL;
- private:
- /** The domain name. */
- std::string name;
- };
- class CGLogger;
- /**
- * The logger stream provides a stream-like way of logging messages.
- */
- class DLL_LINKAGE CLoggerStream
- {
- public:
- CLoggerStream(const CGLogger & logger, ELogLevel::ELogLevel level);
- ~CLoggerStream();
- template<typename T>
- CLoggerStream & operator<<(const T & data)
- {
- if(!sbuffer) sbuffer = new std::stringstream();
- (*sbuffer) << data;
- return *this;
- }
- private:
- const CGLogger & logger;
- ELogLevel::ELogLevel level;
- std::stringstream * sbuffer;
- };
- /**
- * The logger is used to log messages to certain targets of a specific domain/name. Temporary name is
- * CGLogger, should be renamed to CLogger after refactoring.
- */
- class DLL_LINKAGE CGLogger : public boost::noncopyable
- {
- public:
- /**
- * Gets a logger by domain.
- *
- * @param domain The logger domain.
- * @return the logger object
- */
- static CGLogger * getLogger(const CLoggerDomain & domain);
- /**
- * Gets the global logger which is the parent of all domain-specific loggers.
- *
- * @return the global logger object
- */
- static CGLogger * getGlobalLogger();
- /**
- * Logs a message with the trace level.
- *
- * @param message The message to log.
- */
- void trace(const std::string & message) const;
- CLoggerStream traceStream() const;
- /**
- * Logs a message with the debug level.
- *
- * @param message The message to log.
- */
- void debug(const std::string & message) const;
- CLoggerStream debugStream() const;
- /**
- * Logs a message with the info level.
- *
- * @param message The message to log.
- */
- void info(const std::string & message) const;
- CLoggerStream infoStream() const;
- /**
- * Logs a message with the warn level.
- *
- * @param message The message to log.
- */
- void warn(const std::string & message) const;
- CLoggerStream warnStream() const;
- /**
- * Logs a message with the error level.
- *
- * @param message The message to log.
- */
- void error(const std::string & message) const;
- CLoggerStream errorStream() const;
- /**
- * Logs a message of the given log level.
- *
- * @param level The log level of the message to log.
- * @param message The message to log.
- */
- inline void log(ELogLevel::ELogLevel level, const std::string & message) const;
- /**
- * Gets the log level applied for this logger. The default level for the root logger is INFO.
- *
- * @return the log level
- */
- inline ELogLevel::ELogLevel getLevel() const;
- /**
- * Sets the log level.
- *
- * @param level The log level.
- */
- void setLevel(ELogLevel::ELogLevel level);
- /**
- * Gets the logger domain.
- *
- * @return the domain of the logger
- */
- const CLoggerDomain & getDomain() const;
- /**
- * Adds a target to this logger and indirectly to all loggers which derive from this logger.
- * The logger holds strong-ownership of the target object.
- *
- * @param target The log target to add.
- */
- void addTarget(ILogTarget * target);
- /**
- * Destructor.
- */
- ~CGLogger();
- private:
- /**
- * Constructor.
- *
- * @param domain The domain of the logger.
- */
- explicit CGLogger(const CLoggerDomain & domain);
- /**
- * Gets the parent logger.
- *
- * @return the parent logger or nullptr if this is the root logger
- */
- CGLogger * getParent() const;
- /**
- * Gets the effective log level.
- *
- * @return the effective log level with respect to parent log levels
- */
- inline ELogLevel::ELogLevel getEffectiveLevel() const;
- /**
- * Calls all targets in the hierarchy to write the message.
- *
- * @param record The log record to write.
- */
- inline void callTargets(const LogRecord & record) const;
- /**
- * Gets all log targets attached to this logger.
- *
- * @return all log targets as a list
- */
- inline std::list<ILogTarget *> getTargets() const;
- /** The domain of the logger. */
- CLoggerDomain domain;
- /** A reference to the parent logger. */
- CGLogger * parent;
- /** The log level of the logger. */
- ELogLevel::ELogLevel level;
- /** A list of log targets. */
- std::list<ILogTarget *> targets;
- /** The shared mutex for providing synchronous thread-safe access to the logger. */
- mutable boost::shared_mutex mx;
- /** The unique mutex for providing thread-safe get logger access. */
- static boost::mutex smx;
- };
- //extern CLogger * logGlobal;
|