Mercurial > ~mikael > mcabber > hg
comparison mcabber/mcabber/utils.c @ 1674:f02e7076ccec
Use option guards for tracelog
author | Myhailo Danylenko <isbear@ukrpost.net> |
---|---|
date | Mon, 18 Jan 2010 17:49:30 +0200 |
parents | 41c26b7d2890 |
children | e6e89b1d7831 |
comparison
equal
deleted
inserted
replaced
1673:552da310b83e | 1674:f02e7076ccec |
---|---|
25 | 25 |
26 #include <stdio.h> | 26 #include <stdio.h> |
27 #include <stdlib.h> | 27 #include <stdlib.h> |
28 #include <string.h> | 28 #include <string.h> |
29 #include <stdarg.h> | 29 #include <stdarg.h> |
30 #include <errno.h> | |
30 | 31 |
31 #ifdef HAVE_LIBIDN | 32 #ifdef HAVE_LIBIDN |
32 #include <idna.h> | 33 #include <idna.h> |
33 #include <stringprep.h> | 34 #include <stringprep.h> |
34 static char idnprep[1024]; | 35 static char idnprep[1024]; |
48 #include <sys/stat.h> | 49 #include <sys/stat.h> |
49 #include <ctype.h> | 50 #include <ctype.h> |
50 | 51 |
51 #include "utils.h" | 52 #include "utils.h" |
52 #include "logprint.h" | 53 #include "logprint.h" |
54 #include "settings.h" | |
53 | 55 |
54 static int DebugEnabled; | 56 static int DebugEnabled; |
55 static char *FName; | 57 static char *FName; |
56 | 58 |
57 // jidtodisp(jid) | 59 // jidtodisp(jid) |
151 for (i = 0, p = (char*)hex; *p && *(p+1); i++, p += 3) | 153 for (i = 0, p = (char*)hex; *p && *(p+1); i++, p += 3) |
152 fpr[i] = (char) g_ascii_strtoull (p, NULL, 16); | 154 fpr[i] = (char) g_ascii_strtoull (p, NULL, 16); |
153 return TRUE; | 155 return TRUE; |
154 } | 156 } |
155 | 157 |
156 void ut_InitDebug(int level, const char *filename) | 158 static gboolean tracelog_create(void) |
157 { | 159 { |
158 FILE *fp; | 160 FILE *fp; |
159 struct stat buf; | 161 struct stat buf; |
160 int err; | 162 int err; |
161 | 163 |
162 if (level < 1) { | |
163 DebugEnabled = 0; | |
164 FName = NULL; | |
165 return; | |
166 } | |
167 | |
168 if (filename) | |
169 FName = expand_filename(filename); | |
170 else { | |
171 FName = getenv("HOME"); | |
172 if (!FName) | |
173 FName = g_strdup("/tmp/mcabberlog"); | |
174 else { | |
175 FName = g_strdup_printf("%s/mcabberlog", FName); | |
176 } | |
177 } | |
178 | |
179 DebugEnabled = level; | |
180 | |
181 fp = fopen(FName, "a"); | 164 fp = fopen(FName, "a"); |
182 if (!fp) { | 165 if (!fp) { |
183 fprintf(stderr, "ERROR: Cannot open tracelog file\n"); | 166 scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot open tracelog file: %s!", |
184 return; | 167 strerror(errno)); |
168 return FALSE; | |
185 } | 169 } |
186 | 170 |
187 err = fstat(fileno(fp), &buf); | 171 err = fstat(fileno(fp), &buf); |
188 if (err || buf.st_uid != geteuid()) { | 172 if (err || buf.st_uid != geteuid()) { |
189 fclose(fp); | 173 fclose(fp); |
174 if (err) | |
175 scr_LogPrint(LPRINT_NORMAL, "ERROR: cannot stat the tracelog file: %s!", | |
176 strerror(errno)); | |
177 else | |
178 scr_LogPrint(LPRINT_NORMAL, "ERROR: tracelog file does not belong to you!"); | |
179 return FALSE; | |
180 } | |
181 fchmod(fileno(fp), S_IRUSR|S_IWUSR); | |
182 | |
183 fputs("New trace log started.\n----------------------\n", fp); | |
184 fclose(fp); | |
185 | |
186 return TRUE; | |
187 } | |
188 | |
189 static gchar *tracelog_level_guard(const gchar *key, const gchar *new_value) | |
190 { | |
191 int new_level = 0; | |
192 if (new_value) | |
193 new_level = atoi(new_value); | |
194 if (DebugEnabled < 1 && new_level > 0 && FName && !tracelog_create()) | |
190 DebugEnabled = 0; | 195 DebugEnabled = 0; |
191 FName = NULL; | 196 else |
192 if (err) { | 197 DebugEnabled = new_level; |
193 fprintf(stderr, "ERROR: cannot stat the tracelog file!\n"); | 198 return g_strdup(new_value); |
194 } else { | 199 } |
195 fprintf(stderr, "ERROR: tracelog file does not belong to you!\n"); | 200 |
196 } | 201 static gchar *tracelog_file_guard(const gchar *key, const gchar *new_value) |
197 return; | 202 { |
198 } | 203 gchar *new_fname = NULL; |
199 fchmod(fileno(fp), S_IRUSR|S_IWUSR); | 204 |
200 | 205 if (new_value) |
201 fprintf(fp, "New trace log started.\n----------------------\n"); | 206 new_fname = expand_filename(new_value); |
202 fclose(fp); | 207 |
208 if (g_strcmp0(FName, new_fname)) { | |
209 g_free(FName); | |
210 FName = new_fname; | |
211 if (DebugEnabled > 0 && !tracelog_create()) { | |
212 g_free(FName); | |
213 FName = NULL; | |
214 } | |
215 } else | |
216 g_free(new_fname); | |
217 | |
218 return g_strdup(new_value); | |
219 } | |
220 | |
221 // ut_InitDebug() | |
222 // Installs otpion guards before initial config file parsing. | |
223 void ut_InitDebug(void) | |
224 { | |
225 DebugEnabled = 0; | |
226 FName = NULL; | |
227 settings_set_guard("tracelog_level", tracelog_level_guard); | |
228 settings_set_guard("tracelog_file", tracelog_file_guard); | |
203 } | 229 } |
204 | 230 |
205 void ut_WriteLog(unsigned int flag, const char *data) | 231 void ut_WriteLog(unsigned int flag, const char *data) |
206 { | 232 { |
207 if (!DebugEnabled || !FName) return; | 233 if (!DebugEnabled || !FName) return; |
208 | 234 |
209 if (((DebugEnabled >= 2) && (flag & (LPRINT_LOG|LPRINT_DEBUG))) || | 235 if (((DebugEnabled >= 2) && (flag & (LPRINT_LOG|LPRINT_DEBUG))) || |
210 ((DebugEnabled == 1) && (flag & LPRINT_LOG))) { | 236 ((DebugEnabled == 1) && (flag & LPRINT_LOG))) { |
211 FILE *fp = fopen(FName, "a+"); | 237 FILE *fp = fopen(FName, "a+"); |
212 if (!fp) { | 238 if (!fp) { |
213 scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot open tracelog file"); | 239 scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot open tracelog file: %s.", |
240 strerror(errno)); | |
214 return; | 241 return; |
215 } | 242 } |
216 if (fputs(data, fp) == EOF) | 243 if (fputs(data, fp) == EOF) |
217 scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot write to tracelog file"); | 244 scr_LogPrint(LPRINT_NORMAL, "ERROR: Cannot write to tracelog file."); |
218 fclose(fp); | 245 fclose(fp); |
219 } | 246 } |
220 } | 247 } |
221 | 248 |
222 // checkset_perm(name, setmode) | 249 // checkset_perm(name, setmode) |