log.cpp 10 KB

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