/tomo/pyhst

To get this branch, use:
bzr branch http://darksoft.org/webbzr/tomo/pyhst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * The PyHST program is Copyright (C) 2002-2011 of the
 * European Synchrotron Radiation Facility (ESRF) and
 * Karlsruhe Institute of Technology (KIT).
 *
 * PyHST is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * hst is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <stdarg.h>

#include <glib.h>

#include "debug.h"

#define DS_MAX_LOG_STRING 1024
#define DS_MAX_FORMAT_CHARS 32

static int ds_log_time = 0;
static int ds_log_thread = 0;
static int ds_log_level = ((G_LOG_LEVEL_DEBUG<<1) - 1)&G_LOG_LEVEL_MASK;

static char printf_integer_keys[] = "diouxX";
static char printf_accepted_flags[] = "#0- +'I*$123456789.hlLqjzt";

//GStaticMutex ds_log_mutex = G_STATIC_MUTEX_INIT;

/*
static void ds_log_handler_dummy(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) {
}
*/

static void ds_log_handler_stderr(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) {
    fprintf(stderr, "%s\n", message);
}

//static void (*ds_log_handler)(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) = ds_log_handler_stderr;


void ds_log_set_handler(GLogFunc handler, gpointer arg) {
    g_log_set_default_handler(handler?handler:g_log_default_handler, arg);
}

void ds_log_set_level(GLogLevelFlags log_level) {
    ds_log_level = ((log_level<<1) - 1)&G_LOG_LEVEL_MASK;
}

void ds_configure_logger(GLogLevelFlags log_level, const char *sink) {
    ds_log_set_level(log_level);
	// Ignoring at the moment
    ds_log_set_handler(ds_log_handler_stderr, NULL);
}

void ds_log_enable_timestamps() {
    ds_log_time = 1;
}

void ds_log_enable_threads() {
    ds_log_thread = 1;
}

int ds_log_check_level(GLogLevelFlags log_level) {
    return (ds_log_level&log_level);
}

/*
void ds_log_set_domain_level(const gchar *log_domain, GLogLevelFlags log_level) {
    ds_log_level = ((log_level<<1) - 1)&G_LOG_LEVEL_MASK;
    //g_log_set_handler(log_domain,  (~ds_log_level&G_LOG_LEVEL_MASK), &ds_log_handler_dummy);
}

void ds_log_set_domain_handler(const gchar *log_domain, GLogFunc handler, gpointer arg) {
    guint id;

    id = g_log_set_handler(log_domain, G_LOG_LEVEL_MASK, handler, arg);

    // assuming current implementation using sequential uid numbers starting from 1, the handler should not be set directly after that
    if (id > 1) g_log_remove_handler(log_domain, id - 1);
}

int ds_configure_domain_logger(GLogLevelFlags log_level, const char *sink) {
    ds_log_set_level(log_domain, log_level);
	// Ignoring at the moment
    ds_log_set_handler(log_domain, ds_log_handler_stderr, NULL);
}
*/

void _ds_gen_message(int msglen, /* the buffer should be one byte longer for null termination */
	    char *msg,
	    const char *file, 
	    const char *func,
	    int line, 
	    const gchar *service,
	    const gchar *module, 
	    void *ptr, 
	    gchar *prefix,
	    int err, 
	    gchar *format, 
	    va_list ap) {
    struct tm t;
    struct tm gm;
    long offset;
    int hoffset, moffset;
    GTimeVal ts;
    const char *errmsg;
    int flag, pos = 0;
    size_t length, prefixlength;
    gchar *prefixptr, *ppos;

    char subformat[DS_MAX_FORMAT_CHARS + 1] = "";


    if (ds_log_time) {
	g_get_current_time(&ts);
	
	if  ((localtime_r(&ts.tv_sec, &t))&&(!gmtime_r(&ts.tv_sec, &gm))) {
	    offset = (t.tm_sec - gm.tm_sec) + 60*((t.tm_min - gm.tm_min) + 60*(t.tm_hour - gm.tm_hour));
	    if ((t.tm_mday != gm.tm_mday)||(t.tm_mon != gm.tm_mon)||(t.tm_year != gm.tm_year)) {
		if (t.tm_year > gm.tm_year) offset += 86400;
		else if (t.tm_year < gm.tm_year) offset -= 86400;
		else if (t.tm_mon > gm.tm_mon) offset += 86400;
		else if (t.tm_mon < gm.tm_mon) offset -= 86400;
		else if (t.tm_mday > gm.tm_mday) offset += 86400;
		else offset -= 86400;
	    }	
    
	    g_snprintf(msg + pos, msglen - pos + 1, 
		"%04u-%02u-%02uT%02u:%02u:%02u.%07lu",
		t.tm_year+1900,t.tm_mon+1,t.tm_mday,
		t.tm_hour,t.tm_min,t.tm_sec,
		ts.tv_usec*10
	    );
	    
	    pos += strlen(msg + pos);

	    if ((offset==0)&&(pos < msglen)) msg[pos++] = 'Z';
	    else {
		hoffset=offset/3600;
		moffset=abs(offset/60-hoffset*60);
		g_snprintf(msg + pos, msglen - pos + 1, "%+03i:%02u", hoffset, moffset);
	        pos += strlen(msg + pos);
	    }
	} else {
	    g_snprintf(msg + pos, msglen - pos + 1, 
		"%04u-%02u-%02uT%02u:%02u:%02u.%07uZ",
		0,0,0,
		0,0,0,
		0
	    );
	    pos += strlen(msg + pos);
	}
	if (pos < msglen) msg[pos++] = ' ';
    }

    flag = 0;
    if (service) {
	if (ds_log_thread) g_snprintf(msg + pos, msglen - pos + 1, "%s(%p)", service, g_thread_self());
	else g_snprintf(msg + pos, msglen - pos + 1, "%s", service);
	pos += strlen(msg + pos);
    }
    
    if (module) {
	if ((flag)&&(pos < msglen)) msg[pos++] = '/';

	if (ptr) g_snprintf(msg + pos, msglen - pos + 1, "%s(%p): ", module, ptr);
	else g_snprintf(msg + pos, msglen - pos + 1, "%s: ", module);

	pos += strlen(msg + pos);
    } else if ((flag)&&(pos < msglen)) msg[pos++] = '/';
    

    if (err < 0) {
#if (defined(_WIN32)||defined(_WIN64))
        err = GetLastError(); 
#else
        err = errno;
#endif /* WIN */
    }

    if (prefix) {
	prefixptr = prefix;
	for (ppos = strchr(prefix, '%');ppos;) {
	    length = strspn(ppos + 1, printf_accepted_flags) + 1;
	    if ((length < DS_MAX_FORMAT_CHARS)&&((ppos[length]=='s')||(strspn(ppos+length, printf_integer_keys)>0))) {
		prefixlength = (ppos - prefixptr);
		if (prefixlength) {
		    if ((msglen - pos) > prefixlength) g_strlcpy(msg + pos, prefixptr, prefixlength + 1);
		    else g_strlcpy(msg + pos, prefixptr, msglen - pos + 1);
		    pos += strlen(msg + pos);
		}

		if (ppos[length]=='s') g_vsnprintf(msg + pos, msglen - pos + 1, format, ap);
		else {
		    strncpy(subformat, ppos, length + 1); subformat[length + 1] = 0;
		    g_snprintf(msg + pos, msglen - pos + 1, subformat, err);
		}
		pos += strlen(msg + pos);
		    
		prefixptr = ppos + length + 1;
		ppos = strchr(prefixptr, '%');
	    } else if (ppos[length]) {
		ppos = strchr(ppos + length + 1, '%');
	    } else {
		break;
	    }
	}
	
	if (*prefixptr) {
	    g_strlcpy(msg + pos, prefixptr, msglen - pos + 1);
	    pos += strlen(msg + pos);
	}
    } else {	
	if (err) {
#if (defined(_WIN32)||defined(_WIN64))
	    errmsg = NULL;
#else
    	    errmsg = g_strerror(err);
#endif /* WIN */

	    if (errmsg) g_snprintf(msg + pos, msglen - pos + 1, "Error %i(%s): ", err, errmsg);
	    else g_snprintf(msg + pos, msglen - pos + 1, "Error %i: ", err);

	    pos += strlen(msg + pos);
	}

	if (format) {
	    g_vsnprintf(msg + pos, msglen - pos + 1, format, ap);

	    pos += strlen(msg + pos);
	}
    }
    
    if (file) {
	if (func) g_snprintf(msg + pos, msglen - pos + 1, " <%s:%s:%u>", file, func, line);
	else g_snprintf(msg + pos, msglen - pos + 1, " <%s:%u>", file, line);
	pos += strlen(msg + pos);
    }
    
//    if (pos < msglen) msg[pos++] = '\n';
    msg[pos] = 0;
}

void _ds_log_message(int condition, 
	    const gchar *log_domain, 
	    GLogLevelFlags log_levels, 
	    const char *file, 
	    const char *func,
	    int line, 
	    const gchar *service,
	    const gchar *module, 
	    void *ptr, 
	    const gchar *prefix,
	    int err, 
	    const gchar *format, 
	    ...) {

    va_list ap;
    char msg[DS_MAX_LOG_STRING + 1] = "";
    
    if (condition) return;
    if ((ds_log_level&log_levels)==0) return;

    va_start(ap, format);
    _ds_gen_message(DS_MAX_LOG_STRING, msg, file, func, line, service, module, ptr, prefix, err, format, ap);
    va_end(ap);

	/* This is a temporal solution due to silent segmentation failures if
	g_error (fatal errors) are executed from multithreaded context. */
    if (log_levels&DS_LOG_FATAL_MESSAGE) {
	printf("Aborting due to the fatal error: %s\n", msg);
    }

    //g_static_mutex_lock(g_log_mutex);
    g_log(log_domain, log_levels&DS_LOG_LEVEL_MASK, "%s", msg);
    //g_static_mutex_unlock(g_log_mutex);
    
    if (log_levels&DS_LOG_CLEAN_MESSAGE) {
	if (prefix) g_free(prefix);
	else if (format) g_free(format);
    }
    

    if (log_levels&DS_LOG_FATAL_MESSAGE) {
	if (file) g_error(" Program stops at %s:%u. Bye\n", file, line);
	else g_error(" Program stops. Bye\n");
	exit(1);
    }
}

int pyhst_configure_logger(int log_level, GLogFunc handler, gpointer arg) {
    if (log_level <= 10) ds_log_set_level(G_LOG_LEVEL_DEBUG);
    else if (log_level <= 20) ds_log_set_level(G_LOG_LEVEL_INFO);
    else if (log_level <= 30) ds_log_set_level(G_LOG_LEVEL_WARNING);
    else if (log_level <= 40) ds_log_set_level(G_LOG_LEVEL_CRITICAL);
    else ds_log_set_level(G_LOG_LEVEL_ERROR);
    ds_log_set_handler(handler, arg);
    
    return 0;
}