You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

spdlog.h 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // Copyright(c) 2015 Gabi Melman.
  3. // Distributed under the MIT License (http://opensource.org/licenses/MIT)
  4. //
  5. // spdlog main header file.
  6. // see example.cpp for usage example
  7. #pragma once
  8. #include <spdlog/tweakme.h>
  9. #include <spdlog/common.h>
  10. #include <spdlog/logger.h>
  11. #include <memory>
  12. #include <functional>
  13. #include <chrono>
  14. #include <string>
  15. namespace spdlog
  16. {
  17. //
  18. // Return an existing logger or nullptr if a logger with such name doesn't exist.
  19. // example: spdlog::get("my_logger")->info("hello {}", "world");
  20. //
  21. std::shared_ptr<logger> get(const std::string& name);
  22. //
  23. // Set global formatting
  24. // example: spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v");
  25. //
  26. void set_pattern(const std::string& format_string);
  27. void set_formatter(formatter_ptr f);
  28. //
  29. // Set global logging level for
  30. //
  31. void set_level(level::level_enum log_level);
  32. //
  33. // Set global error handler
  34. //
  35. void set_error_handler(log_err_handler);
  36. //
  37. // Turn on async mode (off by default) and set the queue size for each async_logger.
  38. // effective only for loggers created after this call.
  39. // queue_size: size of queue (must be power of 2):
  40. // Each logger will pre-allocate a dedicated queue with queue_size entries upon construction.
  41. //
  42. // async_overflow_policy (optional, block_retry by default):
  43. // async_overflow_policy::block_retry - if queue is full, block until queue has room for the new log entry.
  44. // async_overflow_policy::discard_log_msg - never block and discard any new messages when queue overflows.
  45. //
  46. // worker_warmup_cb (optional):
  47. // callback function that will be called in worker thread upon start (can be used to init stuff like thread affinity)
  48. //
  49. // worker_teardown_cb (optional):
  50. // callback function that will be called in worker thread upon exit
  51. //
  52. void set_async_mode(size_t queue_size, const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, const std::function<void()>& worker_warmup_cb = nullptr, const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), const std::function<void()>& worker_teardown_cb = nullptr);
  53. // Turn off async mode
  54. void set_sync_mode();
  55. //
  56. // Create and register multi/single threaded basic file logger.
  57. // Basic logger simply writes to given file without any limitatons or rotations.
  58. //
  59. std::shared_ptr<logger> basic_logger_mt(const std::string& logger_name, const filename_t& filename, bool truncate = false);
  60. std::shared_ptr<logger> basic_logger_st(const std::string& logger_name, const filename_t& filename, bool truncate = false);
  61. //
  62. // Create and register multi/single threaded rotating file logger
  63. //
  64. std::shared_ptr<logger> rotating_logger_mt(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files);
  65. std::shared_ptr<logger> rotating_logger_st(const std::string& logger_name, const filename_t& filename, size_t max_file_size, size_t max_files);
  66. //
  67. // Create file logger which creates new file on the given time (default in midnight):
  68. //
  69. std::shared_ptr<logger> daily_logger_mt(const std::string& logger_name, const filename_t& filename, int hour=0, int minute=0);
  70. std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const filename_t& filename, int hour=0, int minute=0);
  71. //
  72. // Create and register stdout/stderr loggers
  73. //
  74. std::shared_ptr<logger> stdout_logger_mt(const std::string& logger_name, bool color = false);
  75. std::shared_ptr<logger> stdout_logger_st(const std::string& logger_name, bool color = false);
  76. std::shared_ptr<logger> stderr_logger_mt(const std::string& logger_name, bool color = false);
  77. std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name, bool color = false);
  78. //
  79. // Create and register a syslog logger
  80. //
  81. #ifdef SPDLOG_ENABLE_SYSLOG
  82. std::shared_ptr<logger> syslog_logger(const std::string& logger_name, const std::string& ident = "", int syslog_option = 0);
  83. #endif
  84. #if defined(__ANDROID__)
  85. std::shared_ptr<logger> android_logger(const std::string& logger_name, const std::string& tag = "spdlog");
  86. #endif
  87. // Create and register a logger a single sink
  88. std::shared_ptr<logger> create(const std::string& logger_name, const sink_ptr& sink);
  89. // Create and register a logger with multiple sinks
  90. std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks);
  91. template<class It>
  92. std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end);
  93. // Create and register a logger with templated sink type
  94. // Example:
  95. // spdlog::create<daily_file_sink_st>("mylog", "dailylog_filename", "txt");
  96. template <typename Sink, typename... Args>
  97. std::shared_ptr<spdlog::logger> create(const std::string& logger_name, Args...);
  98. // Register the given logger with the given name
  99. void register_logger(std::shared_ptr<logger> logger);
  100. // Apply a user defined function on all registered loggers
  101. // Example:
  102. // spdlog::apply_all([&](std::shared_ptr<spdlog::logger> l) {l->flush();});
  103. void apply_all(std::function<void(std::shared_ptr<logger>)> fun);
  104. // Drop the reference to the given logger
  105. void drop(const std::string &name);
  106. // Drop all references from the registry
  107. void drop_all();
  108. ///////////////////////////////////////////////////////////////////////////////
  109. //
  110. // Trace & Debug can be switched on/off at compile time for zero cost debug statements.
  111. // Uncomment SPDLOG_DEBUG_ON/SPDLOG_TRACE_ON in teakme.h to enable.
  112. // SPDLOG_TRACE(..) will also print current file and line.
  113. //
  114. // Example:
  115. // spdlog::set_level(spdlog::level::trace);
  116. // SPDLOG_TRACE(my_logger, "some trace message");
  117. // SPDLOG_TRACE(my_logger, "another trace message {} {}", 1, 2);
  118. // SPDLOG_DEBUG(my_logger, "some debug message {} {}", 3, 4);
  119. ///////////////////////////////////////////////////////////////////////////////
  120. #ifdef SPDLOG_TRACE_ON
  121. #define SPDLOG_STR_H(x) #x
  122. #define SPDLOG_STR_HELPER(x) SPDLOG_STR_H(x)
  123. #define SPDLOG_TRACE(logger, ...) logger->trace("[" __FILE__ " line #" SPDLOG_STR_HELPER(__LINE__) "] " __VA_ARGS__)
  124. #else
  125. #define SPDLOG_TRACE(logger, ...)
  126. #endif
  127. #ifdef SPDLOG_DEBUG_ON
  128. #define SPDLOG_DEBUG(logger, ...) logger->debug(__VA_ARGS__)
  129. #else
  130. #define SPDLOG_DEBUG(logger, ...)
  131. #endif
  132. }
  133. #include <spdlog/details/spdlog_impl.h>