backend.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. // Copyright 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <array>
  6. #include <chrono>
  7. #include <climits>
  8. #include <condition_variable>
  9. #include <memory>
  10. #include <thread>
  11. #ifdef _WIN32
  12. #include <share.h> // For _SH_DENYWR
  13. #else
  14. #define _SH_DENYWR 0
  15. #endif
  16. #include "common/assert.h"
  17. #include "common/common_funcs.h" // snprintf compatibility define
  18. #include "common/logging/backend.h"
  19. #include "common/logging/log.h"
  20. #include "common/logging/text_formatter.h"
  21. #include "common/string_util.h"
  22. #include "common/threadsafe_queue.h"
  23. namespace Log {
  24. /**
  25. * Static state as a singleton.
  26. */
  27. class Impl {
  28. public:
  29. static Impl& Instance() {
  30. static Impl backend;
  31. return backend;
  32. }
  33. Impl(Impl const&) = delete;
  34. const Impl& operator=(Impl const&) = delete;
  35. void PushEntry(Entry e) {
  36. std::lock_guard<std::mutex> lock(message_mutex);
  37. message_queue.Push(std::move(e));
  38. message_cv.notify_one();
  39. }
  40. void AddBackend(std::unique_ptr<Backend> backend) {
  41. std::lock_guard<std::mutex> lock(writing_mutex);
  42. backends.push_back(std::move(backend));
  43. }
  44. void RemoveBackend(std::string_view backend_name) {
  45. std::lock_guard<std::mutex> lock(writing_mutex);
  46. const auto it =
  47. std::remove_if(backends.begin(), backends.end(),
  48. [&backend_name](const auto& i) { return backend_name == i->GetName(); });
  49. backends.erase(it, backends.end());
  50. }
  51. const Filter& GetGlobalFilter() const {
  52. return filter;
  53. }
  54. void SetGlobalFilter(const Filter& f) {
  55. filter = f;
  56. }
  57. Backend* GetBackend(std::string_view backend_name) {
  58. const auto it =
  59. std::find_if(backends.begin(), backends.end(),
  60. [&backend_name](const auto& i) { return backend_name == i->GetName(); });
  61. if (it == backends.end())
  62. return nullptr;
  63. return it->get();
  64. }
  65. private:
  66. Impl() {
  67. backend_thread = std::thread([&] {
  68. Entry entry;
  69. auto write_logs = [&](Entry& e) {
  70. std::lock_guard<std::mutex> lock(writing_mutex);
  71. for (const auto& backend : backends) {
  72. backend->Write(e);
  73. }
  74. };
  75. while (true) {
  76. {
  77. std::unique_lock<std::mutex> lock(message_mutex);
  78. message_cv.wait(lock, [&] { return !running || message_queue.Pop(entry); });
  79. }
  80. if (!running) {
  81. break;
  82. }
  83. write_logs(entry);
  84. }
  85. // Drain the logging queue. Only writes out up to MAX_LOGS_TO_WRITE to prevent a case
  86. // where a system is repeatedly spamming logs even on close.
  87. const int MAX_LOGS_TO_WRITE = filter.IsDebug() ? INT_MAX : 100;
  88. int logs_written = 0;
  89. while (logs_written++ < MAX_LOGS_TO_WRITE && message_queue.Pop(entry)) {
  90. write_logs(entry);
  91. }
  92. });
  93. }
  94. ~Impl() {
  95. running = false;
  96. message_cv.notify_one();
  97. backend_thread.join();
  98. }
  99. std::atomic_bool running{true};
  100. std::mutex message_mutex, writing_mutex;
  101. std::condition_variable message_cv;
  102. std::thread backend_thread;
  103. std::vector<std::unique_ptr<Backend>> backends;
  104. Common::MPSCQueue<Log::Entry> message_queue;
  105. Filter filter;
  106. };
  107. void ConsoleBackend::Write(const Entry& entry) {
  108. PrintMessage(entry);
  109. }
  110. void ColorConsoleBackend::Write(const Entry& entry) {
  111. PrintColoredMessage(entry);
  112. }
  113. // _SH_DENYWR allows read only access to the file for other programs.
  114. // It is #defined to 0 on other platforms
  115. FileBackend::FileBackend(const std::string& filename)
  116. : file(filename, "w", _SH_DENYWR), bytes_written(0) {}
  117. void FileBackend::Write(const Entry& entry) {
  118. // prevent logs from going over the maximum size (in case its spamming and the user doesn't
  119. // know)
  120. constexpr size_t MAX_BYTES_WRITTEN = 50 * 1024L * 1024L;
  121. if (!file.IsOpen() || bytes_written > MAX_BYTES_WRITTEN) {
  122. return;
  123. }
  124. bytes_written += file.WriteString(FormatLogMessage(entry) + '\n');
  125. if (entry.log_level >= Level::Error) {
  126. file.Flush();
  127. }
  128. }
  129. /// Macro listing all log classes. Code should define CLS and SUB as desired before invoking this.
  130. #define ALL_LOG_CLASSES() \
  131. CLS(Log) \
  132. CLS(Common) \
  133. SUB(Common, Filesystem) \
  134. SUB(Common, Memory) \
  135. CLS(Core) \
  136. SUB(Core, ARM) \
  137. SUB(Core, Timing) \
  138. CLS(Config) \
  139. CLS(Debug) \
  140. SUB(Debug, Emulated) \
  141. SUB(Debug, GPU) \
  142. SUB(Debug, Breakpoint) \
  143. SUB(Debug, GDBStub) \
  144. CLS(Kernel) \
  145. SUB(Kernel, SVC) \
  146. CLS(Service) \
  147. SUB(Service, ACC) \
  148. SUB(Service, Audio) \
  149. SUB(Service, AM) \
  150. SUB(Service, AOC) \
  151. SUB(Service, APM) \
  152. SUB(Service, BCAT) \
  153. SUB(Service, Fatal) \
  154. SUB(Service, Friend) \
  155. SUB(Service, FS) \
  156. SUB(Service, HID) \
  157. SUB(Service, LM) \
  158. SUB(Service, MM) \
  159. SUB(Service, NFP) \
  160. SUB(Service, NIFM) \
  161. SUB(Service, NS) \
  162. SUB(Service, NVDRV) \
  163. SUB(Service, PCTL) \
  164. SUB(Service, PREPO) \
  165. SUB(Service, SET) \
  166. SUB(Service, SM) \
  167. SUB(Service, SPL) \
  168. SUB(Service, SSL) \
  169. SUB(Service, Time) \
  170. SUB(Service, VI) \
  171. CLS(HW) \
  172. SUB(HW, Memory) \
  173. SUB(HW, LCD) \
  174. SUB(HW, GPU) \
  175. SUB(HW, AES) \
  176. CLS(IPC) \
  177. CLS(Frontend) \
  178. CLS(Render) \
  179. SUB(Render, Software) \
  180. SUB(Render, OpenGL) \
  181. CLS(Audio) \
  182. SUB(Audio, DSP) \
  183. SUB(Audio, Sink) \
  184. CLS(Input) \
  185. CLS(Network) \
  186. CLS(Loader) \
  187. CLS(WebService)
  188. // GetClassName is a macro defined by Windows.h, grrr...
  189. const char* GetLogClassName(Class log_class) {
  190. switch (log_class) {
  191. #define CLS(x) \
  192. case Class::x: \
  193. return #x;
  194. #define SUB(x, y) \
  195. case Class::x##_##y: \
  196. return #x "." #y;
  197. ALL_LOG_CLASSES()
  198. #undef CLS
  199. #undef SUB
  200. case Class::Count:
  201. UNREACHABLE();
  202. }
  203. }
  204. const char* GetLevelName(Level log_level) {
  205. #define LVL(x) \
  206. case Level::x: \
  207. return #x
  208. switch (log_level) {
  209. LVL(Trace);
  210. LVL(Debug);
  211. LVL(Info);
  212. LVL(Warning);
  213. LVL(Error);
  214. LVL(Critical);
  215. case Level::Count:
  216. UNREACHABLE();
  217. }
  218. #undef LVL
  219. }
  220. Entry CreateEntry(Class log_class, Level log_level, const char* filename, unsigned int line_nr,
  221. const char* function, std::string message) {
  222. using std::chrono::duration_cast;
  223. using std::chrono::steady_clock;
  224. static steady_clock::time_point time_origin = steady_clock::now();
  225. Entry entry;
  226. entry.timestamp = duration_cast<std::chrono::microseconds>(steady_clock::now() - time_origin);
  227. entry.log_class = log_class;
  228. entry.log_level = log_level;
  229. entry.filename = Common::TrimSourcePath(filename);
  230. entry.line_num = line_nr;
  231. entry.function = function;
  232. entry.message = std::move(message);
  233. return entry;
  234. }
  235. void SetGlobalFilter(const Filter& filter) {
  236. Impl::Instance().SetGlobalFilter(filter);
  237. }
  238. void AddBackend(std::unique_ptr<Backend> backend) {
  239. Impl::Instance().AddBackend(std::move(backend));
  240. }
  241. void RemoveBackend(std::string_view backend_name) {
  242. Impl::Instance().RemoveBackend(backend_name);
  243. }
  244. Backend* GetBackend(std::string_view backend_name) {
  245. return Impl::Instance().GetBackend(backend_name);
  246. }
  247. void FmtLogMessageImpl(Class log_class, Level log_level, const char* filename,
  248. unsigned int line_num, const char* function, const char* format,
  249. const fmt::format_args& args) {
  250. auto filter = Impl::Instance().GetGlobalFilter();
  251. if (!filter.CheckMessage(log_class, log_level))
  252. return;
  253. Entry entry =
  254. CreateEntry(log_class, log_level, filename, line_num, function, fmt::vformat(format, args));
  255. Impl::Instance().PushEntry(std::move(entry));
  256. }
  257. } // namespace Log