logs.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "base/basic_types.h"
  9. #include "base/assertion.h"
  10. #include "base/debug_log.h"
  11. namespace Logs {
  12. void SetDebugEnabled(bool enabled);
  13. bool DebugEnabled();
  14. [[nodiscard]] bool WritingEntry();
  15. void start();
  16. bool started();
  17. void finish();
  18. bool instanceChecked();
  19. void multipleInstances();
  20. void closeMain();
  21. void writeMain(const QString &v);
  22. void writeDebug(const QString &v);
  23. void writeTcp(const QString &v);
  24. void writeMtp(int32 dc, const QString &v);
  25. QString full();
  26. inline const char *b(bool v) {
  27. return v ? "[TRUE]" : "[FALSE]";
  28. }
  29. struct MemoryBuffer {
  30. MemoryBuffer(const void *ptr, uint32 size) : p(ptr), s(size) {
  31. }
  32. QString str() const {
  33. QString result;
  34. const uchar *buf((const uchar*)p);
  35. const char *hex = "0123456789ABCDEF";
  36. result.reserve(s * 3);
  37. for (uint32 i = 0; i < s; ++i) {
  38. result += hex[(buf[i] >> 4)];
  39. result += hex[buf[i] & 0x0F];
  40. result += ' ';
  41. }
  42. result.chop(1);
  43. return result;
  44. }
  45. const void *p;
  46. uint32 s;
  47. };
  48. inline MemoryBuffer mb(const void *ptr, uint32 size) {
  49. return MemoryBuffer(ptr, size);
  50. }
  51. } // namespace Logs
  52. #define TCP_LOG(msg) {\
  53. if (Logs::DebugEnabled() || !Logs::started()) {\
  54. Logs::writeTcp(QString msg);\
  55. }\
  56. }
  57. //usage TCP_LOG(("log: %1 %2").arg(1).arg(2))
  58. #define MTP_LOG(dc, msg) {\
  59. if (Logs::DebugEnabled() || !Logs::started()) {\
  60. Logs::writeMtp(dc, QString msg);\
  61. }\
  62. }
  63. //usage MTP_LOG(dc, ("log: %1 %2").arg(1).arg(2))