Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 876c1044a1
Fetching contributors…

Cannot retrieve contributors at this time

file 305 lines (274 sloc) 6.492 kb
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* lt-messages.c
* Copyright (C) 2006-2012 Akira TAGOH
*
* Authors:
* Akira TAGOH <akira@tagoh.org>
*
* You may distribute under the terms of either the GNU
* Lesser General Public License or the Mozilla Public
* License, as specified in the README file.
*
* Borrowed from hieroglyph:
* http://cgit.freedesktop.org/hieroglyph/tree/hieroglyph/hgmessages.c
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lt-messages.h"

static void _lt_message_default_handler(lt_message_type_t type,
lt_message_flags_t flags,
lt_message_category_t category,
const char *message,
lt_pointer_t user_data);


static lt_message_func_t __lt_message_default_handler = _lt_message_default_handler;
static lt_pointer_t __lt_message_default_handler_data = NULL;
static lt_message_func_t __lt_message_handler[LT_MSG_END];
static lt_pointer_t __lt_message_handler_data[LT_MSG_END];

/*< private >*/
static char *
_lt_message_get_prefix(lt_message_type_t type,
lt_message_category_t category)
{
static const char *type_string[LT_MSG_END + 1] = {
NULL,
"*** ",
"E: ",
"W: ",
"I: ",
"D: ",
NULL
};
static const char *category_string[LT_MSGCAT_END + 1] = {
NULL,
" DEBUG",
" TRACE",
"MODULE",
NULL
};
static const char unknown_type[] = "?: ";
static const char unknown_cat[] = "???";
static const char no_cat[] = "";
const char *ts, *cs;
char *retval = NULL, *catstring = NULL;
size_t tlen = 0, clen = 0, len;

type = LT_MIN (type, LT_MSG_END);
category = LT_MIN (category, LT_MSGCAT_END);
if (type_string[type]) {
ts = type_string[type];
} else {
ts = unknown_type;
}
tlen = strlen(ts);
if (category_string[category]) {
cs = category_string[category];
} else if (category == 0) {
cs = no_cat;
} else {
cs = unknown_cat;
}
clen = strlen(cs);
if (clen > 0) {
catstring = malloc(sizeof (char) * (clen + 6));
snprintf(catstring, clen + 6, "[%s]: ", cs);
clen = strlen(catstring);
}
len = tlen + clen + 1;
retval = malloc(sizeof (char) * len);
if (retval) {
snprintf(retval, len, "%s%s ", ts, catstring ? catstring : "");
}
if (catstring)
free(catstring);

return retval;
}

static void
_lt_message_stacktrace(void)
{
void *traces[1024];
char **strings;
int size, i;

size = backtrace(traces, 1024);
if (size > 0) {
strings = backtrace_symbols(traces, size);
lt_debug(LT_MSGCAT_TRACE, "Stacktrace:");
/*
* XXX:
* 0.. here.
* 1.. _lt_message_default_handler
* 2.. lt_message_vprintf
* 3.. lt_message_printf
* 4.. lt_* macros
*/
for (i = 4; i < size; i++) {
lt_debug(LT_MSGCAT_TRACE, " %d. %s", i - 3, strings[i]);
}
free(strings);
}
}

static void
_lt_message_default_handler(lt_message_type_t type,
lt_message_flags_t flags,
lt_message_category_t category,
const char *message,
lt_pointer_t user_data)
{
char *prefix = NULL;

if (flags == 0 || (flags & LT_MSG_FLAG_NO_PREFIX) == 0)
prefix = _lt_message_get_prefix(type, category);
fprintf(stderr, "%s%s%s", prefix ? prefix : "", message, flags == 0 || (flags & LT_MSG_FLAG_NO_LINEFEED) == 0 ? "\n" : "");
if (lt_message_is_enabled(LT_MSGCAT_TRACE) && category != LT_MSGCAT_TRACE)
_lt_message_stacktrace();
if (lt_message_is_enabled(LT_MSGCAT_DEBUG) &&
type != LT_MSG_DEBUG)
LT_BREAKPOINT();

if (prefix)
free(prefix);
}

/*< public >*/

/**
* lt_message_set_default_handler:
* @func:
* @user_data:
*
* FIXME
*
* Returns:
*/
lt_message_func_t
lt_message_set_default_handler(lt_message_func_t func,
lt_pointer_t user_data)
{
lt_message_func_t retval = __lt_message_default_handler;

__lt_message_default_handler = func;
__lt_message_default_handler_data = user_data;

return retval;
}

/**
* lt_message_set_handler:
* @type:
* @func:
* @user_data:
*
* FIXME
*
* Returns:
*/
lt_message_func_t
lt_message_set_handler(lt_message_type_t type,
lt_message_func_t func,
lt_pointer_t user_data)
{
lt_message_func_t retval;

if (type >= LT_MSG_END) {
fprintf(stderr, "[BUG] invalid message type: %d\n", type);
return NULL;
}

retval = __lt_message_handler[type];
__lt_message_handler[type] = func;
__lt_message_handler_data[type] = user_data;

return retval;
}

/**
* lt_message_is_masked:
* @category:
*
* FIXME
*
* Returns:
*/
lt_bool_t
lt_message_is_enabled(lt_message_category_t category)
{
static lt_bool_t cache = FALSE;
static int mask = 0;
const char *env;

if (!cache) {
env = getenv("LT_DEBUG");
if (env)
mask = atoi(env);
cache = TRUE;
}

return ((1 << (category - 1)) & mask) != 0;
}

/**
* lt_message_printf:
* @type:
* @category:
* @format:
*
* FIXME
*/
void
lt_message_printf(lt_message_type_t type,
lt_message_flags_t flags,
lt_message_category_t category,
const char *format,
...)
{
va_list args;

va_start(args, format);

lt_message_vprintf(type, flags, category, format, args);

va_end(args);
}

/**
* lt_message_vprintf:
* @type:
* @category:
* @format:
* @args:
*
* FIXME
*/
void
lt_message_vprintf(lt_message_type_t type,
lt_message_flags_t flags,
lt_message_category_t category,
const char *format,
va_list args)
{
char buffer[4096];

if (type >= LT_MSG_END) {
fprintf(stderr, "[BUG] Invalid message type: %d\n", type);
return;
}
if (category >= LT_MSGCAT_END) {
fprintf(stderr, "[BUG] Invalid category type: %d\n", category);
return;
}
if (type == LT_MSG_DEBUG) {
if (!lt_message_is_enabled(category))
return;
}

vsnprintf(buffer, 4096, format, args);
if (__lt_message_handler[type]) {
__lt_message_handler[type](type, flags, category, buffer, __lt_message_handler_data[type]);
} else if (__lt_message_default_handler) {
__lt_message_default_handler(type, flags, category, buffer, __lt_message_default_handler_data);
}
if (type == LT_MSG_FATAL)
abort();
}

/**
* lt_return_if_fail_warning:
* @pretty_function:
* @expression:
*
* FIXME
*/
void
lt_return_if_fail_warning(const char *pretty_function,
const char *expression)
{
lt_message_printf(LT_MSG_CRITICAL,
LT_MSG_FLAG_NONE,
0,
"%s: assertion `%s' failed",
pretty_function,
expression);
}
Something went wrong with that request. Please try again.