Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame^] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // -=- LogWriter.h - The Log writer class. |
| 20 | |
| 21 | #ifndef __RFB_LOG_WRITER_H__ |
| 22 | #define __RFB_LOG_WRITER_H__ |
| 23 | |
| 24 | #include <stdarg.h> |
| 25 | #include <rfb/Logger.h> |
| 26 | #include <rfb/Configuration.h> |
| 27 | |
| 28 | // Each log writer instance has a unique textual name, |
| 29 | // and is attached to a particular Log instance and |
| 30 | // is assigned a particular log level. |
| 31 | |
| 32 | #define DEF_LOGFUNCTION(name, level) \ |
| 33 | inline void name(const char* fmt, ...) { \ |
| 34 | if (m_log && (level <= m_level)) { \ |
| 35 | va_list ap; va_start(ap, fmt); \ |
| 36 | m_log->write(level, m_name, fmt, ap);\ |
| 37 | va_end(ap); \ |
| 38 | } \ |
| 39 | } |
| 40 | |
| 41 | namespace rfb { |
| 42 | |
| 43 | class LogWriter; |
| 44 | |
| 45 | class LogWriter { |
| 46 | public: |
| 47 | LogWriter(const char* name); |
| 48 | ~LogWriter(); |
| 49 | |
| 50 | const char *getName() {return m_name;} |
| 51 | |
| 52 | void setLog(Logger *logger); |
| 53 | void setLevel(int level); |
| 54 | |
| 55 | inline void write(int level, const char* format, ...) { |
| 56 | if (m_log && (level <= m_level)) { |
| 57 | va_list ap; |
| 58 | va_start(ap, format); |
| 59 | m_log->write(level, m_name, format, ap); |
| 60 | va_end(ap); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | DEF_LOGFUNCTION(error, 0) |
| 65 | DEF_LOGFUNCTION(status, 10) |
| 66 | DEF_LOGFUNCTION(info, 30) |
| 67 | DEF_LOGFUNCTION(debug, 100) |
| 68 | |
| 69 | // -=- DIAGNOSTIC & HELPER ROUTINES |
| 70 | |
| 71 | static void listLogWriters(int width=79); |
| 72 | |
| 73 | // -=- CLASS FIELDS & FUNCTIONS |
| 74 | |
| 75 | static LogWriter* log_writers; |
| 76 | |
| 77 | static LogWriter* getLogWriter(const char* name); |
| 78 | |
| 79 | static bool setLogParams(const char* params); |
| 80 | |
| 81 | private: |
| 82 | const char* m_name; |
| 83 | int m_level; |
| 84 | Logger* m_log; |
| 85 | LogWriter* m_next; |
| 86 | }; |
| 87 | |
| 88 | class LogParameter : public StringParameter { |
| 89 | public: |
| 90 | LogParameter(); |
| 91 | virtual bool setParam(const char* v); |
| 92 | |
| 93 | // Call this to set a suitable default value. |
| 94 | // Can't use the normal default mechanism for |
| 95 | // this because there is no guarantee on C++ |
| 96 | // constructor ordering - some LogWriters may |
| 97 | // not exist when LogParameter gets constructed. |
| 98 | // NB: The default value must exist for the |
| 99 | // lifetime of the process! |
| 100 | void setDefault(const char* v); |
| 101 | }; |
| 102 | extern LogParameter logParams; |
| 103 | |
| 104 | }; |
| 105 | |
| 106 | #endif // __RFB_LOG_WRITER_H__ |