log.cpp 9.7 KB

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