msg_log.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // This file is part of BOINC.
  2. // http://boinc.berkeley.edu
  3. // Copyright (C) 2008 University of California
  4. //
  5. // BOINC is free software; you can redistribute it and/or modify it
  6. // under the terms of the GNU Lesser General Public License
  7. // as published by the Free Software Foundation,
  8. // either version 3 of the License, or (at your option) any later version.
  9. //
  10. // BOINC is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. // See the GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with BOINC. If not, see <http://www.gnu.org/licenses/>.
  17. #ifndef BOINC_MSG_LOG_H
  18. #define BOINC_MSG_LOG_H
  19. #include <cstdio>
  20. #include <cstdarg>
  21. #ifdef _USING_FCGI_
  22. #include "boinc_fcgi.h"
  23. #endif
  24. // the __attribute((format...)) tags are GCC extensions that let the compiler
  25. // do like-checking on printf-like arguments
  26. //
  27. #if !defined(__GNUC__) && !defined(__attribute__)
  28. #define __attribute__(x) /*nothing*/
  29. #endif
  30. #ifdef _USING_FCGI_
  31. #define __attribute__(x) //nothing
  32. #endif
  33. #undef printf
  34. #undef vprintf
  35. class MSG_LOG {
  36. public:
  37. int debug_level;
  38. int indent_level;
  39. char spaces[80];
  40. FILE* output;
  41. int pid;
  42. MSG_LOG(FILE* output);
  43. virtual ~MSG_LOG(){}
  44. void enter_level(int = 1);
  45. void leave_level() { enter_level(-1); }
  46. MSG_LOG& operator++() { enter_level(); return *this; }
  47. MSG_LOG& operator--() { leave_level(); return *this; }
  48. void printf(int kind, const char* format, ...) __attribute__ ((format (printf, 3, 4)));
  49. void printf_multiline(int kind, const char* str, const char* prefix_format, ...) __attribute__ ((format (printf, 4, 5)));
  50. void printf_file(int kind, const char* filename, const char* prefix_format, ...) __attribute__ ((format (printf, 4, 5)));
  51. void vprintf(int kind, const char* format, va_list va);
  52. void vprintf_multiline(int kind, const char* str, const char* prefix_format, va_list va);
  53. void vprintf_file(int kind, const char* filename, const char* prefix_format, va_list va);
  54. void set_debug_level(int new_level) { debug_level = new_level; }
  55. void set_indent_level(int new_level);
  56. protected:
  57. virtual const char* v_format_kind(int kind) const = 0;
  58. virtual bool v_message_wanted(int kind) const = 0;
  59. };
  60. // automatically ++/--MSG_LOG on scope entry / exit.
  61. // See lib/msg_log.C for commentary
  62. //
  63. #if _MSC_VER >= 1300
  64. #pragma warning(push)
  65. #pragma warning(disable: 4512) // assignment operator could not be generated
  66. #endif
  67. class SCOPE_MSG_LOG {
  68. MSG_LOG& messages;
  69. int kind;
  70. public:
  71. SCOPE_MSG_LOG(MSG_LOG& messages_, int kind_) : messages(messages_), kind(kind_)
  72. { ++messages; }
  73. ~SCOPE_MSG_LOG() { --messages; }
  74. SCOPE_MSG_LOG& operator++() { ++messages; return *this; }
  75. SCOPE_MSG_LOG& operator--() { --messages; return *this; }
  76. void printf(const char* format, ...) __attribute__ ((format (printf, 2, 3)));
  77. void printf_multiline(const char* str, const char* prefix_format, ...) __attribute__ ((format (printf, 3, 4)));
  78. void printf_file(const char* filename, const char* prefix_format, ...) __attribute__ ((format (printf, 3, 4)));
  79. };
  80. #if _MSC_VER >= 1300
  81. #pragma warning(pop)
  82. #endif
  83. #ifdef _USING_FCGI_
  84. #undef __attribute__
  85. #endif
  86. #endif