blob: 23e3069c5570cc9ed4dab4d48effa641dd1e5c6e [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) \
Pierre Ossman8cb45e52012-04-26 08:51:19 +000039 inline void name(const char* fmt, ...) __printf_attr(2, 3) { \
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000040 if (m_log && (level <= m_level)) { \
41 va_list ap; va_start(ap, fmt); \
42 m_log->write(level, m_name, fmt, ap);\
43 va_end(ap); \
44 } \
45 }
46
47namespace rfb {
48
49 class LogWriter;
50
51 class LogWriter {
52 public:
53 LogWriter(const char* name);
54 ~LogWriter();
55
56 const char *getName() {return m_name;}
57
58 void setLog(Logger *logger);
59 void setLevel(int level);
Adam Tkac348269d2011-02-18 10:54:11 +000060 int getLevel(void) { return m_level; }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000061
Pierre Ossman8cb45e52012-04-26 08:51:19 +000062 inline void write(int level, const char* format, ...) __printf_attr(3, 4) {
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000063 if (m_log && (level <= m_level)) {
64 va_list ap;
65 va_start(ap, format);
66 m_log->write(level, m_name, format, ap);
67 va_end(ap);
68 }
69 }
70
71 DEF_LOGFUNCTION(error, 0)
72 DEF_LOGFUNCTION(status, 10)
73 DEF_LOGFUNCTION(info, 30)
74 DEF_LOGFUNCTION(debug, 100)
75
76 // -=- DIAGNOSTIC & HELPER ROUTINES
77
78 static void listLogWriters(int width=79);
79
80 // -=- CLASS FIELDS & FUNCTIONS
81
82 static LogWriter* log_writers;
83
84 static LogWriter* getLogWriter(const char* name);
85
86 static bool setLogParams(const char* params);
87
88 private:
89 const char* m_name;
90 int m_level;
91 Logger* m_log;
92 LogWriter* m_next;
93 };
94
95 class LogParameter : public StringParameter {
96 public:
97 LogParameter();
98 virtual bool setParam(const char* v);
99
100 // Call this to set a suitable default value.
101 // Can't use the normal default mechanism for
102 // this because there is no guarantee on C++
103 // constructor ordering - some LogWriters may
104 // not exist when LogParameter gets constructed.
105 // NB: The default value must exist for the
106 // lifetime of the process!
107 void setDefault(const char* v);
108 };
109 extern LogParameter logParams;
110
111};
112
113#endif // __RFB_LOG_WRITER_H__