Merge branch 'msys2'
[git-cheetah.git] / common / debug.c
bloba689abf2fef5d5a6120efdd15734de386f57f2bd
1 #include "git-compat-util.h"
2 #include "systeminfo.h"
3 #include "debug.h"
5 static FILE *debug_git_fp = NULL;
7 #ifdef _WIN32
8 static void reset_inherit_flag(FILE *file)
10 HANDLE handle;
12 if(!file)
13 return;
15 handle = (HANDLE)_get_osfhandle(_fileno(file));
16 SetHandleInformation(handle, HANDLE_FLAG_INHERIT, 0);
18 #endif
20 void _debug_git(char * format, ...)
22 if (!debug_git_fp) {
23 #ifdef _WIN32
24 WCHAR path[MAX_PATH];
25 GetTempPathW(MAX_PATH, path);
26 wcsncat(path, L"git_shell_ext_debug.txt", MAX_PATH);
27 debug_git_fp = _wfopen(path, L"a+");
28 reset_inherit_flag(debug_git_fp);
29 #else
30 debug_git_fp = fopen("/tmp/git-cheetah-plugin.log", "a+");
31 #endif
34 /* Check again in case the above debug_git_set_file failed. */
35 if (debug_git_fp)
37 va_list params;
38 char *buffer;
39 int length = 0;
41 va_start(params, format);
42 length = vsnprintf(NULL, 0, format, params);
43 if (length < 0)
44 return;
46 buffer = xmalloc(length + 1);
47 vsnprintf(buffer, length + 1, format, params);
48 va_end(params);
49 fwrite(buffer, sizeof(char), length, debug_git_fp);
50 fputc('\n', debug_git_fp);
51 fflush(debug_git_fp);
52 free(buffer);
56 void _debug_git_mbox(char *format, ...)
58 va_list params;
59 char buffer[1024];
61 va_start(params, format);
62 vsnprintf(buffer, sizeof(buffer), format, params);
63 va_end(params);
64 message_box(buffer);