log.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * llama.cpp - commit ba1cb19cdd0d92e012e0f6e009e0620f854b6afd - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #pragma once
  27. #include "ggml.h" // for ggml_log_level
  28. #ifndef __GNUC__
  29. # define LOG_ATTRIBUTE_FORMAT(...)
  30. #elif defined(__MINGW32__)
  31. # define LOG_ATTRIBUTE_FORMAT(...) __attribute__((format(gnu_printf, __VA_ARGS__)))
  32. #else
  33. # define LOG_ATTRIBUTE_FORMAT(...) __attribute__((format(printf, __VA_ARGS__)))
  34. #endif
  35. #define LOG_DEFAULT_DEBUG 1
  36. #define LOG_DEFAULT_LLAMA 0
  37. // needed by the LOG_TMPL macro to avoid computing log arguments if the verbosity lower
  38. // set via common_log_set_verbosity()
  39. extern int common_log_verbosity_thold;
  40. void common_log_set_verbosity_thold(int verbosity); // not thread-safe
  41. // the common_log uses an internal worker thread to print/write log messages
  42. // when the worker thread is paused, incoming log messages are discarded
  43. struct common_log;
  44. struct common_log * common_log_init();
  45. struct common_log * common_log_main(); // singleton, automatically destroys itself on exit
  46. void common_log_pause (struct common_log * log); // pause the worker thread, not thread-safe
  47. void common_log_resume(struct common_log * log); // resume the worker thread, not thread-safe
  48. void common_log_free (struct common_log * log);
  49. LOG_ATTRIBUTE_FORMAT(3, 4)
  50. void common_log_add(struct common_log * log, enum ggml_log_level level, const char * fmt, ...);
  51. // defaults: file = NULL, colors = false, prefix = false, timestamps = false
  52. //
  53. // regular log output:
  54. //
  55. // ggml_backend_metal_log_allocated_size: allocated buffer, size = 6695.84 MiB, ( 6695.91 / 21845.34)
  56. // llm_load_tensors: ggml ctx size = 0.27 MiB
  57. // llm_load_tensors: offloading 32 repeating layers to GPU
  58. // llm_load_tensors: offloading non-repeating layers to GPU
  59. //
  60. // with prefix = true, timestamps = true, the log output will look like this:
  61. //
  62. // 0.00.035.060 D ggml_backend_metal_log_allocated_size: allocated buffer, size = 6695.84 MiB, ( 6695.91 / 21845.34)
  63. // 0.00.035.064 I llm_load_tensors: ggml ctx size = 0.27 MiB
  64. // 0.00.090.578 I llm_load_tensors: offloading 32 repeating layers to GPU
  65. // 0.00.090.579 I llm_load_tensors: offloading non-repeating layers to GPU
  66. //
  67. // I - info (stdout, V = 0)
  68. // W - warning (stderr, V = 0)
  69. // E - error (stderr, V = 0)
  70. // D - debug (stderr, V = LOG_DEFAULT_DEBUG)
  71. //
  72. void common_log_set_file (struct common_log * log, const char * file); // not thread-safe
  73. void common_log_set_colors (struct common_log * log, bool colors); // not thread-safe
  74. void common_log_set_prefix (struct common_log * log, bool prefix); // whether to output prefix to each log
  75. void common_log_set_timestamps(struct common_log * log, bool timestamps); // whether to output timestamps in the prefix
  76. // helper macros for logging
  77. // use these to avoid computing log arguments if the verbosity of the log is higher than the threshold
  78. //
  79. // for example:
  80. //
  81. // LOG_DBG("this is a debug message: %d\n", expensive_function());
  82. //
  83. // this will avoid calling expensive_function() if LOG_DEFAULT_DEBUG > common_log_verbosity_thold
  84. //
  85. #define LOG_TMPL(level, verbosity, ...) \
  86. do { \
  87. if ((verbosity) <= common_log_verbosity_thold) { \
  88. common_log_add(common_log_main(), (level), __VA_ARGS__); \
  89. } \
  90. } while (0)
  91. #define LOG(...) LOG_TMPL(GGML_LOG_LEVEL_NONE, 0, __VA_ARGS__)
  92. #define LOGV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_NONE, verbosity, __VA_ARGS__)
  93. #define LOG_INF(...) LOG_TMPL(GGML_LOG_LEVEL_INFO, 0, __VA_ARGS__)
  94. #define LOG_WRN(...) LOG_TMPL(GGML_LOG_LEVEL_WARN, 0, __VA_ARGS__)
  95. #define LOG_ERR(...) LOG_TMPL(GGML_LOG_LEVEL_ERROR, 0, __VA_ARGS__)
  96. #define LOG_DBG(...) LOG_TMPL(GGML_LOG_LEVEL_DEBUG, LOG_DEFAULT_DEBUG, __VA_ARGS__)
  97. #define LOG_CNT(...) LOG_TMPL(GGML_LOG_LEVEL_CONT, 0, __VA_ARGS__)
  98. #define LOG_INFV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_INFO, verbosity, __VA_ARGS__)
  99. #define LOG_WRNV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_WARN, verbosity, __VA_ARGS__)
  100. #define LOG_ERRV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_ERROR, verbosity, __VA_ARGS__)
  101. #define LOG_DBGV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_DEBUG, verbosity, __VA_ARGS__)
  102. #define LOG_CNTV(verbosity, ...) LOG_TMPL(GGML_LOG_LEVEL_CONT, verbosity, __VA_ARGS__)