backend.cpp 16 KB

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