graphics updates
[voxelands-alt.git] / src / core / log.c
blobe2a7e60740a5f8afeb1c306c18fc1bbc1c37dc54
1 /************************************************************************
2 * log.c
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
20 #include "common.h"
21 #include "path.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
28 static struct {
29 int min_level;
30 int max_level;
31 int system_min_level;
32 int system_max_level;
33 int console_min_level;
34 int console_max_level;
35 char* logfile;
36 char* levels[7];
37 } logdata = {
44 NULL,
45 {"none","error","warn","action","chat","info","debug"}
48 static void level_setter(char* v, int *l, int d)
50 int i;
51 if (v) {
52 for (i=0; i<7; i++) {
53 if (!strcmp(logdata.levels[i],v)) {
54 *l = i;
55 return;
60 *l = d;
63 /* config setters */
64 int log_minlevel_setter(char* v)
66 level_setter(v,&logdata.min_level,1);
67 return 0;
69 int log_maxlevel_setter(char* v)
71 level_setter(v,&logdata.max_level,5);
72 return 0;
74 int log_sminlevel_setter(char* v)
76 level_setter(v,&logdata.system_min_level,4);
77 return 0;
79 int log_smaxlevel_setter(char* v)
81 level_setter(v,&logdata.system_max_level,5);
82 return 0;
84 int log_cminlevel_setter(char* v)
86 level_setter(v,&logdata.console_min_level,4);
87 return 0;
89 int log_cmaxlevel_setter(char* v)
91 level_setter(v,&logdata.console_max_level,5);
92 return 0;
94 int log_file_setter(char* v)
96 if (logdata.logfile)
97 free(logdata.logfile);
98 logdata.logfile = NULL;
100 if (!v)
101 return 0;
103 logdata.logfile = path_get(NULL,v,0,NULL,0);
105 return 0;
108 /* print formatted text to game and system consoles */
109 void vlprintf(uint8_t type, char* fmt,...)
111 char buff[1024];
112 char* b = buff;
113 int s = 1024;
114 va_list ap;
116 if (!fmt)
117 return;
119 /* if it's not logged, don't process it */
120 if (
121 !type
122 || (
123 (type < logdata.min_level || type > logdata.max_level)
124 && (type < logdata.system_min_level || type > logdata.system_max_level)
125 && (type < logdata.console_min_level || type > logdata.console_max_level)
128 return;
130 switch (type) {
131 case 1:
132 strcpy(buff,"ERROR: ");
133 b += 7;
134 s -= 7;
135 break;
136 case 2:
137 strcpy(buff,"WARNING: ");
138 b += 9;
139 s -= 9;
140 break;
141 case 3:
142 strcpy(buff,"ACTION: ");
143 b += 8;
144 s -= 8;
145 break;
146 case 4:
147 strcpy(buff,"CHAT: ");
148 b += 6;
149 s -= 6;
150 break;
151 case 6:
152 strcpy(buff,"DEBUG: ");
153 b += 7;
154 s -= 7;
155 break;
156 default:;
159 va_start(ap, fmt);
160 if (vsnprintf(b, s, fmt, ap) >= s) {
161 va_end(ap);
162 return;
165 va_end(ap);
167 if (type >= logdata.system_min_level && type <= logdata.system_max_level)
168 sys_console_print(buff,1);
169 /* TODO: log to game console */
170 /*if (type >= logdata.console_min_level && type <= logdata.console_max_level)
171 console_put(buff);*/
173 if (type < logdata.min_level || type > logdata.max_level)
174 return;
176 if (logdata.logfile) {
177 /* the only time raw file calls are used, for speed */
178 FILE *f = fopen(logdata.logfile,"a");
179 if (!f)
180 return;
182 fputs(buff,f);
183 fclose(f);