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.
24 logger_init(logger_t
*logger
)
28 logger
->level
= LOGGER_INFO
;
29 logger
->callback
= NULL
;
33 logger_set_level(logger_t
*logger
, int level
)
37 logger
->level
= level
;
41 logger_set_callback(logger_t
*logger
, logger_callback_t callback
)
45 logger
->callback
= callback
;
49 logger_utf8_to_local(const char *str
)
53 /* FIXME: This is only implemented on Windows for now */
54 #if defined(_WIN32) || defined(_WIN64)
59 wclen
= MultiByteToWideChar(CP_UTF8
, 0, str
, -1, NULL
, 0);
60 wcstr
= malloc(sizeof(WCHAR
) * wclen
);
61 MultiByteToWideChar(CP_UTF8
, 0, str
, -1, wcstr
, wclen
);
63 mblen
= WideCharToMultiByte(CP_ACP
, 0, wcstr
, wclen
, NULL
, 0, NULL
, &failed
);
65 /* Invalid characters in input, conversion failed */
70 ret
= malloc(sizeof(CHAR
) * mblen
);
71 WideCharToMultiByte(CP_ACP
, 0, wcstr
, wclen
, ret
, mblen
, NULL
, NULL
);
79 logger_log(logger_t
*logger
, int level
, const char *fmt
, ...)
84 if (level
> logger
->level
)
87 buffer
[sizeof(buffer
)-1] = '\0';
89 vsnprintf(buffer
, sizeof(buffer
)-1, fmt
, ap
);
92 if (logger
->callback
) {
93 logger
->callback(level
, buffer
);
95 char *local
= logger_utf8_to_local(buffer
);
98 fprintf(stderr
, "%s", local
);
101 fprintf(stderr
, "%s", buffer
);