Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder | |
38b8221f | 3 | * Copyright (C) 2005, 2007 Richard Kettlewell |
460b9539 | 4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
18 | * USA | |
19 | */ | |
20 | ||
21 | #include <config.h> | |
22 | #include "types.h" | |
23 | ||
24 | #include <unistd.h> | |
25 | #include <string.h> | |
26 | #include <errno.h> | |
27 | ||
28 | #include "syscalls.h" | |
29 | #include "logfd.h" | |
30 | #include "event.h" | |
31 | #include "log.h" | |
32 | ||
460b9539 | 33 | /* called when bytes are available and at eof */ |
34 | static int logfd_readable(ev_source attribute((unused)) *ev, | |
35 | ev_reader *reader, | |
460b9539 | 36 | void *ptr, |
37 | size_t bytes, | |
38 | int eof, | |
39 | void *u) { | |
40 | char *nl; | |
41 | const char *tag = u; | |
42 | int len; | |
43 | ||
44 | while((nl = memchr(ptr, '\n', bytes))) { | |
45 | len = nl - (char *)ptr; | |
46 | ev_reader_consume(reader, len + 1); | |
47 | info("%s: %.*s", tag, len, (char *)ptr); | |
48 | ptr = nl + 1; | |
49 | bytes -= len + 1; | |
50 | } | |
51 | if(eof && bytes) { | |
52 | info("%s: %.*s", tag, (int)bytes, (char *)ptr); | |
53 | ev_reader_consume(reader, bytes); | |
54 | } | |
460b9539 | 55 | return 0; |
56 | } | |
57 | ||
58 | /* called when a read error occurs */ | |
59 | static int logfd_error(ev_source attribute((unused)) *ev, | |
460b9539 | 60 | int errno_value, |
61 | void *u) { | |
62 | const char *tag = u; | |
63 | ||
64 | error(errno_value, "error reading log pipe from %s", tag); | |
460b9539 | 65 | return 0; |
66 | } | |
67 | ||
75d64210 RK |
68 | /** @brief Create file descriptor for a subprocess to log to |
69 | * @param ev Event loop | |
70 | * @param tag Tag for this log | |
71 | * @return File descriptor | |
72 | * | |
73 | * Returns a file descriptor which a subprocess can log to. The normal thing | |
74 | * to do would be to dup2() this fd onto the subprocess's stderr (and to close | |
75 | * it in the parent). | |
76 | * | |
77 | * Any lines written to this fd (i.e. by the subprocess) will be logged via | |
78 | * info(), with @p tag included. | |
79 | */ | |
460b9539 | 80 | int logfd(ev_source *ev, const char *tag) { |
81 | int p[2]; | |
82 | ||
83 | xpipe(p); | |
84 | cloexec(p[0]); | |
85 | nonblock(p[0]); | |
e8c92ba7 RK |
86 | if(!ev_reader_new(ev, p[0], logfd_readable, logfd_error, (void *)tag, |
87 | "logfd")) | |
460b9539 | 88 | fatal(errno, "error calling ev_reader_new"); |
89 | return p[1]; | |
90 | } | |
91 | ||
92 | /* | |
93 | Local Variables: | |
94 | c-basic-offset:2 | |
95 | comment-column:40 | |
96 | fill-column:79 | |
97 | indent-tabs-mode:nil | |
98 | End: | |
99 | */ |