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 | // -=- Logger.cxx - support for the Logger and LogWriter classes |
| 20 | |
| 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <string.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 24 | |
| 25 | #include <rfb/Logger.h> |
| 26 | #include <rfb/LogWriter.h> |
| 27 | #include <rfb/util.h> |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace rfb; |
| 30 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 31 | Logger* Logger::loggers = 0; |
| 32 | |
| 33 | Logger::Logger(const char* name) : registered(false), m_name(name), m_next(0) { |
| 34 | } |
| 35 | |
| 36 | Logger::~Logger() { |
| 37 | // *** Should remove this logger here! |
| 38 | } |
| 39 | |
| 40 | void 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 Ossman | 46c2c46 | 2018-11-01 16:04:30 +0100 | [diff] [blame^] | 50 | 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 Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void |
| 63 | Logger::registerLogger() { |
| 64 | if (!registered) { |
| 65 | registered = true; |
| 66 | m_next = loggers; |
| 67 | loggers=this; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | Logger* |
| 72 | Logger::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 | |
| 81 | void |
| 82 | Logger::listLoggers() { |
| 83 | Logger* current = loggers; |
| 84 | while (current) { |
| 85 | printf(" %s\n", current->m_name); |
| 86 | current = current->m_next; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | |