author | Mikael Berthe <mikael@lilotux.net> |
Wed, 06 Jul 2005 22:18:05 +0100 | |
changeset 281 | f562b9af2de7 |
parent 241 | 8584f919d9b9 |
child 362 | d8f147d6e872 |
child 364 | 33b8e801ffa6 |
permissions | -rw-r--r-- |
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> |
|
29 |
||
241 | 30 |
#include <config.h> |
31 |
||
76 | 32 |
static int DebugEnabled; |
77 | 33 |
static char *FName; |
24 | 34 |
|
281
f562b9af2de7
Add "const" specifier in prototypes
Mikael Berthe <mikael@lilotux.net>
parents:
241
diff
changeset
|
35 |
void ut_InitDebug(unsigned int level, const char *filename) |
24 | 36 |
{ |
77 | 37 |
FILE *fp; |
38 |
||
138 | 39 |
if (!level) { |
40 |
DebugEnabled = 0; |
|
41 |
FName = NULL; |
|
42 |
return; |
|
43 |
} |
|
44 |
||
45 |
if (filename) |
|
46 |
FName = strdup(filename); |
|
77 | 47 |
else { |
138 | 48 |
FName = getenv("HOME"); |
49 |
if (!FName) |
|
50 |
FName = "/tmp/mcabberlog"; |
|
51 |
else { |
|
52 |
char *tmpname = malloc(strlen(FName) + 12); |
|
53 |
strcpy(tmpname, FName); |
|
54 |
strcat(tmpname, "/mcabberlog"); |
|
55 |
FName = tmpname; |
|
56 |
} |
|
77 | 57 |
} |
24 | 58 |
|
59 |
DebugEnabled = level; |
|
76 | 60 |
|
77 | 61 |
fp = fopen(FName, "w"); |
76 | 62 |
if (!fp) return; |
24 | 63 |
fprintf(fp, "Debugging mode started...\n" |
64 |
"-----------------------------------\n"); |
|
65 |
fclose(fp); |
|
66 |
} |
|
67 |
||
68 |
void ut_WriteLog(const char *fmt, ...) |
|
69 |
{ |
|
70 |
FILE *fp = NULL; |
|
71 |
time_t ahora; |
|
72 |
va_list ap; |
|
73 |
char *buffer = NULL; |
|
74 |
||
138 | 75 |
if (DebugEnabled && FName) { |
77 | 76 |
fp = fopen(FName, "a+"); |
76 | 77 |
if (!fp) return; |
24 | 78 |
buffer = (char *) calloc(1, 64); |
79 |
||
80 |
ahora = time(NULL); |
|
81 |
strftime(buffer, 64, "[%H:%M:%S] ", localtime(&ahora)); |
|
82 |
fprintf(fp, "%s", buffer); |
|
83 |
||
84 |
va_start(ap, fmt); |
|
85 |
vfprintf(fp, fmt, ap); |
|
86 |
va_end(ap); |
|
87 |
||
88 |
free(buffer); |
|
89 |
fclose(fp); |
|
90 |
} |
|
91 |
} |
|
92 |
||
241 | 93 |
// to_iso8601(dststr, timestamp) |
94 |
// Convert timestamp to iso8601 format, and store it in dststr. |
|
95 |
// NOTE: dststr should be at last 19 chars long. |
|
96 |
// Return should be 0 |
|
97 |
int to_iso8601(char *dststr, time_t timestamp) |
|
98 |
{ |
|
99 |
struct tm *tm_time; |
|
100 |
int ret; |
|
101 |
||
102 |
tm_time = gmtime(×tamp); |
|
103 |
||
104 |
ret = snprintf(dststr, 19, "%.4d%02d%02dT%02d:%02d:%02dZ", |
|
105 |
1900+tm_time->tm_year, tm_time->tm_mon+1, tm_time->tm_mday, |
|
106 |
tm_time->tm_hour, tm_time->tm_min, tm_time->tm_sec); |
|
107 |
||
108 |
return ((ret == -1) ? -1 : 0); |
|
109 |
} |
|
110 |
||
111 |
// from_iso8601(timestamp, utc) |
|
112 |
// This function comes from the Gaim project, gaim_str_to_time(). |
|
113 |
// (Actually date may not be pure iso-8601) |
|
114 |
// Thanks, guys! |
|
115 |
time_t from_iso8601(const char *timestamp, int utc) |
|
116 |
{ |
|
117 |
struct tm t; |
|
118 |
time_t retval = 0; |
|
119 |
char buf[32]; |
|
120 |
char *c; |
|
121 |
int tzoff = 0; |
|
122 |
||
123 |
time(&retval); |
|
124 |
localtime_r(&retval, &t); |
|
125 |
||
126 |
snprintf(buf, sizeof(buf), "%s", timestamp); |
|
127 |
c = buf; |
|
128 |
||
129 |
/* 4 digit year */ |
|
130 |
if (!sscanf(c, "%04d", &t.tm_year)) return 0; |
|
131 |
c+=4; |
|
132 |
if (*c == '-') |
|
133 |
c++; |
|
134 |
||
135 |
t.tm_year -= 1900; |
|
136 |
||
137 |
/* 2 digit month */ |
|
138 |
if (!sscanf(c, "%02d", &t.tm_mon)) return 0; |
|
139 |
c+=2; |
|
140 |
if (*c == '-') |
|
141 |
c++; |
|
142 |
||
143 |
t.tm_mon -= 1; |
|
144 |
||
145 |
/* 2 digit day */ |
|
146 |
if (!sscanf(c, "%02d", &t.tm_mday)) return 0; |
|
147 |
c+=2; |
|
148 |
if (*c == 'T' || *c == '.') { /* we have more than a date, keep going */ |
|
149 |
c++; /* skip the "T" */ |
|
150 |
||
151 |
/* 2 digit hour */ |
|
152 |
if (sscanf(c, "%02d:%02d:%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3 || |
|
153 |
sscanf(c, "%02d%02d%02d", &t.tm_hour, &t.tm_min, &t.tm_sec) == 3) { |
|
154 |
int tzhrs, tzmins; |
|
155 |
c+=8; |
|
156 |
if (*c == '.') /* dealing with precision we don't care about */ |
|
157 |
c += 4; |
|
158 |
||
159 |
if ((*c == '+' || *c == '-') && |
|
160 |
sscanf(c+1, "%02d:%02d", &tzhrs, &tzmins)) { |
|
161 |
tzoff = tzhrs*60*60 + tzmins*60; |
|
162 |
if (*c == '+') |
|
163 |
tzoff *= -1; |
|
164 |
} |
|
165 |
||
166 |
if (tzoff || utc) { |
|
167 |
||
168 |
//#ifdef HAVE_TM_GMTOFF |
|
169 |
tzoff += t.tm_gmtoff; |
|
170 |
//#else |
|
171 |
//# ifdef HAVE_TIMEZONE |
|
172 |
// tzset(); /* making sure */ |
|
173 |
// tzoff -= timezone; |
|
174 |
//# endif |
|
175 |
//#endif |
|
176 |
} |
|
177 |
} |
|
178 |
} |
|
179 |
||
180 |
t.tm_isdst = -1; |
|
181 |
||
182 |
retval = mktime(&t); |
|
183 |
||
184 |
retval += tzoff; |
|
185 |
||
186 |
return retval; |
|
187 |
} |
|
188 |