log.cpp 9.7 KB

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