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.

LogWriter.cxx 3.6KB

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