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.h 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.h - The Log writer class.
  19. #ifndef __RFB_LOG_WRITER_H__
  20. #define __RFB_LOG_WRITER_H__
  21. #include <stdarg.h>
  22. #include <rfb/Logger.h>
  23. #include <rfb/Configuration.h>
  24. #ifdef __GNUC__
  25. # define __printf_attr(a, b) __attribute__((__format__ (__printf__, a, b)))
  26. #else
  27. # define __printf_attr(a, b)
  28. #endif // __GNUC__
  29. // Each log writer instance has a unique textual name,
  30. // and is attached to a particular Log instance and
  31. // is assigned a particular log level.
  32. #define DEF_LOGFUNCTION(name, level) \
  33. inline void v##name(const char* fmt, va_list ap) __printf_attr(2, 0) { \
  34. if (m_log && (level <= m_level)) \
  35. m_log->write(level, m_name, fmt, ap); \
  36. } \
  37. inline void name(const char* fmt, ...) __printf_attr(2, 3) { \
  38. if (m_log && (level <= m_level)) { \
  39. va_list ap; va_start(ap, fmt); \
  40. m_log->write(level, m_name, fmt, ap);\
  41. va_end(ap); \
  42. } \
  43. }
  44. namespace rfb {
  45. class LogWriter;
  46. class LogWriter {
  47. public:
  48. LogWriter(const char* name);
  49. ~LogWriter();
  50. const char *getName() {return m_name;}
  51. void setLog(Logger *logger);
  52. void setLevel(int level);
  53. int getLevel(void) { return m_level; }
  54. inline void write(int level, const char* format, ...) __printf_attr(3, 4) {
  55. if (m_log && (level <= m_level)) {
  56. va_list ap;
  57. va_start(ap, format);
  58. m_log->write(level, m_name, format, ap);
  59. va_end(ap);
  60. }
  61. }
  62. static const int LEVEL_ERROR = 0;
  63. static const int LEVEL_STATUS = 10;
  64. static const int LEVEL_INFO = 30;
  65. static const int LEVEL_DEBUG = 100;
  66. DEF_LOGFUNCTION(error, LEVEL_ERROR)
  67. DEF_LOGFUNCTION(status, LEVEL_STATUS)
  68. DEF_LOGFUNCTION(info, LEVEL_INFO)
  69. DEF_LOGFUNCTION(debug, LEVEL_DEBUG)
  70. // -=- DIAGNOSTIC & HELPER ROUTINES
  71. static void listLogWriters(int width=79);
  72. // -=- CLASS FIELDS & FUNCTIONS
  73. static LogWriter* log_writers;
  74. static LogWriter* getLogWriter(const char* name);
  75. static bool setLogParams(const char* params);
  76. private:
  77. const char* m_name;
  78. int m_level;
  79. Logger* m_log;
  80. LogWriter* m_next;
  81. };
  82. class LogParameter : public StringParameter {
  83. public:
  84. LogParameter();
  85. virtual bool setParam(const char* v);
  86. // Call this to set a suitable default value.
  87. // Can't use the normal default mechanism for
  88. // this because there is no guarantee on C++
  89. // constructor ordering - some LogWriters may
  90. // not exist when LogParameter gets constructed.
  91. // NB: The default value must exist for the
  92. // lifetime of the process!
  93. void setDefault(const char* v);
  94. };
  95. extern LogParameter logParams;
  96. };
  97. #endif // __RFB_LOG_WRITER_H__