blob: 52d33085576577aef130558cf7e499f5f5dc7763 [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// -=- Logger.cxx - support for the Logger and LogWriter classes
20
21#include <stdarg.h>
22#include <stdio.h>
23#include <string.h>
24#ifdef WIN32
25#define strcasecmp _stricmp
26#define vsnprintf _vsnprintf
27#define HAVE_VSNPRINTF
28#endif
29
30#include <rfb/Logger.h>
31#include <rfb/LogWriter.h>
32#include <rfb/util.h>
33#include <rfb/Threading.h>
34
35using namespace rfb;
36
37#ifndef HAVE_VSNPRINTF
38#ifdef __RFB_THREADING_IMPL
39static Mutex fpLock;
40#endif
41static FILE* fp = 0;
42int vsnprintf(char *str, size_t n, const char *format, va_list ap)
43{
44 str[0] = 0;
45 if (!fp) {
46 // Safely create a FILE* for /dev/null if there isn't already one
47#ifdef __RFB_THREADING_IMPL
48 Lock l(fpLock);
49#endif
50 if (!fp)
51 fp = fopen("/dev/null","w");
52 if (!fp) return 0;
53 }
54 int len = vfprintf(fp, format, ap);
55 if (len <= 0) return 0;
56
57 CharArray s(len+1);
58 vsprintf(s.buf, format, ap);
59
60 int written = __rfbmin(len, (int)n-1);
61 memcpy(str, s.buf, written);
62 str[written] = 0;
63 return len;
64}
65#endif
66
67
68Logger* Logger::loggers = 0;
69
70Logger::Logger(const char* name) : registered(false), m_name(name), m_next(0) {
71}
72
73Logger::~Logger() {
74 // *** Should remove this logger here!
75}
76
77void Logger::write(int level, const char *logname, const char* format,
78 va_list ap)
79{
80 // - Format the supplied data, and pass it to the
81 // actual log_message function
82 // The log level is included as a hint for loggers capable of representing
83 // different log levels in some way.
84 char buf1[4096];
85 vsnprintf(buf1, sizeof(buf1)-1, format, ap);
86 buf1[sizeof(buf1)-1] = 0;
87 write(level, logname, buf1);
88}
89
90void
91Logger::registerLogger() {
92 if (!registered) {
93 registered = true;
94 m_next = loggers;
95 loggers=this;
96 }
97}
98
99Logger*
100Logger::getLogger(const char* name) {
101 Logger* current = loggers;
102 while (current) {
103 if (strcasecmp(name, current->m_name) == 0) return current;
104 current = current->m_next;
105 }
106 return 0;
107}
108
109void
110Logger::listLoggers() {
111 Logger* current = loggers;
112 while (current) {
113 printf(" %s\n", current->m_name);
114 current = current->m_next;
115 }
116}
117
118