log.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. #include "log.h"
  27. #include <condition_variable>
  28. #include <cstdarg>
  29. #include <cstdio>
  30. #include <mutex>
  31. #include <sstream>
  32. #include <thread>
  33. #include <vector>
  34. int common_log_verbosity_thold = LOG_DEFAULT_LLAMA;
  35. void common_log_set_verbosity_thold(int verbosity) {
  36. common_log_verbosity_thold = verbosity;
  37. }
  38. #define LOG_COL_DEFAULT "\033[0m"
  39. #define LOG_COL_BOLD "\033[1m"
  40. #define LOG_COL_RED "\033[31m"
  41. #define LOG_COL_GREEN "\033[32m"
  42. #define LOG_COL_YELLOW "\033[33m"
  43. #define LOG_COL_BLUE "\033[34m"
  44. #define LOG_COL_MAGENTA "\033[35m"
  45. #define LOG_COL_CYAN "\033[36m"
  46. #define LOG_COL_WHITE "\033[37m"
  47. static int64_t t_us() {
  48. return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
  49. }
  50. // colors
  51. enum common_log_col : int {
  52. COMMON_LOG_COL_DEFAULT = 0,
  53. COMMON_LOG_COL_BOLD,
  54. COMMON_LOG_COL_RED,
  55. COMMON_LOG_COL_GREEN,
  56. COMMON_LOG_COL_YELLOW,
  57. COMMON_LOG_COL_BLUE,
  58. COMMON_LOG_COL_MAGENTA,
  59. COMMON_LOG_COL_CYAN,
  60. COMMON_LOG_COL_WHITE,
  61. };
  62. // disable colors by default
  63. static std::vector<const char *> g_col = {
  64. "",
  65. "",
  66. "",
  67. "",
  68. "",
  69. "",
  70. "",
  71. "",
  72. "",
  73. };
  74. struct common_log_entry {
  75. enum ggml_log_level level;
  76. bool prefix;
  77. int64_t timestamp;
  78. std::vector<char> msg;
  79. // signals the worker thread to stop
  80. bool is_end;
  81. void print(FILE * file = nullptr) const {
  82. FILE * fcur = file;
  83. if (!fcur) {
  84. // stderr displays DBG messages only when their verbosity level is not higher than the threshold
  85. // these messages will still be logged to a file
  86. if (level == GGML_LOG_LEVEL_DEBUG && common_log_verbosity_thold < LOG_DEFAULT_DEBUG) {
  87. return;
  88. }
  89. fcur = stdout;
  90. if (level != GGML_LOG_LEVEL_NONE) {
  91. fcur = stderr;
  92. }
  93. }
  94. if (level != GGML_LOG_LEVEL_NONE && level != GGML_LOG_LEVEL_CONT && prefix) {
  95. if (timestamp) {
  96. // [M.s.ms.us]
  97. fprintf(fcur, "%s%d.%02d.%03d.%03d%s ",
  98. g_col[COMMON_LOG_COL_BLUE],
  99. (int) (timestamp / 1000000 / 60),
  100. (int) (timestamp / 1000000 % 60),
  101. (int) (timestamp / 1000 % 1000),
  102. (int) (timestamp % 1000),
  103. g_col[COMMON_LOG_COL_DEFAULT]);
  104. }
  105. switch (level) {
  106. case GGML_LOG_LEVEL_INFO: fprintf(fcur, "%sI %s", g_col[COMMON_LOG_COL_GREEN], g_col[COMMON_LOG_COL_DEFAULT]); break;
  107. case GGML_LOG_LEVEL_WARN: fprintf(fcur, "%sW %s", g_col[COMMON_LOG_COL_MAGENTA], "" ); break;
  108. case GGML_LOG_LEVEL_ERROR: fprintf(fcur, "%sE %s", g_col[COMMON_LOG_COL_RED], "" ); break;
  109. case GGML_LOG_LEVEL_DEBUG: fprintf(fcur, "%sD %s", g_col[COMMON_LOG_COL_YELLOW], "" ); break;
  110. default:
  111. break;
  112. }
  113. }
  114. fprintf(fcur, "%s", msg.data());
  115. if (level == GGML_LOG_LEVEL_WARN || level == GGML_LOG_LEVEL_ERROR || level == GGML_LOG_LEVEL_DEBUG) {
  116. fprintf(fcur, "%s", g_col[COMMON_LOG_COL_DEFAULT]);
  117. }
  118. fflush(fcur);
  119. }
  120. };
  121. struct common_log {
  122. // default capacity - will be expanded if needed
  123. common_log() : common_log(256) {}
  124. common_log(size_t capacity) {
  125. file = nullptr;
  126. prefix = false;
  127. timestamps = false;
  128. running = false;
  129. t_start = t_us();
  130. // initial message size - will be expanded if longer messages arrive
  131. entries.resize(capacity);
  132. for (auto & entry : entries) {
  133. entry.msg.resize(256);
  134. }
  135. head = 0;
  136. tail = 0;
  137. resume();
  138. }
  139. ~common_log() {
  140. pause();
  141. if (file) {
  142. fclose(file);
  143. }
  144. }
  145. private:
  146. std::mutex mtx;
  147. std::thread thrd;
  148. std::condition_variable cv;
  149. FILE * file;
  150. bool prefix;
  151. bool timestamps;
  152. bool running;
  153. int64_t t_start;
  154. // ring buffer of entries
  155. std::vector<common_log_entry> entries;
  156. size_t head;
  157. size_t tail;
  158. // worker thread copies into this
  159. common_log_entry cur;
  160. public:
  161. void add(enum ggml_log_level level, const char * fmt, va_list args) {
  162. std::lock_guard<std::mutex> lock(mtx);
  163. if (!running) {
  164. // discard messages while the worker thread is paused
  165. return;
  166. }
  167. auto & entry = entries[tail];
  168. {
  169. // cannot use args twice, so make a copy in case we need to expand the buffer
  170. va_list args_copy;
  171. va_copy(args_copy, args);
  172. #if 1
  173. const size_t n = vsnprintf(entry.msg.data(), entry.msg.size(), fmt, args);
  174. if (n >= entry.msg.size()) {
  175. entry.msg.resize(n + 1);
  176. vsnprintf(entry.msg.data(), entry.msg.size(), fmt, args_copy);
  177. }
  178. #else
  179. // hack for bolding arguments
  180. std::stringstream ss;
  181. for (int i = 0; fmt[i] != 0; i++) {
  182. if (fmt[i] == '%') {
  183. ss << LOG_COL_BOLD;
  184. while (fmt[i] != ' ' && fmt[i] != ')' && fmt[i] != ']' && fmt[i] != 0) ss << fmt[i++];
  185. ss << LOG_COL_DEFAULT;
  186. if (fmt[i] == 0) break;
  187. }
  188. ss << fmt[i];
  189. }
  190. const size_t n = vsnprintf(entry.msg.data(), entry.msg.size(), ss.str().c_str(), args);
  191. if (n >= entry.msg.size()) {
  192. entry.msg.resize(n + 1);
  193. vsnprintf(entry.msg.data(), entry.msg.size(), ss.str().c_str(), args_copy);
  194. }
  195. #endif
  196. }
  197. entry.level = level;
  198. entry.prefix = prefix;
  199. entry.timestamp = 0;
  200. if (timestamps) {
  201. entry.timestamp = t_us() - t_start;
  202. }
  203. entry.is_end = false;
  204. tail = (tail + 1) % entries.size();
  205. if (tail == head) {
  206. // expand the buffer
  207. std::vector<common_log_entry> new_entries(2*entries.size());
  208. size_t new_tail = 0;
  209. do {
  210. new_entries[new_tail] = std::move(entries[head]);
  211. head = (head + 1) % entries.size();
  212. new_tail = (new_tail + 1);
  213. } while (head != tail);
  214. head = 0;
  215. tail = new_tail;
  216. for (size_t i = tail; i < new_entries.size(); i++) {
  217. new_entries[i].msg.resize(256);
  218. }
  219. entries = std::move(new_entries);
  220. }
  221. cv.notify_one();
  222. }
  223. void resume() {
  224. std::lock_guard<std::mutex> lock(mtx);
  225. if (running) {
  226. return;
  227. }
  228. running = true;
  229. thrd = std::thread([this]() {
  230. while (true) {
  231. {
  232. std::unique_lock<std::mutex> lock(mtx);
  233. cv.wait(lock, [this]() { return head != tail; });
  234. cur = entries[head];
  235. head = (head + 1) % entries.size();
  236. }
  237. if (cur.is_end) {
  238. break;
  239. }
  240. cur.print(); // stdout and stderr
  241. if (file) {
  242. cur.print(file);
  243. }
  244. }
  245. });
  246. }
  247. void pause() {
  248. {
  249. std::lock_guard<std::mutex> lock(mtx);
  250. if (!running) {
  251. return;
  252. }
  253. running = false;
  254. // push an entry to signal the worker thread to stop
  255. {
  256. auto & entry = entries[tail];
  257. entry.is_end = true;
  258. tail = (tail + 1) % entries.size();
  259. }
  260. cv.notify_one();
  261. }
  262. thrd.join();
  263. }
  264. void set_file(const char * path) {
  265. pause();
  266. if (file) {
  267. fclose(file);
  268. }
  269. if (path) {
  270. file = fopen(path, "w");
  271. } else {
  272. file = nullptr;
  273. }
  274. resume();
  275. }
  276. void set_colors(bool colors) {
  277. pause();
  278. if (colors) {
  279. g_col[COMMON_LOG_COL_DEFAULT] = LOG_COL_DEFAULT;
  280. g_col[COMMON_LOG_COL_BOLD] = LOG_COL_BOLD;
  281. g_col[COMMON_LOG_COL_RED] = LOG_COL_RED;
  282. g_col[COMMON_LOG_COL_GREEN] = LOG_COL_GREEN;
  283. g_col[COMMON_LOG_COL_YELLOW] = LOG_COL_YELLOW;
  284. g_col[COMMON_LOG_COL_BLUE] = LOG_COL_BLUE;
  285. g_col[COMMON_LOG_COL_MAGENTA] = LOG_COL_MAGENTA;
  286. g_col[COMMON_LOG_COL_CYAN] = LOG_COL_CYAN;
  287. g_col[COMMON_LOG_COL_WHITE] = LOG_COL_WHITE;
  288. } else {
  289. for (size_t i = 0; i < g_col.size(); i++) {
  290. g_col[i] = "";
  291. }
  292. }
  293. resume();
  294. }
  295. void set_prefix(bool prefix) {
  296. std::lock_guard<std::mutex> lock(mtx);
  297. this->prefix = prefix;
  298. }
  299. void set_timestamps(bool timestamps) {
  300. std::lock_guard<std::mutex> lock(mtx);
  301. this->timestamps = timestamps;
  302. }
  303. };
  304. //
  305. // public API
  306. //
  307. struct common_log * common_log_init() {
  308. return new common_log;
  309. }
  310. struct common_log * common_log_main() {
  311. static struct common_log log;
  312. return &log;
  313. }
  314. void common_log_pause(struct common_log * log) {
  315. log->pause();
  316. }
  317. void common_log_resume(struct common_log * log) {
  318. log->resume();
  319. }
  320. void common_log_free(struct common_log * log) {
  321. delete log;
  322. }
  323. void common_log_add(struct common_log * log, enum ggml_log_level level, const char * fmt, ...) {
  324. va_list args;
  325. va_start(args, fmt);
  326. log->add(level, fmt, args);
  327. va_end(args);
  328. }
  329. void common_log_set_file(struct common_log * log, const char * file) {
  330. log->set_file(file);
  331. }
  332. void common_log_set_colors(struct common_log * log, bool colors) {
  333. log->set_colors(colors);
  334. }
  335. void common_log_set_prefix(struct common_log * log, bool prefix) {
  336. log->set_prefix(prefix);
  337. }
  338. void common_log_set_timestamps(struct common_log * log, bool timestamps) {
  339. log->set_timestamps(timestamps);
  340. }