repository: repo_submodule_init to take a submodule struct
[gitweb.git] / trace.c
diff --git a/trace.c b/trace.c
index 7393926ebcd95404fe16cdd84e40fd4f1bb02f40..fa4a2e7120e405f3cf2d12422802b785f9e37fad 100644 (file)
--- a/trace.c
+++ b/trace.c
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "cache.h"
 #include "quote.h"
 
+struct trace_key trace_default_key = { "GIT_TRACE", 0, 0, 0 };
+struct trace_key trace_perf_key = TRACE_KEY_INIT(PERFORMANCE);
+struct trace_key trace_setup_key = TRACE_KEY_INIT(SETUP);
+
 /* Get a trace file descriptor from "key" env variable. */
 static int get_trace_fd(struct trace_key *key)
 {
-       static struct trace_key trace_default = { "GIT_TRACE" };
        const char *trace;
 
-       /* use default "GIT_TRACE" if NULL */
-       if (!key)
-               key = &trace_default;
-
        /* don't open twice */
        if (key->initialized)
                return key->fd;
@@ -51,22 +49,19 @@ static int get_trace_fd(struct trace_key *key)
        else if (is_absolute_path(trace)) {
                int fd = open(trace, O_WRONLY | O_APPEND | O_CREAT, 0666);
                if (fd == -1) {
-                       fprintf(stderr,
-                               "Could not open '%s' for tracing: %s\n"
-                               "Defaulting to tracing on stderr...\n",
+                       warning("could not open '%s' for tracing: %s",
                                trace, strerror(errno));
-                       key->fd = STDERR_FILENO;
+                       trace_disable(key);
                } else {
                        key->fd = fd;
                        key->need_close = 1;
                }
        } else {
-               fprintf(stderr, "What does '%s' for %s mean?\n"
-                       "If you want to trace into a file, then please set "
-                       "%s to an absolute pathname (starting with /).\n"
-                       "Defaulting to tracing on stderr...\n",
-                       trace, key->key, key->key);
-               key->fd = STDERR_FILENO;
+               warning("unknown trace value for '%s': %s\n"
+                       "         If you want to trace into a file, then please set %s\n"
+                       "         to an absolute pathname (starting with /)",
+                       key->key, trace, key->key);
+               trace_disable(key);
        }
 
        key->initialized = 1;
@@ -82,9 +77,6 @@ void trace_disable(struct trace_key *key)
        key->need_close = 0;
 }
 
-static const char err_msg[] = "Could not trace into fd given by "
-       "GIT_TRACE environment variable";
-
 static int prepare_trace_line(const char *file, int line,
                              struct trace_key *key, struct strbuf *buf)
 {
@@ -120,19 +112,26 @@ static int prepare_trace_line(const char *file, int line,
        return 1;
 }
 
+static void trace_write(struct trace_key *key, const void *buf, unsigned len)
+{
+       if (write_in_full(get_trace_fd(key), buf, len) < 0) {
+               warning("unable to write trace for %s: %s",
+                       key->key, strerror(errno));
+               trace_disable(key);
+       }
+}
+
 void trace_verbatim(struct trace_key *key, const void *buf, unsigned len)
 {
        if (!trace_want(key))
                return;
-       write_or_whine_pipe(get_trace_fd(key), buf, len, err_msg);
+       trace_write(key, buf, len);
 }
 
 static void print_trace_line(struct trace_key *key, struct strbuf *buf)
 {
        strbuf_complete_line(buf);
-
-       write_or_whine_pipe(get_trace_fd(key), buf->buf, buf->len, err_msg);
-       strbuf_release(buf);
+       trace_write(key, buf->buf, buf->len);
 }
 
 static void trace_vprintf_fl(const char *file, int line, struct trace_key *key,
@@ -145,6 +144,7 @@ static void trace_vprintf_fl(const char *file, int line, struct trace_key *key,
 
        strbuf_vaddf(&buf, format, ap);
        print_trace_line(key, &buf);
+       strbuf_release(&buf);
 }
 
 static void trace_argv_vprintf_fl(const char *file, int line,
@@ -153,13 +153,14 @@ static void trace_argv_vprintf_fl(const char *file, int line,
 {
        struct strbuf buf = STRBUF_INIT;
 
-       if (!prepare_trace_line(file, line, NULL, &buf))
+       if (!prepare_trace_line(file, line, &trace_default_key, &buf))
                return;
 
        strbuf_vaddf(&buf, format, ap);
 
-       sq_quote_argv(&buf, argv, 0);
-       print_trace_line(NULL, &buf);
+       sq_quote_argv_pretty(&buf, argv);
+       print_trace_line(&trace_default_key, &buf);
+       strbuf_release(&buf);
 }
 
 void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
@@ -172,14 +173,33 @@ void trace_strbuf_fl(const char *file, int line, struct trace_key *key,
 
        strbuf_addbuf(&buf, data);
        print_trace_line(key, &buf);
+       strbuf_release(&buf);
 }
 
-static struct trace_key trace_perf_key = TRACE_KEY_INIT(PERFORMANCE);
+static uint64_t perf_start_times[10];
+static int perf_indent;
+
+uint64_t trace_performance_enter(void)
+{
+       uint64_t now;
+
+       if (!trace_want(&trace_perf_key))
+               return 0;
+
+       now = getnanotime();
+       perf_start_times[perf_indent] = now;
+       if (perf_indent + 1 < ARRAY_SIZE(perf_start_times))
+               perf_indent++;
+       else
+               BUG("Too deep indentation");
+       return now;
+}
 
 static void trace_performance_vprintf_fl(const char *file, int line,
                                         uint64_t nanos, const char *format,
                                         va_list ap)
 {
+       static const char space[] = "          ";
        struct strbuf buf = STRBUF_INIT;
 
        if (!prepare_trace_line(file, line, &trace_perf_key, &buf))
@@ -188,11 +208,15 @@ static void trace_performance_vprintf_fl(const char *file, int line,
        strbuf_addf(&buf, "performance: %.9f s", (double) nanos / 1000000000);
 
        if (format && *format) {
-               strbuf_addstr(&buf, ": ");
+               if (perf_indent >= strlen(space))
+                       BUG("Too deep indentation");
+
+               strbuf_addf(&buf, ":%.*s ", perf_indent, space);
                strbuf_vaddf(&buf, format, ap);
        }
 
        print_trace_line(&trace_perf_key, &buf);
+       strbuf_release(&buf);
 }
 
 #ifndef HAVE_VARIADIC_MACROS
@@ -201,7 +225,7 @@ void trace_printf(const char *format, ...)
 {
        va_list ap;
        va_start(ap, format);
-       trace_vprintf_fl(NULL, 0, NULL, format, ap);
+       trace_vprintf_fl(NULL, 0, &trace_default_key, format, ap);
        va_end(ap);
 }
 
@@ -243,6 +267,24 @@ void trace_performance_since(uint64_t start, const char *format, ...)
        va_end(ap);
 }
 
+void trace_performance_leave(const char *format, ...)
+{
+       va_list ap;
+       uint64_t since;
+
+       if (perf_indent)
+               perf_indent--;
+
+       if (!format) /* Allow callers to leave without tracing anything */
+               return;
+
+       since = perf_start_times[perf_indent];
+       va_start(ap, format);
+       trace_performance_vprintf_fl(NULL, 0, getnanotime() - since,
+                                    format, ap);
+       va_end(ap);
+}
+
 #else
 
 void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
@@ -272,40 +314,56 @@ void trace_performance_fl(const char *file, int line, uint64_t nanos,
        va_end(ap);
 }
 
+void trace_performance_leave_fl(const char *file, int line,
+                               uint64_t nanos, const char *format, ...)
+{
+       va_list ap;
+       uint64_t since;
+
+       if (perf_indent)
+               perf_indent--;
+
+       if (!format) /* Allow callers to leave without tracing anything */
+               return;
+
+       since = perf_start_times[perf_indent];
+       va_start(ap, format);
+       trace_performance_vprintf_fl(file, line, nanos - since, format, ap);
+       va_end(ap);
+}
+
 #endif /* HAVE_VARIADIC_MACROS */
 
 
 static const char *quote_crnl(const char *path)
 {
-       static char new_path[PATH_MAX];
-       const char *p2 = path;
-       char *p1 = new_path;
+       static struct strbuf new_path = STRBUF_INIT;
 
        if (!path)
                return NULL;
 
-       while (*p2) {
-               switch (*p2) {
-               case '\\': *p1++ = '\\'; *p1++ = '\\'; break;
-               case '\n': *p1++ = '\\'; *p1++ = 'n'; break;
-               case '\r': *p1++ = '\\'; *p1++ = 'r'; break;
+       strbuf_reset(&new_path);
+
+       while (*path) {
+               switch (*path) {
+               case '\\': strbuf_addstr(&new_path, "\\\\"); break;
+               case '\n': strbuf_addstr(&new_path, "\\n"); break;
+               case '\r': strbuf_addstr(&new_path, "\\r"); break;
                default:
-                       *p1++ = *p2;
+                       strbuf_addch(&new_path, *path);
                }
-               p2++;
+               path++;
        }
-       *p1 = '\0';
-       return new_path;
+       return new_path.buf;
 }
 
 /* FIXME: move prefix to startup_info struct and get rid of this arg */
 void trace_repo_setup(const char *prefix)
 {
-       static struct trace_key key = TRACE_KEY_INIT(SETUP);
        const char *git_work_tree;
        char *cwd;
 
-       if (!trace_want(&key))
+       if (!trace_want(&trace_setup_key))
                return;
 
        cwd = xgetcwd();
@@ -316,11 +374,11 @@ void trace_repo_setup(const char *prefix)
        if (!prefix)
                prefix = "(null)";
 
-       trace_printf_key(&key, "setup: git_dir: %s\n", quote_crnl(get_git_dir()));
-       trace_printf_key(&key, "setup: git_common_dir: %s\n", quote_crnl(get_git_common_dir()));
-       trace_printf_key(&key, "setup: worktree: %s\n", quote_crnl(git_work_tree));
-       trace_printf_key(&key, "setup: cwd: %s\n", quote_crnl(cwd));
-       trace_printf_key(&key, "setup: prefix: %s\n", quote_crnl(prefix));
+       trace_printf_key(&trace_setup_key, "setup: git_dir: %s\n", quote_crnl(get_git_dir()));
+       trace_printf_key(&trace_setup_key, "setup: git_common_dir: %s\n", quote_crnl(get_git_common_dir()));
+       trace_printf_key(&trace_setup_key, "setup: worktree: %s\n", quote_crnl(git_work_tree));
+       trace_printf_key(&trace_setup_key, "setup: cwd: %s\n", quote_crnl(cwd));
+       trace_printf_key(&trace_setup_key, "setup: prefix: %s\n", quote_crnl(prefix));
 
        free(cwd);
 }
@@ -412,13 +470,11 @@ uint64_t getnanotime(void)
        }
 }
 
-static uint64_t command_start_time;
 static struct strbuf command_line = STRBUF_INIT;
 
 static void print_command_performance_atexit(void)
 {
-       trace_performance_since(command_start_time, "git command:%s",
-                               command_line.buf);
+       trace_performance_leave("git command:%s", command_line.buf);
 }
 
 void trace_command_performance(const char **argv)
@@ -426,10 +482,10 @@ void trace_command_performance(const char **argv)
        if (!trace_want(&trace_perf_key))
                return;
 
-       if (!command_start_time)
+       if (!command_line.len)
                atexit(print_command_performance_atexit);
 
        strbuf_reset(&command_line);
-       sq_quote_argv(&command_line, argv, 0);
-       command_start_time = getnanotime();
+       sq_quote_argv_pretty(&command_line, argv);
+       trace_performance_enter();
 }