2 * Copyright (C) 2011-2012 Juho Vähä-Herttua
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
23 logger_init(logger_t
*logger
)
27 logger
->level
= LOGGER_INFO
;
28 logger
->callback
= NULL
;
32 logger_set_level(logger_t
*logger
, int level
)
36 logger
->level
= level
;
40 logger_set_callback(logger_t
*logger
, logger_callback_t callback
)
44 logger
->callback
= callback
;
48 logger_utf8_to_local(const char *str
)
52 /* FIXME: This is only implemented on Windows for now */
53 #if defined(_WIN32) || defined(_WIN64)
58 wclen
= MultiByteToWideChar(CP_UTF8
, 0, str
, -1, NULL
, 0);
59 wcstr
= malloc(sizeof(WCHAR
) * wclen
);
60 MultiByteToWideChar(CP_UTF8
, 0, str
, -1, wcstr
, wclen
);
62 mblen
= WideCharToMultiByte(CP_ACP
, 0, wcstr
, wclen
, NULL
, 0, NULL
, &failed
);
64 /* Invalid characters in input, conversion failed */
69 ret
= malloc(sizeof(CHAR
) * mblen
);
70 WideCharToMultiByte(CP_ACP
, 0, wcstr
, wclen
, ret
, mblen
, NULL
, NULL
);
78 logger_log(logger_t
*logger
, int level
, const char *fmt
, ...)
83 if (level
> logger
->level
)
86 buffer
[sizeof(buffer
)-1] = '\0';
88 vsnprintf(buffer
, sizeof(buffer
)-1, fmt
, ap
);
91 if (logger
->callback
) {
92 logger
->callback(level
, buffer
);
94 char *local
= logger_utf8_to_local(buffer
);
97 fprintf(stderr
, "%s", local
);
100 fprintf(stderr
, "%s", buffer
);