THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

326 lines
8.3KB

  1. #include "cache.h"
  2. #include "config.h"
  3. #include "run-command.h"
  4. #include "quote.h"
  5. #include "version.h"
  6. #include "trace2/tr2_dst.h"
  7. #include "trace2/tr2_sysenv.h"
  8. #include "trace2/tr2_tbuf.h"
  9. #include "trace2/tr2_tgt.h"
  10. #include "trace2/tr2_tls.h"
  11. static struct tr2_dst tr2dst_normal = { TR2_SYSENV_NORMAL, 0, 0, 0, 0 };
  12. /*
  13. * Use the TR2_SYSENV_NORMAL_BRIEF setting to omit the "<time> <file>:<line>"
  14. * fields from each line written to the builtin normal target.
  15. *
  16. * Unit tests may want to use this to help with testing.
  17. */
  18. static int tr2env_normal_be_brief;
  19. #define TR2FMT_NORMAL_FL_WIDTH (50)
  20. static int fn_init(void)
  21. {
  22. int want = tr2_dst_trace_want(&tr2dst_normal);
  23. int want_brief;
  24. const char *brief;
  25. if (!want)
  26. return want;
  27. brief = tr2_sysenv_get(TR2_SYSENV_NORMAL_BRIEF);
  28. if (brief && *brief &&
  29. ((want_brief = git_parse_maybe_bool(brief)) != -1))
  30. tr2env_normal_be_brief = want_brief;
  31. return want;
  32. }
  33. static void fn_term(void)
  34. {
  35. tr2_dst_trace_disable(&tr2dst_normal);
  36. }
  37. static void normal_fmt_prepare(const char *file, int line, struct strbuf *buf)
  38. {
  39. strbuf_setlen(buf, 0);
  40. if (!tr2env_normal_be_brief) {
  41. struct tr2_tbuf tb_now;
  42. tr2_tbuf_local_time(&tb_now);
  43. strbuf_addstr(buf, tb_now.buf);
  44. strbuf_addch(buf, ' ');
  45. if (file && *file)
  46. strbuf_addf(buf, "%s:%d ", file, line);
  47. while (buf->len < TR2FMT_NORMAL_FL_WIDTH)
  48. strbuf_addch(buf, ' ');
  49. }
  50. }
  51. static void normal_io_write_fl(const char *file, int line,
  52. const struct strbuf *buf_payload)
  53. {
  54. struct strbuf buf_line = STRBUF_INIT;
  55. normal_fmt_prepare(file, line, &buf_line);
  56. strbuf_addbuf(&buf_line, buf_payload);
  57. tr2_dst_write_line(&tr2dst_normal, &buf_line);
  58. strbuf_release(&buf_line);
  59. }
  60. static void fn_version_fl(const char *file, int line)
  61. {
  62. struct strbuf buf_payload = STRBUF_INIT;
  63. strbuf_addf(&buf_payload, "version %s", git_version_string);
  64. normal_io_write_fl(file, line, &buf_payload);
  65. strbuf_release(&buf_payload);
  66. }
  67. static void fn_start_fl(const char *file, int line,
  68. uint64_t us_elapsed_absolute, const char **argv)
  69. {
  70. struct strbuf buf_payload = STRBUF_INIT;
  71. strbuf_addstr(&buf_payload, "start ");
  72. sq_append_quote_argv_pretty(&buf_payload, argv);
  73. normal_io_write_fl(file, line, &buf_payload);
  74. strbuf_release(&buf_payload);
  75. }
  76. static void fn_exit_fl(const char *file, int line, uint64_t us_elapsed_absolute,
  77. int code)
  78. {
  79. struct strbuf buf_payload = STRBUF_INIT;
  80. double elapsed = (double)us_elapsed_absolute / 1000000.0;
  81. strbuf_addf(&buf_payload, "exit elapsed:%.6f code:%d", elapsed, code);
  82. normal_io_write_fl(file, line, &buf_payload);
  83. strbuf_release(&buf_payload);
  84. }
  85. static void fn_signal(uint64_t us_elapsed_absolute, int signo)
  86. {
  87. struct strbuf buf_payload = STRBUF_INIT;
  88. double elapsed = (double)us_elapsed_absolute / 1000000.0;
  89. strbuf_addf(&buf_payload, "signal elapsed:%.6f code:%d", elapsed,
  90. signo);
  91. normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
  92. strbuf_release(&buf_payload);
  93. }
  94. static void fn_atexit(uint64_t us_elapsed_absolute, int code)
  95. {
  96. struct strbuf buf_payload = STRBUF_INIT;
  97. double elapsed = (double)us_elapsed_absolute / 1000000.0;
  98. strbuf_addf(&buf_payload, "atexit elapsed:%.6f code:%d", elapsed, code);
  99. normal_io_write_fl(__FILE__, __LINE__, &buf_payload);
  100. strbuf_release(&buf_payload);
  101. }
  102. static void maybe_append_string_va(struct strbuf *buf, const char *fmt,
  103. va_list ap)
  104. {
  105. if (fmt && *fmt) {
  106. va_list copy_ap;
  107. va_copy(copy_ap, ap);
  108. strbuf_vaddf(buf, fmt, copy_ap);
  109. va_end(copy_ap);
  110. return;
  111. }
  112. }
  113. static void fn_error_va_fl(const char *file, int line, const char *fmt,
  114. va_list ap)
  115. {
  116. struct strbuf buf_payload = STRBUF_INIT;
  117. strbuf_addstr(&buf_payload, "error");
  118. if (fmt && *fmt) {
  119. strbuf_addch(&buf_payload, ' ');
  120. maybe_append_string_va(&buf_payload, fmt, ap);
  121. }
  122. normal_io_write_fl(file, line, &buf_payload);
  123. strbuf_release(&buf_payload);
  124. }
  125. static void fn_command_path_fl(const char *file, int line, const char *pathname)
  126. {
  127. struct strbuf buf_payload = STRBUF_INIT;
  128. strbuf_addf(&buf_payload, "cmd_path %s", pathname);
  129. normal_io_write_fl(file, line, &buf_payload);
  130. strbuf_release(&buf_payload);
  131. }
  132. static void fn_command_name_fl(const char *file, int line, const char *name,
  133. const char *hierarchy)
  134. {
  135. struct strbuf buf_payload = STRBUF_INIT;
  136. strbuf_addf(&buf_payload, "cmd_name %s", name);
  137. if (hierarchy && *hierarchy)
  138. strbuf_addf(&buf_payload, " (%s)", hierarchy);
  139. normal_io_write_fl(file, line, &buf_payload);
  140. strbuf_release(&buf_payload);
  141. }
  142. static void fn_command_mode_fl(const char *file, int line, const char *mode)
  143. {
  144. struct strbuf buf_payload = STRBUF_INIT;
  145. strbuf_addf(&buf_payload, "cmd_mode %s", mode);
  146. normal_io_write_fl(file, line, &buf_payload);
  147. strbuf_release(&buf_payload);
  148. }
  149. static void fn_alias_fl(const char *file, int line, const char *alias,
  150. const char **argv)
  151. {
  152. struct strbuf buf_payload = STRBUF_INIT;
  153. strbuf_addf(&buf_payload, "alias %s -> ", alias);
  154. sq_append_quote_argv_pretty(&buf_payload, argv);
  155. normal_io_write_fl(file, line, &buf_payload);
  156. strbuf_release(&buf_payload);
  157. }
  158. static void fn_child_start_fl(const char *file, int line,
  159. uint64_t us_elapsed_absolute,
  160. const struct child_process *cmd)
  161. {
  162. struct strbuf buf_payload = STRBUF_INIT;
  163. strbuf_addf(&buf_payload, "child_start[%d]", cmd->trace2_child_id);
  164. if (cmd->dir) {
  165. strbuf_addstr(&buf_payload, " cd ");
  166. sq_quote_buf_pretty(&buf_payload, cmd->dir);
  167. strbuf_addstr(&buf_payload, ";");
  168. }
  169. /*
  170. * TODO if (cmd->env) { Consider dumping changes to environment. }
  171. * See trace_add_env() in run-command.c as used by original trace.c
  172. */
  173. strbuf_addch(&buf_payload, ' ');
  174. if (cmd->git_cmd)
  175. strbuf_addstr(&buf_payload, "git ");
  176. sq_append_quote_argv_pretty(&buf_payload, cmd->argv);
  177. normal_io_write_fl(file, line, &buf_payload);
  178. strbuf_release(&buf_payload);
  179. }
  180. static void fn_child_exit_fl(const char *file, int line,
  181. uint64_t us_elapsed_absolute, int cid, int pid,
  182. int code, uint64_t us_elapsed_child)
  183. {
  184. struct strbuf buf_payload = STRBUF_INIT;
  185. double elapsed = (double)us_elapsed_child / 1000000.0;
  186. strbuf_addf(&buf_payload, "child_exit[%d] pid:%d code:%d elapsed:%.6f",
  187. cid, pid, code, elapsed);
  188. normal_io_write_fl(file, line, &buf_payload);
  189. strbuf_release(&buf_payload);
  190. }
  191. static void fn_exec_fl(const char *file, int line, uint64_t us_elapsed_absolute,
  192. int exec_id, const char *exe, const char **argv)
  193. {
  194. struct strbuf buf_payload = STRBUF_INIT;
  195. strbuf_addf(&buf_payload, "exec[%d] ", exec_id);
  196. if (exe) {
  197. strbuf_addstr(&buf_payload, exe);
  198. strbuf_addch(&buf_payload, ' ');
  199. }
  200. sq_append_quote_argv_pretty(&buf_payload, argv);
  201. normal_io_write_fl(file, line, &buf_payload);
  202. strbuf_release(&buf_payload);
  203. }
  204. static void fn_exec_result_fl(const char *file, int line,
  205. uint64_t us_elapsed_absolute, int exec_id,
  206. int code)
  207. {
  208. struct strbuf buf_payload = STRBUF_INIT;
  209. strbuf_addf(&buf_payload, "exec_result[%d] code:%d", exec_id, code);
  210. if (code > 0)
  211. strbuf_addf(&buf_payload, " err:%s", strerror(code));
  212. normal_io_write_fl(file, line, &buf_payload);
  213. strbuf_release(&buf_payload);
  214. }
  215. static void fn_param_fl(const char *file, int line, const char *param,
  216. const char *value)
  217. {
  218. struct strbuf buf_payload = STRBUF_INIT;
  219. strbuf_addf(&buf_payload, "def_param %s=%s", param, value);
  220. normal_io_write_fl(file, line, &buf_payload);
  221. strbuf_release(&buf_payload);
  222. }
  223. static void fn_repo_fl(const char *file, int line,
  224. const struct repository *repo)
  225. {
  226. struct strbuf buf_payload = STRBUF_INIT;
  227. strbuf_addstr(&buf_payload, "worktree ");
  228. sq_quote_buf_pretty(&buf_payload, repo->worktree);
  229. normal_io_write_fl(file, line, &buf_payload);
  230. strbuf_release(&buf_payload);
  231. }
  232. static void fn_printf_va_fl(const char *file, int line,
  233. uint64_t us_elapsed_absolute, const char *fmt,
  234. va_list ap)
  235. {
  236. struct strbuf buf_payload = STRBUF_INIT;
  237. maybe_append_string_va(&buf_payload, fmt, ap);
  238. normal_io_write_fl(file, line, &buf_payload);
  239. strbuf_release(&buf_payload);
  240. }
  241. struct tr2_tgt tr2_tgt_normal = {
  242. &tr2dst_normal,
  243. fn_init,
  244. fn_term,
  245. fn_version_fl,
  246. fn_start_fl,
  247. fn_exit_fl,
  248. fn_signal,
  249. fn_atexit,
  250. fn_error_va_fl,
  251. fn_command_path_fl,
  252. fn_command_name_fl,
  253. fn_command_mode_fl,
  254. fn_alias_fl,
  255. fn_child_start_fl,
  256. fn_child_exit_fl,
  257. NULL, /* thread_start */
  258. NULL, /* thread_exit */
  259. fn_exec_fl,
  260. fn_exec_result_fl,
  261. fn_param_fl,
  262. fn_repo_fl,
  263. NULL, /* region_enter */
  264. NULL, /* region_leave */
  265. NULL, /* data */
  266. NULL, /* data_json */
  267. fn_printf_va_fl,
  268. };