log.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. #pragma once
  2. #include <chrono>
  3. #include <cstring>
  4. #include <sstream>
  5. #include <iostream>
  6. #include <thread>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <cinttypes>
  10. // --------------------------------
  11. //
  12. // Basic usage:
  13. //
  14. // --------
  15. //
  16. // The LOG() and LOG_TEE() macros are ready to go by default
  17. // they do not require any initialization.
  18. //
  19. // LOGLN() and LOG_TEELN() are variants which automatically
  20. // include \n character at the end of the log string.
  21. //
  22. // LOG() behaves exactly like printf, by default writing to a logfile.
  23. // LOG_TEE() additionally, prints to the screen too ( mimics Unix tee command ).
  24. //
  25. // Default logfile is named
  26. // "llama.<threadID>.log"
  27. // Default LOG_TEE() secondary output target is
  28. // stderr
  29. //
  30. // Logs can be dynamically disabled or enabled using functions:
  31. // log_disable()
  32. // and
  33. // log_enable()
  34. //
  35. // A log target can be changed with:
  36. // log_set_target( string )
  37. // creating and opening, or re-opening a file by string filename
  38. // or
  39. // log_set_target( FILE* )
  40. // allowing to point at stderr, stdout, or any valid FILE* file handler.
  41. //
  42. // --------
  43. //
  44. // End of Basic usage.
  45. //
  46. // --------------------------------
  47. // Specifies a log target.
  48. // default uses log_handler() with "llama.log" log file
  49. // this can be changed, by defining LOG_TARGET
  50. // like so:
  51. //
  52. // #define LOG_TARGET (a valid FILE*)
  53. // #include "log.h"
  54. //
  55. // or it can be simply redirected to stdout or stderr
  56. // like so:
  57. //
  58. // #define LOG_TARGET stderr
  59. // #include "log.h"
  60. //
  61. // The log target can also be redirected to a diffrent function
  62. // like so:
  63. //
  64. // #define LOG_TARGET log_handler_diffrent()
  65. // #include "log.h"
  66. //
  67. // FILE* log_handler_diffrent()
  68. // {
  69. // return stderr;
  70. // }
  71. //
  72. // or:
  73. //
  74. // #define LOG_TARGET log_handler_another_one("somelog.log")
  75. // #include "log.h"
  76. //
  77. // FILE* log_handler_another_one(char*filename)
  78. // {
  79. // static FILE* logfile = nullptr;
  80. // (...)
  81. // if( !logfile )
  82. // {
  83. // fopen(...)
  84. // }
  85. // (...)
  86. // return logfile
  87. // }
  88. //
  89. #ifndef LOG_TARGET
  90. #define LOG_TARGET log_handler()
  91. #endif
  92. #ifndef LOG_TEE_TARGET
  93. #define LOG_TEE_TARGET stderr
  94. #endif
  95. // NOTE: currently disabled as it produces too many log files
  96. // Utility to obtain "pid" like unique process id and use it when creating log files.
  97. //inline std::string log_get_pid()
  98. //{
  99. // static std::string pid;
  100. // if (pid.empty())
  101. // {
  102. // // std::this_thread::get_id() is the most portable way of obtaining a "process id"
  103. // // it's not the same as "pid" but is unique enough to solve multiple instances
  104. // // trying to write to the same log.
  105. // std::stringstream ss;
  106. // ss << std::this_thread::get_id();
  107. // pid = ss.str();
  108. // }
  109. //
  110. // return pid;
  111. //}
  112. // Utility function for generating log file names with unique id based on thread id.
  113. // invocation with log_filename_generator( "llama", "log" ) creates a string "llama.<number>.log"
  114. // where the number is a runtime id of the current thread.
  115. #define log_filename_generator(log_file_basename, log_file_extension) log_filename_generator_impl(log_file_basename, log_file_extension)
  116. // INTERNAL, DO NOT USE
  117. inline std::string log_filename_generator_impl(const std::string & log_file_basename, const std::string & log_file_extension)
  118. {
  119. std::stringstream buf;
  120. buf << log_file_basename;
  121. //buf << ".";
  122. //buf << log_get_pid();
  123. buf << ".";
  124. buf << log_file_extension;
  125. return buf.str();
  126. }
  127. #ifndef LOG_DEFAULT_FILE_NAME
  128. #define LOG_DEFAULT_FILE_NAME log_filename_generator("llama", "log")
  129. #endif
  130. // Utility for turning #define values into string literals
  131. // so we can have a define for stderr and
  132. // we can print "stderr" instead of literal stderr, etc.
  133. #define LOG_STRINGIZE1(s) #s
  134. #define LOG_STRINGIZE(s) LOG_STRINGIZE1(s)
  135. #define LOG_TEE_TARGET_STRING LOG_STRINGIZE(LOG_TEE_TARGET)
  136. // Allows disabling timestamps.
  137. // in order to disable, define LOG_NO_TIMESTAMPS
  138. // like so:
  139. //
  140. // #define LOG_NO_TIMESTAMPS
  141. // #include "log.h"
  142. //
  143. #ifndef LOG_NO_TIMESTAMPS
  144. #ifndef _MSC_VER
  145. #define LOG_TIMESTAMP_FMT "[%" PRIu64 "] "
  146. #define LOG_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count()
  147. #else
  148. #define LOG_TIMESTAMP_FMT "[%" PRIu64 "] "
  149. #define LOG_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count()
  150. #endif
  151. #else
  152. #define LOG_TIMESTAMP_FMT "%s"
  153. #define LOG_TIMESTAMP_VAL ,""
  154. #endif
  155. #ifdef LOG_TEE_TIMESTAMPS
  156. #ifndef _MSC_VER
  157. #define LOG_TEE_TIMESTAMP_FMT "[%" PRIu64 "] "
  158. #define LOG_TEE_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count()
  159. #else
  160. #define LOG_TEE_TIMESTAMP_FMT "[%" PRIu64 "] "
  161. #define LOG_TEE_TIMESTAMP_VAL , (std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(std::chrono::system_clock::now().time_since_epoch())).count()
  162. #endif
  163. #else
  164. #define LOG_TEE_TIMESTAMP_FMT "%s"
  165. #define LOG_TEE_TIMESTAMP_VAL ,""
  166. #endif
  167. // Allows disabling file/line/function prefix
  168. // in order to disable, define LOG_NO_FILE_LINE_FUNCTION
  169. // like so:
  170. //
  171. // #define LOG_NO_FILE_LINE_FUNCTION
  172. // #include "log.h"
  173. //
  174. #ifndef LOG_NO_FILE_LINE_FUNCTION
  175. #ifndef _MSC_VER
  176. #define LOG_FLF_FMT "[%24s:%5d][%24s] "
  177. #define LOG_FLF_VAL , __FILE__, __LINE__, __FUNCTION__
  178. #else
  179. #define LOG_FLF_FMT "[%24s:%5ld][%24s] "
  180. #define LOG_FLF_VAL , __FILE__, __LINE__, __FUNCTION__
  181. #endif
  182. #else
  183. #define LOG_FLF_FMT "%s"
  184. #define LOG_FLF_VAL ,""
  185. #endif
  186. #ifdef LOG_TEE_FILE_LINE_FUNCTION
  187. #ifndef _MSC_VER
  188. #define LOG_TEE_FLF_FMT "[%24s:%5d][%24s] "
  189. #define LOG_TEE_FLF_VAL , __FILE__, __LINE__, __FUNCTION__
  190. #else
  191. #define LOG_TEE_FLF_FMT "[%24s:%5ld][%24s] "
  192. #define LOG_TEE_FLF_VAL , __FILE__, __LINE__, __FUNCTION__
  193. #endif
  194. #else
  195. #define LOG_TEE_FLF_FMT "%s"
  196. #define LOG_TEE_FLF_VAL ,""
  197. #endif
  198. // Utility for synchronizing log configuration state
  199. // since std::optional was introduced only in c++17
  200. enum LogTriState
  201. {
  202. LogTriStateSame,
  203. LogTriStateFalse,
  204. LogTriStateTrue
  205. };
  206. // INTERNAL, DO NOT USE
  207. // USE LOG() INSTEAD
  208. //
  209. #ifndef _MSC_VER
  210. #define LOG_IMPL(str, ...) \
  211. do { \
  212. if (LOG_TARGET != nullptr) \
  213. { \
  214. fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL, __VA_ARGS__); \
  215. fflush(LOG_TARGET); \
  216. } \
  217. } while (0)
  218. #else
  219. #define LOG_IMPL(str, ...) \
  220. do { \
  221. if (LOG_TARGET != nullptr) \
  222. { \
  223. fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL "", ##__VA_ARGS__); \
  224. fflush(LOG_TARGET); \
  225. } \
  226. } while (0)
  227. #endif
  228. // INTERNAL, DO NOT USE
  229. // USE LOG_TEE() INSTEAD
  230. //
  231. #ifndef _MSC_VER
  232. #define LOG_TEE_IMPL(str, ...) \
  233. do { \
  234. if (LOG_TARGET != nullptr) \
  235. { \
  236. fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL, __VA_ARGS__); \
  237. fflush(LOG_TARGET); \
  238. } \
  239. if (LOG_TARGET != nullptr && LOG_TARGET != stdout && LOG_TARGET != stderr && LOG_TEE_TARGET != nullptr) \
  240. { \
  241. fprintf(LOG_TEE_TARGET, LOG_TEE_TIMESTAMP_FMT LOG_TEE_FLF_FMT str "%s" LOG_TEE_TIMESTAMP_VAL LOG_TEE_FLF_VAL, __VA_ARGS__); \
  242. fflush(LOG_TEE_TARGET); \
  243. } \
  244. } while (0)
  245. #else
  246. #define LOG_TEE_IMPL(str, ...) \
  247. do { \
  248. if (LOG_TARGET != nullptr) \
  249. { \
  250. fprintf(LOG_TARGET, LOG_TIMESTAMP_FMT LOG_FLF_FMT str "%s" LOG_TIMESTAMP_VAL LOG_FLF_VAL "", ##__VA_ARGS__); \
  251. fflush(LOG_TARGET); \
  252. } \
  253. if (LOG_TARGET != nullptr && LOG_TARGET != stdout && LOG_TARGET != stderr && LOG_TEE_TARGET != nullptr) \
  254. { \
  255. fprintf(LOG_TEE_TARGET, LOG_TEE_TIMESTAMP_FMT LOG_TEE_FLF_FMT str "%s" LOG_TEE_TIMESTAMP_VAL LOG_TEE_FLF_VAL "", ##__VA_ARGS__); \
  256. fflush(LOG_TEE_TARGET); \
  257. } \
  258. } while (0)
  259. #endif
  260. // The '\0' as a last argument, is a trick to bypass the silly
  261. // "warning: ISO C++11 requires at least one argument for the "..." in a variadic macro"
  262. // so we can have a single macro which can be called just like printf.
  263. // Main LOG macro.
  264. // behaves like printf, and supports arguments the exact same way.
  265. //
  266. #ifndef _MSC_VER
  267. #define LOG(...) LOG_IMPL(__VA_ARGS__, "")
  268. #else
  269. #define LOG(str, ...) LOG_IMPL("%s" str, "", __VA_ARGS__, "")
  270. #endif
  271. // Main TEE macro.
  272. // does the same as LOG
  273. // and
  274. // simultaneously writes stderr.
  275. //
  276. // Secondary target can be changed just like LOG_TARGET
  277. // by defining LOG_TEE_TARGET
  278. //
  279. #ifndef _MSC_VER
  280. #define LOG_TEE(...) LOG_TEE_IMPL(__VA_ARGS__, "")
  281. #else
  282. #define LOG_TEE(str, ...) LOG_TEE_IMPL("%s" str, "", __VA_ARGS__, "")
  283. #endif
  284. // LOG macro variants with auto endline.
  285. #ifndef _MSC_VER
  286. #define LOGLN(...) LOG_IMPL(__VA_ARGS__, "\n")
  287. #define LOG_TEELN(...) LOG_TEE_IMPL(__VA_ARGS__, "\n")
  288. #else
  289. #define LOGLN(str, ...) LOG_IMPL("%s" str, "", __VA_ARGS__, "\n")
  290. #define LOG_TEELN(str, ...) LOG_TEE_IMPL("%s" str, "", __VA_ARGS__, "\n")
  291. #endif
  292. // INTERNAL, DO NOT USE
  293. inline FILE *log_handler1_impl(bool change = false, LogTriState disable = LogTriStateSame, const std::string & filename = LOG_DEFAULT_FILE_NAME, FILE *target = nullptr)
  294. {
  295. static bool _initialized{false};
  296. static bool _disabled{(filename.empty() && target == nullptr)};
  297. static std::string log_current_filename{filename};
  298. static FILE *log_current_target{target};
  299. static FILE *logfile = nullptr;
  300. if (change)
  301. {
  302. if (disable == LogTriStateTrue)
  303. {
  304. // Disable primary target
  305. _disabled = true;
  306. }
  307. // If previously disabled, only enable, and keep previous target
  308. else if (disable == LogTriStateFalse)
  309. {
  310. _disabled = false;
  311. }
  312. // Otherwise, process the arguments
  313. else if (log_current_filename != filename || log_current_target != target)
  314. {
  315. _initialized = false;
  316. }
  317. }
  318. if (_disabled)
  319. {
  320. // Log is disabled
  321. return nullptr;
  322. }
  323. if (_initialized)
  324. {
  325. // with fallback in case something went wrong
  326. return logfile ? logfile : stderr;
  327. }
  328. // do the (re)initialization
  329. if (target != nullptr)
  330. {
  331. if (logfile != nullptr && logfile != stdout && logfile != stderr)
  332. {
  333. fclose(logfile);
  334. }
  335. log_current_filename = LOG_DEFAULT_FILE_NAME;
  336. log_current_target = target;
  337. logfile = target;
  338. }
  339. else
  340. {
  341. if (log_current_filename != filename)
  342. {
  343. if (logfile != nullptr && logfile != stdout && logfile != stderr)
  344. {
  345. fclose(logfile);
  346. }
  347. }
  348. logfile = fopen(filename.c_str(), "w");
  349. }
  350. if (!logfile)
  351. {
  352. // Verify whether the file was opened, otherwise fallback to stderr
  353. logfile = stderr;
  354. fprintf(stderr, "Failed to open logfile '%s' with error '%s'\n", filename.c_str(), std::strerror(errno));
  355. fflush(stderr);
  356. // At this point we let the init flag be to true below, and let the target fallback to stderr
  357. // otherwise we would repeatedly fopen() which was already unsuccessful
  358. }
  359. _initialized = true;
  360. return logfile ? logfile : stderr;
  361. }
  362. // INTERNAL, DO NOT USE
  363. inline FILE *log_handler2_impl(bool change = false, LogTriState disable = LogTriStateSame, FILE *target = nullptr, const std::string & filename = LOG_DEFAULT_FILE_NAME)
  364. {
  365. return log_handler1_impl(change, disable, filename, target);
  366. }
  367. // Disables logs entirely at runtime.
  368. // Makes LOG() and LOG_TEE() produce no output,
  369. // untill enabled back.
  370. #define log_disable() log_disable_impl()
  371. // INTERNAL, DO NOT USE
  372. inline FILE *log_disable_impl()
  373. {
  374. return log_handler1_impl(true, LogTriStateTrue);
  375. }
  376. // Enables logs at runtime.
  377. #define log_enable() log_enable_impl()
  378. // INTERNAL, DO NOT USE
  379. inline FILE *log_enable_impl()
  380. {
  381. return log_handler1_impl(true, LogTriStateFalse);
  382. }
  383. // Sets target fir logs, either by a file name or FILE* pointer (stdout, stderr, or any valid FILE*)
  384. #define log_set_target(target) log_set_target_impl(target)
  385. // INTERNAL, DO NOT USE
  386. inline FILE *log_set_target_impl(const std::string & filename) { return log_handler1_impl(true, LogTriStateSame, filename); }
  387. inline FILE *log_set_target_impl(FILE *target) { return log_handler2_impl(true, LogTriStateSame, target); }
  388. // INTERNAL, DO NOT USE
  389. inline FILE *log_handler() { return log_handler1_impl(); }
  390. inline void log_test()
  391. {
  392. log_disable();
  393. LOG("01 Hello World to nobody, because logs are disabled!\n");
  394. log_enable();
  395. LOG("02 Hello World to default output, which is \"%s\" ( Yaaay, arguments! )!\n", LOG_STRINGIZE(LOG_TARGET));
  396. LOG_TEE("03 Hello World to **both** default output and " LOG_TEE_TARGET_STRING "!\n");
  397. log_set_target(stderr);
  398. LOG("04 Hello World to stderr!\n");
  399. LOG_TEE("05 Hello World TEE with double printing to stderr prevented!\n");
  400. log_set_target(LOG_DEFAULT_FILE_NAME);
  401. LOG("06 Hello World to default log file!\n");
  402. log_set_target(stdout);
  403. LOG("07 Hello World to stdout!\n");
  404. log_set_target(LOG_DEFAULT_FILE_NAME);
  405. LOG("08 Hello World to default log file again!\n");
  406. log_disable();
  407. LOG("09 Hello World _1_ into the void!\n");
  408. log_enable();
  409. LOG("10 Hello World back from the void ( you should not see _1_ in the log or the output )!\n");
  410. log_disable();
  411. log_set_target("llama.anotherlog.log");
  412. LOG("11 Hello World _2_ to nobody, new target was selected but logs are still disabled!\n");
  413. log_enable();
  414. LOG("12 Hello World this time in a new file ( you should not see _2_ in the log or the output )?\n");
  415. log_set_target("llama.yetanotherlog.log");
  416. LOG("13 Hello World this time in yet new file?\n");
  417. log_set_target(log_filename_generator("llama_autonamed", "log"));
  418. LOG("14 Hello World in log with generated filename!\n");
  419. #ifdef _MSC_VER
  420. LOG_TEE("15 Hello msvc TEE without arguments\n");
  421. LOG_TEE("16 Hello msvc TEE with (%d)(%s) arguments\n", 1, "test");
  422. LOG_TEELN("17 Hello msvc TEELN without arguments\n");
  423. LOG_TEELN("18 Hello msvc TEELN with (%d)(%s) arguments\n", 1, "test");
  424. LOG("19 Hello msvc LOG without arguments\n");
  425. LOG("20 Hello msvc LOG with (%d)(%s) arguments\n", 1, "test");
  426. LOGLN("21 Hello msvc LOGLN without arguments\n");
  427. LOGLN("22 Hello msvc LOGLN with (%d)(%s) arguments\n", 1, "test");
  428. #endif
  429. }
  430. inline bool log_param_single_parse(const std::string & param)
  431. {
  432. if ( param == "--log-test")
  433. {
  434. log_test();
  435. return true;
  436. }
  437. if ( param == "--log-disable")
  438. {
  439. log_disable();
  440. return true;
  441. }
  442. if ( param == "--log-enable")
  443. {
  444. log_enable();
  445. return true;
  446. }
  447. return false;
  448. }
  449. inline bool log_param_pair_parse(bool check_but_dont_parse, const std::string & param, const std::string & next = std::string())
  450. {
  451. if ( param == "--log-file")
  452. {
  453. if (!check_but_dont_parse)
  454. {
  455. log_set_target(log_filename_generator(next.empty() ? "unnamed" : next, "log"));
  456. }
  457. return true;
  458. }
  459. return false;
  460. }
  461. inline void log_print_usage()
  462. {
  463. printf("log options:\n");
  464. /* format
  465. printf(" -h, --help show this help message and exit\n");*/
  466. /* spacing
  467. printf("__-param----------------Description\n");*/
  468. printf(" --log-test Run simple logging test\n");
  469. printf(" --log-disable Disable trace logs\n");
  470. printf(" --log-enable Enable trace logs\n");
  471. printf(" --log-file Specify a log filename (without extension)\n");
  472. printf(" Log file will be tagged with unique ID and written as \"<name>.<ID>.log\"\n"); /* */
  473. }
  474. #define log_dump_cmdline(argc, argv) log_dump_cmdline_impl(argc, argv)
  475. // INTERNAL, DO NOT USE
  476. inline void log_dump_cmdline_impl(int argc, char **argv)
  477. {
  478. std::stringstream buf;
  479. for (int i = 0; i < argc; ++i)
  480. {
  481. if (std::string(argv[i]).find(' ') != std::string::npos)
  482. {
  483. buf << " \"" << argv[i] <<"\"";
  484. }
  485. else
  486. {
  487. buf << " " << argv[i];
  488. }
  489. }
  490. LOGLN("Cmd:%s", buf.str().c_str());
  491. }
  492. #define log_tostr(var) log_var_to_string_impl(var).c_str()
  493. inline std::string log_var_to_string_impl(bool var)
  494. {
  495. return var ? "true" : "false";
  496. }
  497. inline std::string log_var_to_string_impl(std::string var)
  498. {
  499. return var;
  500. }
  501. inline std::string log_var_to_string_impl(const std::vector<int> & var)
  502. {
  503. std::stringstream buf;
  504. buf << "[ ";
  505. bool first = true;
  506. for (auto e : var)
  507. {
  508. if (first)
  509. {
  510. first = false;
  511. }
  512. else
  513. {
  514. buf << ", ";
  515. }
  516. buf << std::to_string(e);
  517. }
  518. buf << " ]";
  519. return buf.str();
  520. }
  521. template <typename C, typename T>
  522. inline std::string LOG_TOKENS_TOSTR_PRETTY(const C & ctx, const T & tokens)
  523. {
  524. std::stringstream buf;
  525. buf << "[ ";
  526. bool first = true;
  527. for (const auto &token : tokens)
  528. {
  529. if (!first) {
  530. buf << ", ";
  531. } else {
  532. first = false;
  533. }
  534. auto detokenized = llama_token_to_piece(ctx, token);
  535. detokenized.erase(
  536. std::remove_if(
  537. detokenized.begin(),
  538. detokenized.end(),
  539. [](const unsigned char c) { return !std::isprint(c); }),
  540. detokenized.end());
  541. buf
  542. << "'" << detokenized << "'"
  543. << ":" << std::to_string(token);
  544. }
  545. buf << " ]";
  546. return buf.str();
  547. }
  548. template <typename C, typename B>
  549. inline std::string LOG_BATCH_TOSTR_PRETTY(const C & ctx, const B & batch)
  550. {
  551. std::stringstream buf;
  552. buf << "[ ";
  553. bool first = true;
  554. for (int i = 0; i < batch.n_tokens; ++i)
  555. {
  556. if (!first) {
  557. buf << ", ";
  558. } else {
  559. first = false;
  560. }
  561. auto detokenized = llama_token_to_piece(ctx, batch.token[i]);
  562. detokenized.erase(
  563. std::remove_if(
  564. detokenized.begin(),
  565. detokenized.end(),
  566. [](const unsigned char c) { return !std::isprint(c); }),
  567. detokenized.end());
  568. buf
  569. << "\n" << std::to_string(i)
  570. << ":token '" << detokenized << "'"
  571. << ":pos " << std::to_string(batch.pos[i])
  572. << ":n_seq_id " << std::to_string(batch.n_seq_id[i])
  573. << ":seq_id " << std::to_string(batch.seq_id[i][0])
  574. << ":logits " << std::to_string(batch.logits[i]);
  575. }
  576. buf << " ]";
  577. return buf.str();
  578. }
  579. #ifdef LOG_DISABLE_LOGS
  580. #undef LOG
  581. #define LOG(...) // dummy stub
  582. #undef LOGLN
  583. #define LOGLN(...) // dummy stub
  584. #undef LOG_TEE
  585. #define LOG_TEE(...) fprintf(stderr, __VA_ARGS__) // convert to normal fprintf
  586. #undef LOG_TEELN
  587. #define LOG_TEELN(...) fprintf(stderr, __VA_ARGS__) // convert to normal fprintf
  588. #undef LOG_DISABLE
  589. #define LOG_DISABLE() // dummy stub
  590. #undef LOG_ENABLE
  591. #define LOG_ENABLE() // dummy stub
  592. #undef LOG_ENABLE
  593. #define LOG_ENABLE() // dummy stub
  594. #undef LOG_SET_TARGET
  595. #define LOG_SET_TARGET(...) // dummy stub
  596. #undef LOG_DUMP_CMDLINE
  597. #define LOG_DUMP_CMDLINE(...) // dummy stub
  598. #endif // LOG_DISABLE_LOGS