backend.cpp 13 KB

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