trace.c 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * GIT - The information manager from hell
 *
 * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
 * Copyright (C) 2002-2004 Oswald Buddenhagen <ossi@users.sf.net>
 * Copyright (C) 2004 Theodore Y. Ts'o <tytso@mit.edu>
 * Copyright (C) 2006 Mike McCormack
 * Copyright (C) 2006 Christian Couder
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  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
 */

#include "cache.h"
#include "quote.h"

28 29
/* Get a trace file descriptor from "key" env variable. */
static int get_trace_fd(const char *key, int *need_close)
30
{
31
	char *trace = getenv(key);
32

33 34
	if (!trace || !strcmp(trace, "") ||
	    !strcmp(trace, "0") || !strcasecmp(trace, "false"))
35 36 37 38 39
		return 0;
	if (!strcmp(trace, "1") || !strcasecmp(trace, "true"))
		return STDERR_FILENO;
	if (strlen(trace) == 1 && isdigit(*trace))
		return atoi(trace);
40
	if (is_absolute_path(trace)) {
41 42 43 44 45 46 47 48 49 50 51 52
		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",
				trace, strerror(errno));
			return STDERR_FILENO;
		}
		*need_close = 1;
		return fd;
	}

53
	fprintf(stderr, "What does '%s' for %s mean?\n", trace, key);
54
	fprintf(stderr, "If you want to trace into a file, "
55 56
		"then please set %s to an absolute pathname "
		"(starting with /).\n", key);
57 58 59 60 61 62 63 64
	fprintf(stderr, "Defaulting to tracing on stderr...\n");

	return STDERR_FILENO;
}

static const char err_msg[] = "Could not trace into fd given by "
	"GIT_TRACE environment variable";

65
static void trace_vprintf(const char *key, const char *format, va_list ap)
66
{
J
Jeff King 已提交
67
	struct strbuf buf = STRBUF_INIT;
68

J
Jeff King 已提交
69
	if (!trace_want(key))
70 71
		return;

72
	set_try_to_free_routine(NULL);	/* is never reset */
73
	strbuf_vaddf(&buf, format, ap);
J
Jeff King 已提交
74
	trace_strbuf(key, &buf);
75
	strbuf_release(&buf);
76 77
}

78
void trace_printf_key(const char *key, const char *format, ...)
79 80
{
	va_list ap;
81 82
	va_start(ap, format);
	trace_vprintf(key, format, ap);
83 84 85
	va_end(ap);
}

86
void trace_printf(const char *format, ...)
J
Jeff King 已提交
87 88
{
	va_list ap;
89 90
	va_start(ap, format);
	trace_vprintf("GIT_TRACE", format, ap);
J
Jeff King 已提交
91 92 93
	va_end(ap);
}

J
Jeff King 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107
void trace_strbuf(const char *key, const struct strbuf *buf)
{
	int fd, need_close = 0;

	fd = get_trace_fd(key, &need_close);
	if (!fd)
		return;

	write_or_whine_pipe(fd, buf->buf, buf->len, err_msg);

	if (need_close)
		close(fd);
}

108
void trace_argv_printf(const char **argv, const char *format, ...)
109
{
J
Jeff King 已提交
110
	struct strbuf buf = STRBUF_INIT;
111
	va_list ap;
J
Jeff King 已提交
112
	int fd, need_close = 0;
113

114
	fd = get_trace_fd("GIT_TRACE", &need_close);
115 116 117
	if (!fd)
		return;

118
	set_try_to_free_routine(NULL);	/* is never reset */
119 120
	va_start(ap, format);
	strbuf_vaddf(&buf, format, ap);
121
	va_end(ap);
122

123
	sq_quote_argv(&buf, argv, 0);
124 125 126
	strbuf_addch(&buf, '\n');
	write_or_whine_pipe(fd, buf.buf, buf.len, err_msg);
	strbuf_release(&buf);
127 128 129 130

	if (need_close)
		close(fd);
}
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157

static const char *quote_crnl(const char *path)
{
	static char new_path[PATH_MAX];
	const char *p2 = path;
	char *p1 = new_path;

	if (!path)
		return NULL;

	while (*p2) {
		switch (*p2) {
		case '\\': *p1++ = '\\'; *p1++ = '\\'; break;
		case '\n': *p1++ = '\\'; *p1++ = 'n'; break;
		case '\r': *p1++ = '\\'; *p1++ = 'r'; break;
		default:
			*p1++ = *p2;
		}
		p2++;
	}
	*p1 = '\0';
	return new_path;
}

/* FIXME: move prefix to startup_info struct and get rid of this arg */
void trace_repo_setup(const char *prefix)
{
158
	static const char *key = "GIT_TRACE_SETUP";
159
	const char *git_work_tree;
160 161
	char cwd[PATH_MAX];

162
	if (!trace_want(key))
163 164 165 166 167
		return;

	if (!getcwd(cwd, PATH_MAX))
		die("Unable to get current working directory");

168 169 170 171 172 173
	if (!(git_work_tree = get_git_work_tree()))
		git_work_tree = "(null)";

	if (!prefix)
		prefix = "(null)";

174 175 176 177
	trace_printf_key(key, "setup: git_dir: %s\n", quote_crnl(get_git_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));
178
}
179 180 181 182 183 184 185 186 187 188

int trace_want(const char *key)
{
	const char *trace = getenv(key);

	if (!trace || !strcmp(trace, "") ||
	    !strcmp(trace, "0") || !strcasecmp(trace, "false"))
		return 0;
	return 1;
}