log.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // SuperTux Debug Helper Functions
  2. // Copyright (C) 2006 Christoph Sommer <christoph.sommer@2006.expires.deltadevelopment.de>
  3. // Copyright (C) 2010 Florian Forster <supertux at octo.it>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program 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. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #include "util/log.hpp"
  18. #include <iostream>
  19. #ifdef __ANDROID__
  20. #include <android/log.h>
  21. #endif
  22. #include "math/rectf.hpp"
  23. #include "supertux/console.hpp"
  24. #include "supertux/gameconfig.hpp"
  25. #include "supertux/globals.hpp"
  26. #ifdef __ANDROID__
  27. // To print only SuperTux logs in Android logcat output, use command:
  28. // adb logcat -s 'SuperTux:V' 'SDL:V' 'DEBUG:V'
  29. class _android_debugbuf: public std::streambuf
  30. {
  31. public:
  32. _android_debugbuf()
  33. {
  34. pos = 0;
  35. buf[0] = 0;
  36. }
  37. protected:
  38. virtual int overflow(int c = EOF)
  39. {
  40. if (EOF == c)
  41. {
  42. return '\0'; // returning EOF indicates an error
  43. }
  44. else
  45. {
  46. outputchar(c);
  47. return c;
  48. }
  49. }
  50. // we don’t do input so always return EOF
  51. virtual int uflow()
  52. {
  53. return EOF;
  54. }
  55. // we don’t do input so always return 0 chars read
  56. virtual int xsgetn(char *, int)
  57. {
  58. return 0;
  59. }
  60. // Calls outputchar() for each character.
  61. virtual int xsputn(const char *s, int n)
  62. {
  63. for (int i = 0; i < n; ++i)
  64. {
  65. outputchar(s[i]);
  66. }
  67. return n;// we always process all of the chars
  68. }
  69. private:
  70. char buf[512];
  71. int pos;
  72. void outputchar(char c)
  73. {
  74. // TODO: mutex
  75. buf[pos] = c;
  76. pos++;
  77. if (pos >= sizeof(buf) - 1 || c == '\n' || c == '\r' || c == 0)
  78. {
  79. buf[pos] = 0;
  80. __android_log_print(ANDROID_LOG_INFO, "SuperTux", "%s", buf);
  81. pos = 0;
  82. buf[pos] = 0;
  83. }
  84. }
  85. };
  86. static std::ostream android_logcat(new _android_debugbuf());
  87. #endif
  88. LogLevel g_log_level = LOG_WARNING;
  89. std::ostream& get_logging_instance(bool use_console_buffer)
  90. {
  91. if (ConsoleBuffer::current() && use_console_buffer)
  92. return (ConsoleBuffer::output);
  93. else
  94. #ifdef __ANDROID__
  95. return android_logcat;
  96. #else
  97. return (std::cerr);
  98. #endif
  99. }
  100. static std::ostream& log_generic_f (const char *prefix, const char* file, int line, bool use_console_buffer = true)
  101. {
  102. get_logging_instance (use_console_buffer) << prefix << " " << file << ":" << line << " ";
  103. return (get_logging_instance (use_console_buffer));
  104. }
  105. std::ostream& log_debug_f(const char* file, int line, bool use_console_buffer = true)
  106. {
  107. return (log_generic_f ("[DEBUG]", file, line, use_console_buffer));
  108. }
  109. std::ostream& log_info_f(const char* file, int line)
  110. {
  111. return (log_generic_f ("[INFO]", file, line));
  112. }
  113. std::ostream& log_warning_f(const char* file, int line)
  114. {
  115. if (g_config && g_config->developer_mode &&
  116. Console::current() && !Console::current()->hasFocus()) {
  117. Console::current()->open();
  118. }
  119. return (log_generic_f ("[WARNING]", file, line));
  120. }
  121. std::ostream& log_fatal_f(const char* file, int line)
  122. {
  123. if (g_config && g_config->developer_mode &&
  124. Console::current() && !Console::current()->hasFocus()) {
  125. Console::current()->open();
  126. }
  127. return (log_generic_f ("[FATAL]", file, line));
  128. }
  129. /* Callbacks used by tinygettext */
  130. void log_info_callback(const std::string& str)
  131. {
  132. log_info << "\r\n[TINYGETTEXT] " << str << std::endl;
  133. }
  134. void log_warning_callback(const std::string& str)
  135. {
  136. log_debug << "\r\n[TINYGETTEXT] " << str << std::endl;
  137. }
  138. void log_error_callback(const std::string& str)
  139. {
  140. log_warning << "\r\n[TINYGETTEXT] " << str << std::endl;
  141. }
  142. /* EOF */