blob: 8b3c6eefaa5ed236baed3600c38e3c58f49527b2 [file] [log] [blame]
Constantin Kaplinskyde179d42006-04-16 06:53:44 +00001/* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
2 *
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00003 * 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
34using namespace rfb;
35
36#ifndef HAVE_VSNPRINTF
37static int vsnprintf(char *str, size_t n, const char *format, va_list ap)
38{
39 str[0] = 0;
40 FILE* fp = fopen("/dev/null","w");
41 if (!fp) return 0;
42 int len = vfprintf(fp, format, ap);
43 if (len <= 0) return 0;
44 fclose(fp);
45
46 CharArray s(len+1);
47 vsprintf(s.buf, format, ap);
48
49 if (len > (int)n-1) len = n-1;
50 memcpy(str, s.buf, len);
51 str[len] = 0;
52 return len;
53}
54#endif
55
56
57Logger* Logger::loggers = 0;
58
59Logger::Logger(const char* name) : registered(false), m_name(name), m_next(0) {
60}
61
62Logger::~Logger() {
63 // *** Should remove this logger here!
64}
65
66void Logger::write(int level, const char *logname, const char* format,
67 va_list ap)
68{
69 // - Format the supplied data, and pass it to the
70 // actual log_message function
71 // The log level is included as a hint for loggers capable of representing
72 // different log levels in some way.
73 char buf1[4096];
74 vsnprintf(buf1, sizeof(buf1)-1, format, ap);
75 buf1[sizeof(buf1)-1] = 0;
76 write(level, logname, buf1);
77}
78
79void
80Logger::registerLogger() {
81 if (!registered) {
82 registered = true;
83 m_next = loggers;
84 loggers=this;
85 }
86}
87
88Logger*
89Logger::getLogger(const char* name) {
90 Logger* current = loggers;
91 while (current) {
92 if (strcasecmp(name, current->m_name) == 0) return current;
93 current = current->m_next;
94 }
95 return 0;
96}
97
98void
99Logger::listLoggers() {
100 Logger* current = loggers;
101 while (current) {
102 printf(" %s\n", current->m_name);
103 current = current->m_next;
104 }
105}
106
107