]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - src/debug.h
Initial commit.
[coloredstderr/coloredstderr.git] / src / debug.h
1 /*
2  * Debug functions.
3  *
4  * Copyright (C) 2013  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(const char *format, ...) {
24     char buffer[1024];
25
26     /* If the file doesn't exist, do nothing. Prevents writing log files in
27      * unexpected places. The user must create the file manually. */
28     int fd = open(DEBUG_FILE, O_WRONLY | O_APPEND);
29     if (fd == -1)
30         return;
31
32     va_list ap;
33     va_start(ap, format);
34     int written = vsnprintf(buffer, sizeof(buffer), format, ap);
35     va_end(ap);
36
37     /* Make sure these functions are loaded. */
38     DLSYM_FUNCTION(real_write, "write");
39     DLSYM_FUNCTION(real_close, "close");
40
41     static int first_call = 0;
42     if (!first_call++) {
43         char nl = '\n';
44         real_write(fd, &nl, 1);
45     }
46     real_write(fd, buffer, (size_t)written);
47     real_close(fd);
48 }
49
50 #endif