]> jfr.im git - irc/thales.git/blob - src/log_worker.c
4b119aa9627e5617abd67b51a6d2e6148628dc4a
[irc/thales.git] / src / log_worker.c
1 /* Log worker of GNU Thales. Copyright (C)
2 2012 Free Software Foundation, Inc. This file is part of GNU Thales.
3
4 GNU Thales is free software; you can redistribute it and/or modify it under the
5 terms of the GNU General Public License as published by the Free Software
6 Foundation; either version 3 of the License, or (at your option) any later
7 version.
8
9 GNU Make is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
16 #include "log_worker.h"
17 #include <stdlib.h>
18
19 #include "xalloc.h"
20
21 struct log_worker {
22 struct worker worker;
23 FILE *out_stream;
24 };
25
26 static char *
27 log_worker_process_message(const char *msg, struct irc_meta *meta,
28 struct worker *embed, const char *name)
29 {
30 struct log_worker *logger = worker_entry(embed, struct log_worker, worker);
31 FILE *stream = logger->out_stream ? logger->out_stream : stdout;
32
33 fprintf(stream, "[%s]: %s\n", name, msg);
34 return NULL;
35 }
36
37 static void
38 log_worker_free_worker(struct worker *embed)
39 {
40 struct log_worker *logger = worker_entry(embed, struct log_worker, worker);
41
42 if (logger->out_stream)
43 fclose(logger->out_stream);
44 free(logger);
45 }
46
47 struct worker*
48 create_log_worker(const struct envz *env)
49 {
50 struct log_worker *logger;
51 const char *output_filename = envz_get(env->envz, env->envz_len, "output");
52 FILE *out_stream = NULL;
53 if (output_filename && !(out_stream = fopen(output_filename, "a"))) {
54 fprintf(stderr, "log worker: specified file `%s' can not be appended\n",
55 output_filename);
56 return NULL;
57 }
58
59 logger = xmalloc(sizeof *logger);
60 logger->out_stream = out_stream;
61 return &logger->worker;
62 }