Mercurial > ~mikael > mcabber > hg
annotate mcabber/src/utils.c @ 374:bd5638c21834
Improve logging system (traces)
There are now two trace logging levels:
* tracelog_level = 1:
Most messages from the log window are written to disk (LPRINT_LOG)
* tracelog_level =2:
LPRINT_LOG & LPRINT_DEBUG messages are written to disk
The trace file name is set with the "tracelog_file" option.
author | Mikael Berthe <mikael@lilotux.net> |
---|---|
date | Mon, 25 Jul 2005 21:46:35 +0100 |
parents | 33b8e801ffa6 |
children | 2e6c7b1440d1 |
rev | line source |
---|---|
241 | 1 /* |
2 * utils.c -- Various utility functions | |
3 * | |
4 * Copyright (C) 2005 Mikael Berthe <bmikael@lists.lilotux.net> | |
5 * ut_* functions are derived from Cabber debug/log code. | |
6 * from_iso8601() comes from the Gaim project. | |
7 * | |
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; either version 2 of the License, or (at | |
11 * your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program; if not, write to the Free Software | |
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 | |
21 * USA | |
22 */ | |
23 | |
24 | 24 #include <stdio.h> |
25 #include <stdlib.h> | |
77 | 26 #include <string.h> |
24 | 27 #include <stdarg.h> |
28 #include <time.h> | |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
29 #include <unistd.h> |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
30 #include <sys/types.h> |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
31 #include <sys/stat.h> |
24 | 32 |
241 | 33 #include <config.h> |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
34 #include "screen.h" |
241 | 35 |
76 | 36 static int DebugEnabled; |
77 | 37 static char *FName; |
24 | 38 |
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
241
diff
changeset
|
39 void ut_InitDebug(unsigned int level, const char *filename) |
24 | 40 { |
77 | 41 FILE *fp; |
42 | |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
43 if (level < 1) { |
138 | 44 DebugEnabled = 0; |
45 FName = NULL; | |
46 return; | |
47 } | |
48 | |
49 if (filename) | |
50 FName = strdup(filename); | |
77 | 51 else { |
138 | 52 FName = getenv("HOME"); |
53 if (!FName) | |
54 FName = "/tmp/mcabberlog"; | |
55 else { | |
56 char *tmpname = malloc(strlen(FName) + 12); | |
57 strcpy(tmpname, FName); | |
58 strcat(tmpname, "/mcabberlog"); | |
59 FName = tmpname; | |
60 } | |
77 | 61 } |
24 | 62 |
63 DebugEnabled = level; | |
76 | 64 |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
65 fp = fopen(FName, "a"); |
76 | 66 if (!fp) return; |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
67 fprintf(fp, "New trace log started.\n" |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
68 "----------------------\n"); |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
69 fchmod(fileno(fp), S_IRUSR|S_IWUSR); |
24 | 70 fclose(fp); |
71 } | |
72 | |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
73 void ut_WriteLog(unsigned int flag, const char *data) |
24 | 74 { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
75 if (!DebugEnabled || !FName) return; |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
76 |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
77 if (((DebugEnabled == 2) && (flag & (LPRINT_LOG|LPRINT_DEBUG))) || |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
78 ((DebugEnabled == 1) && (flag & LPRINT_LOG))) { |
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
79 FILE *fp = fopen(FName, "a+"); |
76 | 80 if (!fp) return; |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
81 fputs(data, fp); |
24 | 82 fclose(fp); |
83 } | |
84 } | |
85 | |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
86 // checkset_perm(name, setmode) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
87 // Check the permissions of the "name" file/dir |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
88 // If setmode is true, correct the permissions if they are wrong |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
89 // Return values: -1 == bad file/dir, 0 == success, 1 == cannot correct |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
90 int checkset_perm(const char *name, unsigned int setmode) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
91 { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
92 int fd; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
93 struct stat buf; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
94 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
95 fd = lstat(name, &buf); |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
96 if (fd == -1) return -1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
97 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
98 if (buf.st_uid != geteuid()) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
99 scr_LogPrint(LPRINT_LOGNORM, "Wrong file owner [%s]", name); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
100 return 1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
101 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
102 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
103 if (buf.st_mode & (S_IRGRP | S_IWGRP | S_IXGRP) || |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
104 buf.st_mode & (S_IROTH | S_IWOTH | S_IXOTH)) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
105 if (setmode) { |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
106 mode_t newmode = 0; |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
107 scr_LogPrint(LPRINT_LOGNORM, "Bad permissions [%s]", name); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
108 if (S_ISDIR(buf.st_mode)) |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
109 newmode |= S_IXUSR; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
110 newmode |= S_IRUSR | S_IWUSR; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
111 if (chmod(name, newmode)) { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
112 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Failed to correct permissions!"); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
113 return 1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
114 } |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
115 scr_LogPrint(LPRINT_LOGNORM, "Permissions have been corrected"); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
116 } else { |
374
bd5638c21834
Improve logging system (traces)
Mikael Berthe <mikael@lilotux.net>
parents:
364
diff
changeset
|
117 scr_LogPrint(LPRINT_LOGNORM, "WARNING: Bad permissions [%s]", name); |
362
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
118 return 1; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
119 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
120 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
121 |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
122 return 0; |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
123 } |
d8f147d6e872
Check directory and config file permissions
Mikael Berthe <mikael@lilotux.net>
parents:
281
diff
changeset
|
124 |
241 | 125 // to_iso8601(dststr, timestamp) |
126 // Convert timestamp to iso8601 format, and store it in dststr. | |
127 // NOTE: dststr should be at last 19 chars long. | |
128 // Return should be 0 | |
129 int to_iso8601(char *dststr, time_t timestamp) | |
130 { | |
131 struct tm *tm_time; | |
132 int ret; | |
133 | |
134 tm_time = gmtime(×tamp); | |
135 | |
136 ret = snprintf(dststr, 19, "%.4d%02d%02dT%02d:%02d:%02dZ", | |
137 1900+tm_time->tm_year, tm_time->tm_mon+1, tm_time->tm_mday, | |
138 tm_time->tm_hour, tm_time->tm_min, tm_time->tm_sec); | |
139 | |
140 return ((ret == -1) ? -1 : 0); | |
141 } | |
142 | |
143 // from_iso8601(timestamp, utc) | |
144 // This function comes from the Gaim project, gaim_str_to_time(). | |
145 // (Actually date may not be pure iso-8601) | |
146 // Thanks, guys! | |
147 time_t from_iso8601(const char *timestamp, int utc) | |
148 { | |
149 struct tm t; | |
150 time_t retval = 0; | |
151 char buf[32]; | |
152 char *c; | |
153 int tzoff = 0; | |
154 | |
155 time(&retval); | |
156 localtime_r(&retval, &t); | |
157 | |
158 snprintf(buf, sizeof(buf), "%s", timestamp); | |
159 c = buf; | |
160 | |
161 /* 4 digit year */ | |
162 if (!sscanf(c, "%04d", &t.tm_year)) return 0; | |
163 c+=4; | |
164 if (*c == '-') | |
165 c++; | |
166 | |
167 t.tm_year -= 1900; | |
168 | |
169 /* 2 digit month */ | |
170 if (!sscanf(c, "%02d", &t.tm_mon)) return 0; | |
171 c+=2; | |
172 if (*c == '-') | |
173 c++; | |
174 | |
175 t.tm_mon -= 1; | |
176 | |
177 /* 2 digit day */ | |
178 if (!sscanf(c, "%02d", &t.tm_mday)) return 0; | |
179 c+=2; | |
180 if (*c == 'T' || *c == '.') { /* we have more than a date, keep going */ | |
181 c++; /* skip the "T" */ | |
182 | |
183 /* 2 digit hour */ | |
184 if (sscanf(c, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 || | |
185 sscanf(c, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) { | |
186 int tzhrs, tzmins; | |
187 c+=8; | |
188 if (*c == '.') /* dealing with precision we don't care about */ | |
189 c += 4; | |
190 | |
191 if ((*c == '+' || *c == '-') && | |
192 sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) { | |
193 tzoff = tzhrs*60*60 + tzmins*60; | |
194 if (*c == '+') | |
195 tzoff *= -1; | |
196 } | |
197 | |
198 if (tzoff || utc) { | |
199 | |
200 //#ifdef HAVE_TM_GMTOFF | |
201 tzoff += t.tm_gmtoff; | |
202 //#else | |
203 //# ifdef HAVE_TIMEZONE | |
204 // tzset(); /* making sure */ | |
205 // tzoff -= timezone; | |
206 //# endif | |
207 //#endif | |
208 } | |
209 } | |
210 } | |
211 | |
212 t.tm_isdst = -1; | |
213 | |
214 retval = mktime(&t); | |
215 | |
216 retval += tzoff; | |
217 | |
218 return retval; | |
219 } |