log.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #pragma once
  2. #include <array>
  3. #include <cstddef>
  4. #include <string_view>
  5. namespace sunrise::core::log {
  6. /** 1 KiB caps each serialized event, with room for a trailing null byte. */
  7. inline constexpr std::size_t kLineCapacity = 1024;
  8. /** Subsystem owning one structured log event. */
  9. enum class Channel : std::size_t {
  10. core,
  11. client,
  12. state,
  13. server,
  14. middleware,
  15. count,
  16. };
  17. /** Minimum severity accepted by one log channel. */
  18. enum class Level : unsigned char {
  19. error,
  20. warn,
  21. info,
  22. debug,
  23. off,
  24. };
  25. /** Process-wide logging thresholds and sink selection. */
  26. struct Settings {
  27. std::array<Level, static_cast<std::size_t>(Channel::count)> levels{};
  28. bool debuggerSink{true};
  29. bool fileSink{false};
  30. };
  31. /** Returns logging defaults with persistent output disabled. */
  32. [[nodiscard]] Settings defaults() noexcept;
  33. /** Starts configured logging sinks relative to the DLL module. */
  34. [[nodiscard]] bool initialize(void* module, const Settings& settings) noexcept;
  35. /** Closes active sinks and clears the bounded in-memory log view. */
  36. void shutdown() noexcept;
  37. /** @return True while the channel threshold admits this severity. */
  38. [[nodiscard]] bool accepts(Channel channel, Level level) noexcept;
  39. /** Emits one structured event when allowed by the channel threshold. */
  40. void write(Channel channel, Level level, std::string_view event) noexcept;
  41. /**
  42. * Emits one debug event carrying a duration in the ms field.
  43. * Timing is diagnostic, so it stays off at the levels a normal run uses.
  44. * @param channel Channel owning the measured boundary.
  45. * @param event Event and phase text the duration is appended to.
  46. * @param startedTick GetTickCount64 value taken when the boundary began.
  47. * @param result Outcome text for the log line.
  48. */
  49. void write_elapsed(Channel channel,
  50. std::string_view event,
  51. unsigned long long startedTick,
  52. std::string_view result) noexcept;
  53. /**
  54. * Reports whether any thread is inside a sink write.
  55. * A sink holds an operating system lock this process shares, so a thread suspended there
  56. * blocks the next writer. Nothing may suspend process threads while this is true.
  57. * @return True while a sink write is in progress.
  58. */
  59. [[nodiscard]] bool writers_active() noexcept;
  60. /**
  61. * Writes one line straight to the debugger, bypassing the sinks and every threshold.
  62. * Settings are read before the sinks exist, so a boot failure there has no other channel.
  63. */
  64. void early(std::string_view event) noexcept;
  65. } // namespace sunrise::core::log