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 | |
Pierre Ossman | 8cb45e5 | 2012-04-26 08:51:19 +0000 | [diff] [blame] | 28 | #ifdef __GNUC__ |
| 29 | # define __printf_attr(a, b) __attribute__((__format__ (__printf__, a, b))) |
| 30 | #else |
| 31 | # define __printf_attr(a, b) |
| 32 | #endif // __GNUC__ |
| 33 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 34 | // Each log writer instance has a unique textual name, |
| 35 | // and is attached to a particular Log instance and |
| 36 | // is assigned a particular log level. |
| 37 | |
| 38 | #define DEF_LOGFUNCTION(name, level) \ |
Pierre Ossman | cc31790 | 2015-01-26 14:15:12 +0100 | [diff] [blame^] | 39 | inline void name(const char* fmt, va_list ap) { \ |
| 40 | if (m_log && (level <= m_level)) \ |
| 41 | m_log->write(level, m_name, fmt, ap);\ |
| 42 | } \ |
Pierre Ossman | 8cb45e5 | 2012-04-26 08:51:19 +0000 | [diff] [blame] | 43 | inline void name(const char* fmt, ...) __printf_attr(2, 3) { \ |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 44 | if (m_log && (level <= m_level)) { \ |
| 45 | va_list ap; va_start(ap, fmt); \ |
| 46 | m_log->write(level, m_name, fmt, ap);\ |
| 47 | va_end(ap); \ |
| 48 | } \ |
| 49 | } |
| 50 | |
| 51 | namespace rfb { |
| 52 | |
| 53 | class LogWriter; |
| 54 | |
| 55 | class LogWriter { |
| 56 | public: |
| 57 | LogWriter(const char* name); |
| 58 | ~LogWriter(); |
| 59 | |
| 60 | const char *getName() {return m_name;} |
| 61 | |
| 62 | void setLog(Logger *logger); |
| 63 | void setLevel(int level); |
Adam Tkac | 348269d | 2011-02-18 10:54:11 +0000 | [diff] [blame] | 64 | int getLevel(void) { return m_level; } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 65 | |
Pierre Ossman | 8cb45e5 | 2012-04-26 08:51:19 +0000 | [diff] [blame] | 66 | inline void write(int level, const char* format, ...) __printf_attr(3, 4) { |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 67 | if (m_log && (level <= m_level)) { |
| 68 | va_list ap; |
| 69 | va_start(ap, format); |
| 70 | m_log->write(level, m_name, format, ap); |
| 71 | va_end(ap); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | DEF_LOGFUNCTION(error, 0) |
| 76 | DEF_LOGFUNCTION(status, 10) |
| 77 | DEF_LOGFUNCTION(info, 30) |
| 78 | DEF_LOGFUNCTION(debug, 100) |
| 79 | |
| 80 | // -=- DIAGNOSTIC & HELPER ROUTINES |
| 81 | |
| 82 | static void listLogWriters(int width=79); |
| 83 | |
| 84 | // -=- CLASS FIELDS & FUNCTIONS |
| 85 | |
| 86 | static LogWriter* log_writers; |
| 87 | |
| 88 | static LogWriter* getLogWriter(const char* name); |
| 89 | |
| 90 | static bool setLogParams(const char* params); |
| 91 | |
| 92 | private: |
| 93 | const char* m_name; |
| 94 | int m_level; |
| 95 | Logger* m_log; |
| 96 | LogWriter* m_next; |
| 97 | }; |
| 98 | |
| 99 | class LogParameter : public StringParameter { |
| 100 | public: |
| 101 | LogParameter(); |
| 102 | virtual bool setParam(const char* v); |
| 103 | |
| 104 | // Call this to set a suitable default value. |
| 105 | // Can't use the normal default mechanism for |
| 106 | // this because there is no guarantee on C++ |
| 107 | // constructor ordering - some LogWriters may |
| 108 | // not exist when LogParameter gets constructed. |
| 109 | // NB: The default value must exist for the |
| 110 | // lifetime of the process! |
| 111 | void setDefault(const char* v); |
| 112 | }; |
| 113 | extern LogParameter logParams; |
| 114 | |
| 115 | }; |
| 116 | |
| 117 | #endif // __RFB_LOG_WRITER_H__ |