1 /*
2 * Copyright (C) 2014 Neverball authors
3 *
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
8 *
9 * This program 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
15 #include <stdio.h>
16 #include <stdlib.h>
17
18 #include "log.h"
19 #include "common.h"
20 #include "version.h"
21 #include "fs.h"
22
23 static char log_header[MAXSTR];
24 static fs_file log_fp;
25
26 /*---------------------------------------------------------------------------*/
27
log_printf(const char * fmt,...)28 void log_printf(const char *fmt, ...)
29 {
30 char *str;
31 int len;
32
33 va_list ap;
34
35 va_start(ap, fmt);
36 len = 1 + vsnprintf(NULL, 0, fmt, ap);
37 va_end(ap);
38
39 if ((str = malloc(len)))
40 {
41 va_start(ap, fmt);
42 vsnprintf(str, len, fmt, ap);
43 va_end(ap);
44
45 fputs(str, stderr);
46
47 if (log_fp)
48 {
49 /* These are printfs to get us CRLF conversion. */
50
51 if (log_header[0])
52 {
53 fs_printf(log_fp, "%s\n", log_header);
54 log_header[0] = 0;
55 }
56
57 fs_printf(log_fp, "%s", str);
58
59 fs_flush(log_fp);
60 }
61
62 free(str);
63 }
64 }
65
66 /*---------------------------------------------------------------------------*/
67
log_init(const char * name,const char * path)68 void log_init(const char *name, const char *path)
69 {
70 if (!log_fp)
71 {
72 if ((log_fp = fs_open(path, "w+")))
73 {
74 /* Printed on first message. */
75
76 sprintf(log_header, "%s - %s %s",
77 date_to_str(time(NULL)),
78 name, VERSION);
79 }
80 else
81 {
82 fprintf(stderr, "Failure to open %s\n", path);
83 }
84 }
85 }
86
log_quit(void)87 void log_quit(void)
88 {
89 if (log_fp)
90 {
91 fs_close(log_fp);
92 log_header[0] = 0;
93 }
94 }
95
96 /*---------------------------------------------------------------------------*/
97