| 1 | /*
|
|---|
| 2 | * synergy -- mouse and keyboard sharing utility
|
|---|
| 3 | * Copyright (C) 2002 Chris Schoeneman
|
|---|
| 4 | *
|
|---|
| 5 | * This package is free software; you can redistribute it and/or
|
|---|
| 6 | * modify it under the terms of the GNU General Public License
|
|---|
| 7 | * found in the file COPYING that should have accompanied this file.
|
|---|
| 8 | *
|
|---|
| 9 | * This package is distributed in the hope that it will be useful,
|
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 12 | * GNU General Public License for more details.
|
|---|
| 13 | */
|
|---|
| 14 |
|
|---|
| 15 | #ifndef CLOG_H
|
|---|
| 16 | #define CLOG_H
|
|---|
| 17 |
|
|---|
| 18 | #include "common.h"
|
|---|
| 19 | #include "IArchMultithread.h"
|
|---|
| 20 | #include "stdlist.h"
|
|---|
| 21 | #include <stdarg.h>
|
|---|
| 22 |
|
|---|
| 23 | #define CLOG (CLog::getInstance())
|
|---|
| 24 |
|
|---|
| 25 | class ILogOutputter;
|
|---|
| 26 |
|
|---|
| 27 | //! Logging facility
|
|---|
| 28 | /*!
|
|---|
| 29 | The logging class; all console output should go through this class.
|
|---|
| 30 | It supports multithread safe operation, several message priority levels,
|
|---|
| 31 | filtering by priority, and output redirection. The macros LOG() and
|
|---|
| 32 | LOGC() provide convenient access.
|
|---|
| 33 | */
|
|---|
| 34 | class CLog {
|
|---|
| 35 | public:
|
|---|
| 36 | //! Log levels
|
|---|
| 37 | /*!
|
|---|
| 38 | The logging priority levels in order of highest to lowest priority.
|
|---|
| 39 | */
|
|---|
| 40 | enum ELevel {
|
|---|
| 41 | kFATAL, //!< For fatal errors
|
|---|
| 42 | kERROR, //!< For serious errors
|
|---|
| 43 | kWARNING, //!< For minor errors and warnings
|
|---|
| 44 | kNOTE, //!< For messages about notable events
|
|---|
| 45 | kINFO, //!< For informational messages
|
|---|
| 46 | kDEBUG, //!< For important debugging messages
|
|---|
| 47 | kDEBUG1, //!< For more detailed debugging messages
|
|---|
| 48 | kDEBUG2 //!< For even more detailed debugging messages
|
|---|
| 49 | };
|
|---|
| 50 |
|
|---|
| 51 | ~CLog();
|
|---|
| 52 |
|
|---|
| 53 | //! @name manipulators
|
|---|
| 54 | //@{
|
|---|
| 55 |
|
|---|
| 56 | //! Add an outputter to the head of the list
|
|---|
| 57 | /*!
|
|---|
| 58 | Inserts an outputter to the head of the outputter list. When the
|
|---|
| 59 | logger writes a message, it goes to the outputter at the head of
|
|---|
| 60 | the outputter list. If that outputter's \c write() method returns
|
|---|
| 61 | true then it also goes to the next outputter, as so on until an
|
|---|
| 62 | outputter returns false or there are no more outputters. Outputters
|
|---|
| 63 | still in the outputter list when the log is destroyed will be
|
|---|
| 64 | deleted. If \c alwaysAtHead is true then the outputter is always
|
|---|
| 65 | called before all outputters with \c alwaysAtHead false and the
|
|---|
| 66 | return value of the outputter is ignored.
|
|---|
| 67 |
|
|---|
| 68 | By default, the logger has one outputter installed which writes to
|
|---|
| 69 | the console.
|
|---|
| 70 | */
|
|---|
| 71 | void insert(ILogOutputter* adopted,
|
|---|
| 72 | bool alwaysAtHead = false);
|
|---|
| 73 |
|
|---|
| 74 | //! Remove an outputter from the list
|
|---|
| 75 | /*!
|
|---|
| 76 | Removes the first occurrence of the given outputter from the
|
|---|
| 77 | outputter list. It does nothing if the outputter is not in the
|
|---|
| 78 | list. The outputter is not deleted.
|
|---|
| 79 | */
|
|---|
| 80 | void remove(ILogOutputter* orphaned);
|
|---|
| 81 |
|
|---|
| 82 | //! Remove the outputter from the head of the list
|
|---|
| 83 | /*!
|
|---|
| 84 | Removes and deletes the outputter at the head of the outputter list.
|
|---|
| 85 | This does nothing if the outputter list is empty. Only removes
|
|---|
| 86 | outputters that were inserted with the matching \c alwaysAtHead.
|
|---|
| 87 | */
|
|---|
| 88 | void pop_front(bool alwaysAtHead = false);
|
|---|
| 89 |
|
|---|
| 90 | //! Set the minimum priority filter.
|
|---|
| 91 | /*!
|
|---|
| 92 | Set the filter. Messages below this priority are discarded.
|
|---|
| 93 | The default priority is 4 (INFO) (unless built without NDEBUG
|
|---|
| 94 | in which case it's 5 (DEBUG)). setFilter(const char*) returns
|
|---|
| 95 | true if the priority \c name was recognized; if \c name is NULL
|
|---|
| 96 | then it simply returns true.
|
|---|
| 97 | */
|
|---|
| 98 | bool setFilter(const char* name);
|
|---|
| 99 | void setFilter(int);
|
|---|
| 100 |
|
|---|
| 101 | //@}
|
|---|
| 102 | //! @name accessors
|
|---|
| 103 | //@{
|
|---|
| 104 |
|
|---|
| 105 | //! Print a log message
|
|---|
| 106 | /*!
|
|---|
| 107 | Print a log message using the printf-like \c format and arguments
|
|---|
| 108 | preceded by the filename and line number. If \c file is NULL then
|
|---|
| 109 | neither the file nor the line are printed.
|
|---|
| 110 | */
|
|---|
| 111 | void print(const char* file, int line,
|
|---|
| 112 | const char* format, ...) const;
|
|---|
| 113 |
|
|---|
| 114 | //! Get the minimum priority level.
|
|---|
| 115 | int getFilter() const;
|
|---|
| 116 |
|
|---|
| 117 | //! Get the singleton instance of the log
|
|---|
| 118 | static CLog* getInstance();
|
|---|
| 119 |
|
|---|
| 120 | //@}
|
|---|
| 121 |
|
|---|
| 122 | private:
|
|---|
| 123 | CLog();
|
|---|
| 124 |
|
|---|
| 125 | void output(int priority, char* msg) const;
|
|---|
| 126 |
|
|---|
| 127 | private:
|
|---|
| 128 | typedef std::list<ILogOutputter*> COutputterList;
|
|---|
| 129 |
|
|---|
| 130 | static CLog* s_log;
|
|---|
| 131 |
|
|---|
| 132 | CArchMutex m_mutex;
|
|---|
| 133 | COutputterList m_outputters;
|
|---|
| 134 | COutputterList m_alwaysOutputters;
|
|---|
| 135 | int m_maxNewlineLength;
|
|---|
| 136 | int m_maxPriority;
|
|---|
| 137 | };
|
|---|
| 138 |
|
|---|
| 139 | /*!
|
|---|
| 140 | \def LOG(arg)
|
|---|
| 141 | Write to the log. Because macros cannot accept variable arguments, this
|
|---|
| 142 | should be invoked like so:
|
|---|
| 143 | \code
|
|---|
| 144 | LOG((CLOG_XXX "%d and %d are %s", x, y, x == y ? "equal" : "not equal"));
|
|---|
| 145 | \endcode
|
|---|
| 146 | In particular, notice the double open and close parentheses. Also note
|
|---|
| 147 | that there is no comma after the \c CLOG_XXX. The \c XXX should be
|
|---|
| 148 | replaced by one of enumerants in \c CLog::ELevel without the leading
|
|---|
| 149 | \c k. For example, \c CLOG_INFO. The special \c CLOG_PRINT level will
|
|---|
| 150 | not be filtered and is never prefixed by the filename and line number.
|
|---|
| 151 |
|
|---|
| 152 | If \c NOLOGGING is defined during the build then this macro expands to
|
|---|
| 153 | nothing. If \c NDEBUG is defined during the build then it expands to a
|
|---|
| 154 | call to CLog::print. Otherwise it expands to a call to CLog::printt,
|
|---|
| 155 | which includes the filename and line number.
|
|---|
| 156 | */
|
|---|
| 157 |
|
|---|
| 158 | /*!
|
|---|
| 159 | \def LOGC(expr, arg)
|
|---|
| 160 | Write to the log if and only if expr is true. Because macros cannot accept
|
|---|
| 161 | variable arguments, this should be invoked like so:
|
|---|
| 162 | \code
|
|---|
| 163 | LOGC(x == y, (CLOG_XXX "%d and %d are equal", x, y));
|
|---|
| 164 | \endcode
|
|---|
| 165 | In particular, notice the parentheses around everything after the boolean
|
|---|
| 166 | expression. Also note that there is no comma after the \c CLOG_XXX.
|
|---|
| 167 | The \c XXX should be replaced by one of enumerants in \c CLog::ELevel
|
|---|
| 168 | without the leading \c k. For example, \c CLOG_INFO. The special
|
|---|
| 169 | \c CLOG_PRINT level will not be filtered and is never prefixed by the
|
|---|
| 170 | filename and line number.
|
|---|
| 171 |
|
|---|
| 172 | If \c NOLOGGING is defined during the build then this macro expands to
|
|---|
| 173 | nothing. If \c NDEBUG is not defined during the build then it expands
|
|---|
| 174 | to a call to CLog::print that prints the filename and line number,
|
|---|
| 175 | otherwise it expands to a call that doesn't.
|
|---|
| 176 | */
|
|---|
| 177 |
|
|---|
| 178 | #if defined(NOLOGGING)
|
|---|
| 179 | #define LOG(_a1)
|
|---|
| 180 | #define LOGC(_a1, _a2)
|
|---|
| 181 | #define CLOG_TRACE
|
|---|
| 182 | #elif defined(NDEBUG)
|
|---|
| 183 | #define LOG(_a1) CLOG->print _a1
|
|---|
| 184 | #define LOGC(_a1, _a2) if (_a1) CLOG->print _a2
|
|---|
| 185 | #define CLOG_TRACE NULL, 0,
|
|---|
| 186 | #else
|
|---|
| 187 | #define LOG(_a1) CLOG->print _a1
|
|---|
| 188 | #define LOGC(_a1, _a2) if (_a1) CLOG->print _a2
|
|---|
| 189 | #define CLOG_TRACE __FILE__, __LINE__,
|
|---|
| 190 | #endif
|
|---|
| 191 |
|
|---|
| 192 | #define CLOG_PRINT CLOG_TRACE "%z\057"
|
|---|
| 193 | #define CLOG_CRIT CLOG_TRACE "%z\060"
|
|---|
| 194 | #define CLOG_ERR CLOG_TRACE "%z\061"
|
|---|
| 195 | #define CLOG_WARN CLOG_TRACE "%z\062"
|
|---|
| 196 | #define CLOG_NOTE CLOG_TRACE "%z\063"
|
|---|
| 197 | #define CLOG_INFO CLOG_TRACE "%z\064"
|
|---|
| 198 | #define CLOG_DEBUG CLOG_TRACE "%z\065"
|
|---|
| 199 | #define CLOG_DEBUG1 CLOG_TRACE "%z\066"
|
|---|
| 200 | #define CLOG_DEBUG2 CLOG_TRACE "%z\067"
|
|---|
| 201 |
|
|---|
| 202 | #endif
|
|---|