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