blob: 37a9d1ce4b9add56106bad3fd54f1b8b569a4946 [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.cxx - client-side logging interface
20
21#include <string.h>
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000022
23#include <rfb/LogWriter.h>
24#include <rfb/Configuration.h>
25#include <rfb/util.h>
26#include <stdlib.h>
27
28rfb::LogParameter rfb::logParams;
29
30using namespace rfb;
31
32
33LogWriter::LogWriter(const char* name) : m_name(name), m_level(0), m_log(0), m_next(log_writers) {
34 log_writers = this;
35}
36
37LogWriter::~LogWriter() {
38 // *** Should remove this logger here!
39}
40
41void LogWriter::setLog(Logger *logger) {
42 m_log = logger;
43}
44
45void LogWriter::setLevel(int level) {
46 m_level = level;
47}
48
49void
50LogWriter::listLogWriters(int width) {
51 // *** make this respect width...
52 LogWriter* current = log_writers;
53 fprintf(stderr, " ");
54 while (current) {
55 fprintf(stderr, "%s", current->m_name);
56 current = current->m_next;
57 if (current) fprintf(stderr, ", ");
58 }
59 fprintf(stderr, "\n");
60}
61
62LogWriter* LogWriter::log_writers;
63
64LogWriter*
65LogWriter::getLogWriter(const char* name) {
66 LogWriter* current = log_writers;
67 while (current) {
68 if (strcasecmp(name, current->m_name) == 0) return current;
69 current = current->m_next;
70 }
71 return 0;
72}
73
74bool LogWriter::setLogParams(const char* params) {
75 CharArray logwriterName, loggerName, logLevel;
76 if (!strSplit(params, ':', &logwriterName.buf, &loggerName.buf) ||
77 !strSplit(loggerName.buf, ':', &loggerName.buf, &logLevel.buf)) {
78 fprintf(stderr,"failed to parse log params:%s\n",params);
79 return false;
80 }
81 int level = atoi(logLevel.buf);
82 Logger* logger = 0;
83 if (strcmp("", loggerName.buf) != 0) {
84 logger = Logger::getLogger(loggerName.buf);
85 if (!logger) fprintf(stderr,"no logger found! %s\n",loggerName.buf);
86 }
87 if (strcmp("*", logwriterName.buf) == 0) {
88 LogWriter* current = log_writers;
89 while (current) {
90 current->setLog(logger);
91 current->setLevel(level);
92 current = current->m_next;
93 }
94 return true;
95 } else {
96 LogWriter* logwriter = getLogWriter(logwriterName.buf);
97 if (!logwriter) {
98 fprintf(stderr,"no logwriter found! %s\n",logwriterName.buf);
99 } else {
100 logwriter->setLog(logger);
101 logwriter->setLevel(level);
102 return true;
103 }
104 }
105 return false;
106}
107
108
109LogParameter::LogParameter()
110 : StringParameter("Log",
111 "Specifies which log output should be directed to "
112 "which target logger, and the level of output to log. "
113 "Format is <log>:<target>:<level>[, ...].",
114 "") {
115}
116
117bool LogParameter::setParam(const char* v) {
118 if (immutable) return true;
119 LogWriter::setLogParams("*::0");
120 StringParameter::setParam(v);
121 CharArray logParam;
122 CharArray params(getData());
123 while (params.buf) {
124 strSplit(params.buf, ',', &logParam.buf, &params.buf);
125 if (strlen(logParam.buf) && !LogWriter::setLogParams(logParam.buf))
126 return false;
127 }
128 return true;
129}
130
131void LogParameter::setDefault(const char* d) {
132 def_value = d;
133 setParam(def_value);
134}