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.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include <stdarg.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #ifdef WIN32
  23. #define strcasecmp _stricmp
  24. #define vsnprintf _vsnprintf
  25. #define HAVE_VSNPRINTF
  26. #endif
  27. #include <rfb/Logger.h>
  28. #include <rfb/LogWriter.h>
  29. #include <rfb/util.h>
  30. #include <rfb/Threading.h>
  31. using namespace rfb;
  32. #ifndef HAVE_VSNPRINTF
  33. #ifdef __RFB_THREADING_IMPL
  34. static Mutex fpLock;
  35. #endif
  36. static FILE* fp = 0;
  37. int vsnprintf(char *str, size_t n, const char *format, va_list ap)
  38. {
  39. str[0] = 0;
  40. if (!fp) {
  41. // Safely create a FILE* for /dev/null if there isn't already one
  42. #ifdef __RFB_THREADING_IMPL
  43. Lock l(fpLock);
  44. #endif
  45. if (!fp)
  46. fp = fopen("/dev/null","w");
  47. if (!fp) return 0;
  48. }
  49. int len = vfprintf(fp, format, ap);
  50. if (len <= 0) return 0;
  51. CharArray s(len+1);
  52. vsprintf(s.buf, format, ap);
  53. int written = __rfbmin(len, (int)n-1);
  54. memcpy(str, s.buf, written);
  55. str[written] = 0;
  56. return len;
  57. }
  58. #endif
  59. Logger* Logger::loggers = 0;
  60. Logger::Logger(const char* name) : registered(false), m_name(name), m_next(0) {
  61. }
  62. Logger::~Logger() {
  63. // *** Should remove this logger here!
  64. }
  65. void Logger::write(int level, const char *logname, const char* format,
  66. va_list ap)
  67. {
  68. // - Format the supplied data, and pass it to the
  69. // actual log_message function
  70. // The log level is included as a hint for loggers capable of representing
  71. // different log levels in some way.
  72. char buf1[4096];
  73. vsnprintf(buf1, sizeof(buf1)-1, format, ap);
  74. buf1[sizeof(buf1)-1] = 0;
  75. write(level, logname, buf1);
  76. }
  77. void
  78. Logger::registerLogger() {
  79. if (!registered) {
  80. registered = true;
  81. m_next = loggers;
  82. loggers=this;
  83. }
  84. }
  85. Logger*
  86. Logger::getLogger(const char* name) {
  87. Logger* current = loggers;
  88. while (current) {
  89. if (strcasecmp(name, current->m_name) == 0) return current;
  90. current = current->m_next;
  91. }
  92. return 0;
  93. }
  94. void
  95. Logger::listLoggers() {
  96. Logger* current = loggers;
  97. while (current) {
  98. printf(" %s\n", current->m_name);
  99. current = current->m_next;
  100. }
  101. }