You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Logger.cxx 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // -=- Logger.cxx - support for the Logger and LogWriter classes
  19. #ifdef HAVE_CONFIG_H
  20. #include <config.h>
  21. #endif
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <rfb/Logger.h>
  26. #include <rfb/LogWriter.h>
  27. #include <rfb/util.h>
  28. using namespace rfb;
  29. Logger* Logger::loggers = 0;
  30. Logger::Logger(const char* name) : registered(false), m_name(name), m_next(0) {
  31. }
  32. Logger::~Logger() {
  33. // *** Should remove this logger here!
  34. }
  35. void Logger::write(int level, const char *logname, const char* format,
  36. va_list ap)
  37. {
  38. // - Format the supplied data, and pass it to the
  39. // actual log_message function
  40. // The log level is included as a hint for loggers capable of representing
  41. // different log levels in some way.
  42. char buf1[4096];
  43. vsnprintf(buf1, sizeof(buf1)-1, format, ap);
  44. buf1[sizeof(buf1)-1] = 0;
  45. char *buf = buf1;
  46. while (true) {
  47. char *end = strchr(buf, '\n');
  48. if (end)
  49. *end = '\0';
  50. write(level, logname, buf);
  51. if (!end)
  52. break;
  53. buf = end + 1;
  54. }
  55. }
  56. void
  57. Logger::registerLogger() {
  58. if (!registered) {
  59. registered = true;
  60. m_next = loggers;
  61. loggers=this;
  62. }
  63. }
  64. Logger*
  65. Logger::getLogger(const char* name) {
  66. Logger* current = loggers;
  67. while (current) {
  68. if (strcasecmp(name, current->m_name) == 0) return current;
  69. current = current->m_next;
  70. }
  71. return 0;
  72. }
  73. void
  74. Logger::listLoggers() {
  75. Logger* current = loggers;
  76. while (current) {
  77. printf(" %s\n", current->m_name);
  78. current = current->m_next;
  79. }
  80. }