A C++ library for logging very fast and without allocating.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

208 рядки
16 KiB

2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
2 роки тому
  1. #pragma once
  2. #ifdef __cplusplus
  3. #include <cstdint>
  4. #include <cstddef>
  5. #include <string_view>
  6. #include <string>
  7. #include <functional>
  8. #include <any>
  9. #else
  10. #include <stdint.h>
  11. #include <stddef.h>
  12. #endif
  13. #include "sl/strategies.h"
  14. /**
  15. * @brief represents a buffer strategy, generally as an obscure pointer
  16. */
  17. struct sl_buffer_strategy;
  18. /**
  19. * @brief represents a sink strategy, generally as an obscure pointer
  20. */
  21. struct sl_sink_strategy;
  22. /**
  23. * @brief represents an overflow strategy, generally as an obscure pointer
  24. */
  25. struct sl_overflow_strategy;
  26. /**
  27. * @brief represents an output strategy, generally as an obscure pointer
  28. */
  29. struct sl_output_strategy;
  30. #ifdef __cplusplus
  31. namespace sl {
  32. /**
  33. * @brief represents a c-style buffer strategy
  34. */
  35. using buffer_strategy = sl_buffer_strategy;
  36. /**
  37. * @brief represents a c-style sink strategy
  38. */
  39. using sink_strategy = sl_sink_strategy;
  40. /**
  41. * @brief represents a c-style overflow strategy
  42. */
  43. using overflow_strategy = sl_overflow_strategy;
  44. /**
  45. * @brief represents a c-style output strategy
  46. */
  47. using output_strategy = sl_output_strategy;
  48. }
  49. template<BufferStrategy b_strategy_t, SinkStrategy s_strategy_t, OverflowStrategy over_strategy_t, OutputStrategy out_strategy_t >
  50. void register_log(std::string_view buffer_filename, uint64_t buffer_size, uint64_t out_strategy_parameter, std::string_view output_directory);
  51. template<> void register_log<BufferStrategyInternal, SinkStrategyDirect, OverflowStrategyWait, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  52. template<> void register_log<BufferStrategyInternal, SinkStrategyDirect, OverflowStrategyWait, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  53. template<> void register_log<BufferStrategyInternal, SinkStrategyDirect, OverflowStrategyWait, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  54. template<> void register_log<BufferStrategyInternal, SinkStrategyDirect, OverflowStrategyContinue, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  55. template<> void register_log<BufferStrategyInternal, SinkStrategyDirect, OverflowStrategyContinue, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  56. template<> void register_log<BufferStrategyInternal, SinkStrategyDirect, OverflowStrategyContinue, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  57. template<> void register_log<BufferStrategyInternal, SinkStrategyFastest, OverflowStrategyWait, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  58. template<> void register_log<BufferStrategyInternal, SinkStrategyFastest, OverflowStrategyWait, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  59. template<> void register_log<BufferStrategyInternal, SinkStrategyFastest, OverflowStrategyWait, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  60. template<> void register_log<BufferStrategyInternal, SinkStrategyFastest, OverflowStrategyContinue, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  61. template<> void register_log<BufferStrategyInternal, SinkStrategyFastest, OverflowStrategyContinue, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  62. template<> void register_log<BufferStrategyInternal, SinkStrategyFastest, OverflowStrategyContinue, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  63. template<> void register_log<BufferStrategyInternal, SinkStrategyMmaped, OverflowStrategyWait, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  64. template<> void register_log<BufferStrategyInternal, SinkStrategyMmaped, OverflowStrategyWait, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  65. template<> void register_log<BufferStrategyInternal, SinkStrategyMmaped, OverflowStrategyWait, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  66. template<> void register_log<BufferStrategyInternal, SinkStrategyMmaped, OverflowStrategyContinue, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  67. template<> void register_log<BufferStrategyInternal, SinkStrategyMmaped, OverflowStrategyContinue, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  68. template<> void register_log<BufferStrategyInternal, SinkStrategyMmaped, OverflowStrategyContinue, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  69. template<> void register_log<BufferStrategyInternal, SinkStrategyExternal, OverflowStrategyWait, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  70. template<> void register_log<BufferStrategyInternal, SinkStrategyExternal, OverflowStrategyWait, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  71. template<> void register_log<BufferStrategyInternal, SinkStrategyExternal, OverflowStrategyWait, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  72. template<> void register_log<BufferStrategyInternal, SinkStrategyExternal, OverflowStrategyContinue, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  73. template<> void register_log<BufferStrategyInternal, SinkStrategyExternal, OverflowStrategyContinue, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  74. template<> void register_log<BufferStrategyInternal, SinkStrategyExternal, OverflowStrategyContinue, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  75. template<> void register_log<BufferStrategyShared, SinkStrategyDirect, OverflowStrategyWait, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  76. template<> void register_log<BufferStrategyShared, SinkStrategyDirect, OverflowStrategyWait, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  77. template<> void register_log<BufferStrategyShared, SinkStrategyDirect, OverflowStrategyWait, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  78. template<> void register_log<BufferStrategyShared, SinkStrategyDirect, OverflowStrategyContinue, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  79. template<> void register_log<BufferStrategyShared, SinkStrategyDirect, OverflowStrategyContinue, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  80. template<> void register_log<BufferStrategyShared, SinkStrategyDirect, OverflowStrategyContinue, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  81. template<> void register_log<BufferStrategyShared, SinkStrategyFastest, OverflowStrategyWait, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  82. template<> void register_log<BufferStrategyShared, SinkStrategyFastest, OverflowStrategyWait, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  83. template<> void register_log<BufferStrategyShared, SinkStrategyFastest, OverflowStrategyWait, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  84. template<> void register_log<BufferStrategyShared, SinkStrategyFastest, OverflowStrategyContinue, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  85. template<> void register_log<BufferStrategyShared, SinkStrategyFastest, OverflowStrategyContinue, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  86. template<> void register_log<BufferStrategyShared, SinkStrategyFastest, OverflowStrategyContinue, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  87. template<> void register_log<BufferStrategyShared, SinkStrategyMmaped, OverflowStrategyWait, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  88. template<> void register_log<BufferStrategyShared, SinkStrategyMmaped, OverflowStrategyWait, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  89. template<> void register_log<BufferStrategyShared, SinkStrategyMmaped, OverflowStrategyWait, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  90. template<> void register_log<BufferStrategyShared, SinkStrategyMmaped, OverflowStrategyContinue, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  91. template<> void register_log<BufferStrategyShared, SinkStrategyMmaped, OverflowStrategyContinue, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  92. template<> void register_log<BufferStrategyShared, SinkStrategyMmaped, OverflowStrategyContinue, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  93. template<> void register_log<BufferStrategyShared, SinkStrategyExternal, OverflowStrategyWait, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  94. template<> void register_log<BufferStrategyShared, SinkStrategyExternal, OverflowStrategyWait, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  95. template<> void register_log<BufferStrategyShared, SinkStrategyExternal, OverflowStrategyWait, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  96. template<> void register_log<BufferStrategyShared, SinkStrategyExternal, OverflowStrategyContinue, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  97. template<> void register_log<BufferStrategyShared, SinkStrategyExternal, OverflowStrategyContinue, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  98. template<> void register_log<BufferStrategyShared, SinkStrategyExternal, OverflowStrategyContinue, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  99. template<> void register_log<BufferStrategyExternal, SinkStrategyDirect, OverflowStrategyWait, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  100. template<> void register_log<BufferStrategyExternal, SinkStrategyDirect, OverflowStrategyWait, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  101. template<> void register_log<BufferStrategyExternal, SinkStrategyDirect, OverflowStrategyWait, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  102. template<> void register_log<BufferStrategyExternal, SinkStrategyDirect, OverflowStrategyContinue, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  103. template<> void register_log<BufferStrategyExternal, SinkStrategyDirect, OverflowStrategyContinue, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  104. template<> void register_log<BufferStrategyExternal, SinkStrategyDirect, OverflowStrategyContinue, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  105. template<> void register_log<BufferStrategyExternal, SinkStrategyFastest, OverflowStrategyWait, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  106. template<> void register_log<BufferStrategyExternal, SinkStrategyFastest, OverflowStrategyWait, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  107. template<> void register_log<BufferStrategyExternal, SinkStrategyFastest, OverflowStrategyWait, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  108. template<> void register_log<BufferStrategyExternal, SinkStrategyFastest, OverflowStrategyContinue, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  109. template<> void register_log<BufferStrategyExternal, SinkStrategyFastest, OverflowStrategyContinue, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  110. template<> void register_log<BufferStrategyExternal, SinkStrategyFastest, OverflowStrategyContinue, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  111. template<> void register_log<BufferStrategyExternal, SinkStrategyMmaped, OverflowStrategyWait, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  112. template<> void register_log<BufferStrategyExternal, SinkStrategyMmaped, OverflowStrategyWait, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  113. template<> void register_log<BufferStrategyExternal, SinkStrategyMmaped, OverflowStrategyWait, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  114. template<> void register_log<BufferStrategyExternal, SinkStrategyMmaped, OverflowStrategyContinue, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  115. template<> void register_log<BufferStrategyExternal, SinkStrategyMmaped, OverflowStrategyContinue, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  116. template<> void register_log<BufferStrategyExternal, SinkStrategyMmaped, OverflowStrategyContinue, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  117. template<> void register_log<BufferStrategyExternal, SinkStrategyExternal, OverflowStrategyWait, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  118. template<> void register_log<BufferStrategyExternal, SinkStrategyExternal, OverflowStrategyWait, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  119. template<> void register_log<BufferStrategyExternal, SinkStrategyExternal, OverflowStrategyWait, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  120. template<> void register_log<BufferStrategyExternal, SinkStrategyExternal, OverflowStrategyContinue, OutputStrategyTimed>(std::string_view, uint64_t, uint64_t, std::string_view);
  121. template<> void register_log<BufferStrategyExternal, SinkStrategyExternal, OverflowStrategyContinue, OutputStrategySized>(std::string_view, uint64_t, uint64_t, std::string_view);
  122. template<> void register_log<BufferStrategyExternal, SinkStrategyExternal, OverflowStrategyContinue, OutputStrategySimple>(std::string_view, uint64_t, uint64_t, std::string_view);
  123. #else
  124. typedef struct sl_buffer_strategy sl_buffer_strategy;
  125. typedef struct sl_sink_strategy sl_sink_strategy;
  126. typedef struct sl_overflow_strategy sl_overflow_strategy;
  127. typedef struct sl_output_strategy sl_output_strategy;
  128. #endif
  129. #ifdef __cplusplus
  130. extern "C" {
  131. #endif
  132. /**
  133. * @brief Constructs a buffer strategy from C
  134. * @param strategy The type of strategy
  135. * @param filename A file describing the strategy, ignored if SL_BUFFER_TYPE is SL_INTERNAL, optional if SL_BUFFER_TYPE is SL_SHARED
  136. * @return a strategy token for building a logger
  137. */
  138. sl_buffer_strategy* sl_create_buffer_strategy(SL_BUFFER_TYPE strategy, char *filename);
  139. /**
  140. * @brief Constructs a sink strategy for how to handle IO in the logger
  141. * @param strategy The type of strategy
  142. * @return a strategy token to build a logger
  143. */
  144. sl_sink_strategy* sl_create_sink_strategy(SL_SINK_IO_TYPE strategy);
  145. /**
  146. * @brief Constructs an overflow strategy for how to handle the edge case of a full buffer
  147. * @param strategy The type of strategy
  148. * @return a strategy token to build a logger
  149. */
  150. sl_overflow_strategy* sl_create_overflow_strategy(SL_ON_SINK_FULL strategy);
  151. /**
  152. * @brief Constructs a strategy that will direct where data is logged and how it will be split
  153. * @param strategy The strategy to use
  154. * @param strategy_parameter Either the amount of hours to keep in the same file, or the file size in kilobytes
  155. * @param output_directory The directory where the logs will be written
  156. * @return a strategy token to build a logger
  157. */
  158. sl_output_strategy* sl_create_output_strategy(SL_ROLL_LOGS strategy, uint64_t strategy_parameter, char* output_directory);
  159. /**
  160. * @brief Constructs and registers a logger, readily available immediately
  161. * @param internal_id The ID with which you want to refer to the logger to access it as fast as possible
  162. * @param external_id The ID which will be part of the logs filename
  163. * @param strategies Collect the 4 tokens and reunite them into the hero's sword that will slay every other logger
  164. */
  165. void sl_register_logger(int internal_id, char* external_id, sl_buffer_strategy*, sl_sink_strategy*, sl_overflow_strategy*, sl_output_strategy*);
  166. #ifdef __cplusplus
  167. }
  168. #endif