blob: ad10a4cf5bb620b5ff736ead73a582df7c1b58cd [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>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000024
25#include <rfb/Logger.h>
26#include <rfb/LogWriter.h>
27#include <rfb/util.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000028
29using namespace rfb;
30
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000031Logger* Logger::loggers = 0;
32
33Logger::Logger(const char* name) : registered(false), m_name(name), m_next(0) {
34}
35
36Logger::~Logger() {
37 // *** Should remove this logger here!
38}
39
40void Logger::write(int level, const char *logname, const char* format,
41 va_list ap)
42{
43 // - Format the supplied data, and pass it to the
44 // actual log_message function
45 // The log level is included as a hint for loggers capable of representing
46 // different log levels in some way.
47 char buf1[4096];
48 vsnprintf(buf1, sizeof(buf1)-1, format, ap);
49 buf1[sizeof(buf1)-1] = 0;
Pierre Ossman46c2c462018-11-01 16:04:30 +010050 char *buf = buf1;
51 while (true) {
52 char *end = strchr(buf, '\n');
53 if (end)
54 *end = '\0';
55 write(level, logname, buf);
56 if (!end)
57 break;
58 buf = end + 1;
59 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000060}
61
62void
63Logger::registerLogger() {
64 if (!registered) {
65 registered = true;
66 m_next = loggers;
67 loggers=this;
68 }
69}
70
71Logger*
72Logger::getLogger(const char* name) {
73 Logger* current = loggers;
74 while (current) {
75 if (strcasecmp(name, current->m_name) == 0) return current;
76 current = current->m_next;
77 }
78 return 0;
79}
80
81void
82Logger::listLoggers() {
83 Logger* current = loggers;
84 while (current) {
85 printf(" %s\n", current->m_name);
86 current = current->m_next;
87 }
88}
89
90