put example files to dedicated dir
[monster.git] / log.c
blobd5e7f0dd8e4be1a7b64fa321c88a4acd7b6639d8
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
4 * This file is part of monster
6 * Copyright (C) 2006,2007 Nedko Arnaudov <nedko@arnaudov.name>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 *****************************************************************************/
23 #include <stdio.h>
24 #include <stdarg.h>
25 #include <string.h>
26 #include <errno.h>
28 #include "log.h"
30 #define FORMATTING_BUFFER_SIZE 4096
32 void logwrite_file(const char * message)
34 FILE * f;
36 f = fopen("/var/log/monster.log", "a");
37 if (f == NULL)
39 /* shit happens */
40 /* we need cr because we use curses library */
41 fprintf(stderr, "\rfailed to open/create /var/log/monster.log. error %d (%s)\r\n", errno, strerror(errno));
42 return;
45 fwrite(message, strlen(message), 1, f);
46 fwrite("\n", 1, 1, f);
48 fclose(f);
51 void logwrite_stderr(const char * message)
53 fwrite("\r", 1, 1, stderr);
54 fwrite(message, strlen(message), 1, stderr);
55 fwrite("\r\n", 2, 1, stderr); /* we need cr because we use curses library */
58 void logwrite_va(int lvl, const char * format, va_list arglist)
60 char buffer[FORMATTING_BUFFER_SIZE];
62 vsnprintf(buffer, FORMATTING_BUFFER_SIZE-1, format, arglist);
64 if (lvl <= 0)
66 logwrite_stderr(buffer);
69 logwrite_file(buffer);
72 void logwrite(int lvl, const char * format, ...)
74 va_list arglist;
76 va_start(arglist, format);
77 logwrite_va(lvl, format, arglist);
78 va_end(arglist);