jack2 codebase
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.

106 lines
3.7KB

  1. /*
  2. * Copyright (c) 2023 Florian Walpen <dev@submerge.ch>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #ifndef SOSSO_LOGGING_HPP
  17. #define SOSSO_LOGGING_HPP
  18. #include <cstdint>
  19. #include <cstdio>
  20. namespace sosso {
  21. /*!
  22. * \brief Store the source location for logging.
  23. *
  24. * Keep its implementation close to C++20 std::source_location.
  25. * It will be replaced by that when C++20 is widely available.
  26. */
  27. struct SourceLocation {
  28. //! Get the line number in the source file.
  29. std::uint_least32_t line() const { return _line; }
  30. //! Get the column in the source file, not implemented.
  31. std::uint_least32_t column() const { return _column; }
  32. //! Get the file name of the source file.
  33. const char *file_name() const { return _file_name; }
  34. //! Get the function context in the source file.
  35. const char *function_name() const { return _function_name; }
  36. std::uint_least32_t _line;
  37. std::uint_least32_t _column;
  38. const char *_file_name;
  39. const char *_function_name;
  40. };
  41. /// Capture source location in place of this macro.
  42. #define SOSSO_LOC \
  43. SourceLocation { __LINE__, 0, __FILE__, __func__ }
  44. /*!
  45. * \brief Static logging functions.
  46. *
  47. * There are three log levels:
  48. * - warn() indicates warnings and errors.
  49. * - info() provides general information to the user.
  50. * - log() is for low-level information and debugging purposes.
  51. *
  52. * The single message static logging functions have to be implemented in the
  53. * application, so they output to the appropriate places. Otherwise there will
  54. * be a linking error at build time. To give some context for debugging, the
  55. * source location is given.
  56. *
  57. * For printf-style message composition use the corresponding variable argument
  58. * function templates, limited to 255 character length.
  59. */
  60. class Log {
  61. public:
  62. //! Single message low-level log, implement this in the application.
  63. static void log(SourceLocation location, const char *message);
  64. //! Compose printf-style low-level log messages.
  65. template <typename... Args>
  66. static void log(SourceLocation location, const char *message, Args... args) {
  67. char formatted[256];
  68. std::snprintf(formatted, 256, message, args...);
  69. log(location, formatted);
  70. }
  71. //! Single message user information, implement this in the application.
  72. static void info(SourceLocation location, const char *message);
  73. //! Compose printf-style user information messages.
  74. template <typename... Args>
  75. static void info(SourceLocation location, const char *message, Args... args) {
  76. char formatted[256];
  77. std::snprintf(formatted, 256, message, args...);
  78. info(location, formatted);
  79. }
  80. //! Single message warning, implement this in the application.
  81. static void warn(SourceLocation location, const char *message);
  82. //! Compose printf-style warning messages.
  83. template <typename... Args>
  84. static void warn(SourceLocation location, const char *message, Args... args) {
  85. char formatted[256];
  86. std::snprintf(formatted, 256, message, args...);
  87. warn(location, formatted);
  88. }
  89. };
  90. } // namespace sosso
  91. #endif // SOSSO_LOGGING_HPP