]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - src/debug.h
update copyright year
[coloredstderr/coloredstderr.git] / src / debug.h
1 /*
2  * Debug functions.
3  *
4  * Copyright (C) 2013-2015  Simon Ruderich
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef DEBUG_H
21 #define DEBUG_H 1
22
23 static void debug_write(int fd, int first_call, char const *format, va_list ap) {
24     char buffer[1024];
25
26     int written = vsnprintf(buffer, sizeof(buffer), format, ap);
27     if (written < 0) {
28         return; /* shouldn't happen */
29     /* Overflow. */
30     } else if ((size_t)written >= sizeof(buffer)) {
31         written = sizeof(buffer) - 1;
32     }
33
34     /* Make sure these functions are loaded. */
35     DLSYM_FUNCTION(real_write, "write");
36     DLSYM_FUNCTION(real_close, "close");
37
38     if (first_call) {
39         char const nl = '\n';
40         real_write(fd, &nl, 1);
41     }
42     real_write(fd, buffer, (size_t)written);
43     real_close(fd);
44 }
45
46 #ifdef DEBUG
47 static void debug(char const *format, ...) {
48     va_list ap;
49
50     int saved_errno = errno;
51
52     /* If the file doesn't exist, do nothing. Prevents writing log files in
53      * unexpected places. The user must create the file manually. */
54     int fd = open(DEBUG_FILE, O_WRONLY | O_APPEND);
55     if (fd == -1) {
56         errno = saved_errno;
57         return;
58     }
59
60     static int call_count = 0;
61     call_count++;
62
63     va_start(ap, format);
64     debug_write(fd, call_count == 1, format, ap);
65     va_end(ap);
66
67     errno = saved_errno;
68 }
69 #endif
70
71 static void warning(char const *format, ...) {
72     va_list ap;
73
74     int saved_errno = errno;
75
76     char const *home = getenv("HOME");
77     if (!home) {
78         errno = saved_errno;
79         return;
80     }
81
82     char path[strlen(home) + 1 + strlen(WARNING_FILE) + 1];
83     strcpy(path, home);
84     strcat(path, "/");
85     strcat(path, WARNING_FILE);
86
87     /* Create the warning file if it doesn't exist yet. */
88     int fd = open(path, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);
89     if (fd == -1) {
90         errno = saved_errno;
91         return;
92     }
93
94     static int call_count = 0;
95     call_count++;
96
97     va_start(ap, format);
98     debug_write(fd, call_count == 1, format, ap);
99     va_end(ap);
100
101     errno = saved_errno;
102 }
103
104 #endif