blob: 104a8752608804ac60f7aaf2120a7202e4e0517f [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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 Ossman8cb45e52012-04-26 08:51:19 +000028#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 Kaplinskya2adc8d2006-05-25 05:01:55 +000034// 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) \
Steve Kondik5ec2d092017-07-08 02:06:16 -070039 inline void v##name(const char* fmt, va_list ap) __printf_attr(2, 0) { \
40 if (m_log && (level <= m_level)) \
41 m_log->write(level, m_name, fmt, ap); \
Pierre Ossmancc317902015-01-26 14:15:12 +010042 } \
Pierre Ossman8cb45e52012-04-26 08:51:19 +000043 inline void name(const char* fmt, ...) __printf_attr(2, 3) { \
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000044 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
51namespace 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 Tkac348269d2011-02-18 10:54:11 +000064 int getLevel(void) { return m_level; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000065
Pierre Ossman8cb45e52012-04-26 08:51:19 +000066 inline void write(int level, const char* format, ...) __printf_attr(3, 4) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000067 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
Michal Srb2defd782015-04-10 16:52:26 +030075 static const int LEVEL_ERROR = 0;
76 static const int LEVEL_STATUS = 10;
77 static const int LEVEL_INFO = 30;
78 static const int LEVEL_DEBUG = 100;
79
80 DEF_LOGFUNCTION(error, LEVEL_ERROR)
81 DEF_LOGFUNCTION(status, LEVEL_STATUS)
82 DEF_LOGFUNCTION(info, LEVEL_INFO)
83 DEF_LOGFUNCTION(debug, LEVEL_DEBUG)
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000084
85 // -=- DIAGNOSTIC & HELPER ROUTINES
86
87 static void listLogWriters(int width=79);
88
89 // -=- CLASS FIELDS & FUNCTIONS
90
91 static LogWriter* log_writers;
92
93 static LogWriter* getLogWriter(const char* name);
94
95 static bool setLogParams(const char* params);
96
97 private:
98 const char* m_name;
99 int m_level;
100 Logger* m_log;
101 LogWriter* m_next;
102 };
103
104 class LogParameter : public StringParameter {
105 public:
106 LogParameter();
107 virtual bool setParam(const char* v);
108
109 // Call this to set a suitable default value.
110 // Can't use the normal default mechanism for
111 // this because there is no guarantee on C++
112 // constructor ordering - some LogWriters may
113 // not exist when LogParameter gets constructed.
114 // NB: The default value must exist for the
115 // lifetime of the process!
116 void setDefault(const char* v);
117 };
118 extern LogParameter logParams;
119
120};
121
122#endif // __RFB_LOG_WRITER_H__