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