Skip to content

Commit

Permalink
feat(logger): support setting logging level
Browse files Browse the repository at this point in the history
  • Loading branch information
lc-soft committed Oct 3, 2019
1 parent 4f6a01c commit 173b92f
Show file tree
Hide file tree
Showing 22 changed files with 270 additions and 227 deletions.
35 changes: 31 additions & 4 deletions include/LCUI/util/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,40 @@

LCUI_BEGIN_HEADER

LCUI_API int Logger_Log(const char* fmt, ...);
typedef enum LoggerLevel {
LOGGER_LEVEL_ALL,
LOGGER_LEVEL_DEBUG,
LOGGER_LEVEL_INFO,
LOGGER_LEVEL_WARNING,
LOGGER_LEVEL_ERROR,
LOGGER_LEVEL_OFF
} LoggerLevel;

LCUI_API int Logger_LogW(const wchar_t* fmt, ...);
LCUI_API void Logger_SetLevel(LoggerLevel level);

LCUI_API void Logger_SetHandler(void(*handler)(const char*));
LCUI_API int Logger_Log(LoggerLevel level, const char* fmt, ...);

LCUI_API void Logger_SetHandlerW(void(*handler)(const wchar_t*));
LCUI_API int Logger_LogW(LoggerLevel level, const wchar_t* fmt, ...);

LCUI_API void Logger_SetHandler(void (*handler)(const char*));

LCUI_API void Logger_SetHandlerW(void (*handler)(const wchar_t*));

#define Logger_Info(fmt, ...) Logger_Log(LOGGER_LEVEL_INFO, fmt, ##__VA_ARGS__)
#define Logger_Debug(fmt, ...) \
Logger_Log(LOGGER_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
#define Logger_Warning(fmt, ...) \
Logger_Log(LOGGER_LEVEL_WARNING, fmt, ##__VA_ARGS__)
#define Logger_Error(fmt, ...) \
Logger_Log(LOGGER_LEVEL_ERROR, fmt, ##__VA_ARGS__)
#define Logger_InfoW(fmt, ...) \
Logger_LogW(LOGGER_LEVEL_INFO, fmt, ##__VA_ARGS__)
#define Logger_DebugW(fmt, ...) \
Logger_LogW(LOGGER_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
#define Logger_WarningW(fmt, ...) \
Logger_LogW(LOGGER_LEVEL_WARNING, fmt, ##__VA_ARGS__)
#define Logger_ErrorW(fmt, ...) \
Logger_LogW(LOGGER_LEVEL_ERROR, fmt, ##__VA_ARGS__)

LCUI_END_HEADER

Expand Down
10 changes: 4 additions & 6 deletions include/LCUI_Build.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@
#define DEBUG_MSG(format, ...)
#endif

#define LOG Logger_Log
#define LOGW Logger_LogW
#define _DEBUG_MSG(format, ...) \
Logger_Log(__FILE__ " %d: %s(): " format, __LINE__, __FUNCTION__, \
##__VA_ARGS__)
#define _DEBUG_MSG(format, ...) \
Logger_Log(LOGGER_LEVEL_DEBUG, __FILE__ " %d: %s(): " format, \
__LINE__, __FUNCTION__, ##__VA_ARGS__)

#if defined(WIN32) || defined(_WIN32)
#define LCUI_BUILD_IN_WIN32
Expand All @@ -69,7 +67,7 @@
#define ENABLE_TOUCH_SUPPORT
#undef LCUI_THREAD_PTHREAD
#undef LCUI_VIDEO_DRIVER_FRAMEBUFFER
#define PACKAGE_VERSION "1.1.0-beta"
#define PACKAGE_VERSION "1.3.0"
#else
#include <LCUI/config.h>
#ifndef _GNU_SOURCE
Expand Down
16 changes: 8 additions & 8 deletions src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,7 @@ static void OnSurfaceEvent(LCUI_Widget w, LCUI_WidgetEvent e, void *arg)
case LCUI_WEVENT_HIDE:
Surface_Hide(surface);
break;
case LCUI_WEVENT_RESIZE:
{
case LCUI_WEVENT_RESIZE: {
LCUI_Rect area;
RectFToInvalidArea(rect, &area);
if (sync_props) {
Expand Down Expand Up @@ -671,8 +670,8 @@ static void OnResize(LCUI_Event e, void *arg)
Widget_Resize(widget, width, height);
}
LCUI_RunFrame();
LOG("[display] resize: (%d,%d)\n", dpy_ev->resize.width,
dpy_ev->resize.height);
Logger_Debug("[display] resize: (%d,%d)\n", dpy_ev->resize.width,
dpy_ev->resize.height);
}

static void OnMinMaxInfo(LCUI_Event e, void *arg)
Expand Down Expand Up @@ -709,7 +708,7 @@ static void OnMinMaxInfo(LCUI_Event e, void *arg)
}

int LCUIDisplay_BindEvent(int event_id, LCUI_EventFunc func, void *arg,
void *data, void(*destroy_data)(void *))
void *data, void (*destroy_data)(void *))
{
if (display.active) {
return display.driver->bindEvent(event_id, func, data,
Expand All @@ -724,7 +723,7 @@ int LCUI_InitDisplay(LCUI_DisplayDriver driver)
if (display.active) {
return -1;
}
LOG("[display] init ...\n");
Logger_Info("[display] init ...\n");
display.mode = 0;
display.driver = driver;
display.active = TRUE;
Expand All @@ -736,7 +735,7 @@ int LCUI_InitDisplay(LCUI_DisplayDriver driver)
display.driver = LCUI_CreateDisplayDriver();
}
if (!display.driver) {
LOG("[display] init failed\n");
Logger_Warning("[display] init failed\n");
LCUIDisplay_SetMode(LCUI_DMODE_DEFAULT);
LCUIDisplay_Update();
return -2;
Expand All @@ -749,7 +748,8 @@ int LCUI_InitDisplay(LCUI_DisplayDriver driver)
Widget_BindEvent(root, "surface", OnSurfaceEvent, NULL, NULL);
LCUIDisplay_SetMode(LCUI_DMODE_DEFAULT);
LCUIDisplay_Update();
LOG("[display] init ok, driver name: %s\n", display.driver->name);
Logger_Info("[display] init ok, driver name: %s\n",
display.driver->name);
return 0;
}

Expand Down
38 changes: 20 additions & 18 deletions src/font/fontlibrary.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ static struct LCUI_FontLibraryModule {

/* clang-format on */

#define FontBitmap_IsValid(fbmp) ((fbmp) && (fbmp)->width > 0 && (fbmp)->rows > 0)
#define FontBitmap_IsValid(fbmp) \
((fbmp) && (fbmp)->width > 0 && (fbmp)->rows > 0)
#define SelectChar(ch) (RBTree *)RBTree_GetData(&fontlib.bitmap_cache, ch)
#define SelectFont(ch, font_id) (RBTree *)RBTree_GetData(ch, font_id)
#define SelectBitmap(font, size) (LCUI_FontBitmap *)RBTree_GetData(font, size)
Expand Down Expand Up @@ -145,7 +146,7 @@ static int SetFontCache(LCUI_Font font)
LCUI_FontCache *caches, cache;

if (font->id > FONT_CACHE_MAX_SIZE) {
LOG("[font] font cache size is the max size\n");
Logger_Error("[font] font cache size is the max size\n");
return -1;
}
while (font->id >= fontlib.font_cache_num * FONT_CACHE_SIZE) {
Expand Down Expand Up @@ -310,7 +311,8 @@ size_t LCUIFont_UpdateWeight(const int *font_ids, LCUI_FontWeight weight,
if (!font_ids) {
return 0;
}
for (len = 0; font_ids[len]; ++len);
for (len = 0; font_ids[len]; ++len)
;
if (len < 1) {
return 0;
}
Expand Down Expand Up @@ -515,7 +517,7 @@ void LCUIFont_SetDefault(int id)
LCUI_Font font = LCUIFont_GetById(id);
if (font) {
fontlib.default_font = font;
LOG("[font] select: %s\n", font->family_name);
Logger_Info("[font] select: %s\n", font->family_name);
}
}

Expand Down Expand Up @@ -621,20 +623,20 @@ static int LCUIFont_LoadFileEx(LCUI_FontEngine *engine, const char *file)
LCUI_Font *fonts;
int i, num_fonts, id;

LOG("[font] load file: %s\n", file);
Logger_Debug("[font] load file: %s\n", file);
if (!engine) {
return -1;
}
num_fonts = fontlib.engine->open(file, &fonts);
if (num_fonts < 1) {
LOG("[font] failed to load file: %s\n", file);
Logger_Debug("[font] failed to load file: %s\n", file);
return -2;
}
for (i = 0; i < num_fonts; ++i) {
fonts[i]->engine = engine;
id = LCUIFont_Add(fonts[i]);
LOG("[font] add family: %s, style name: %s, id: %d\n",
fonts[i]->family_name, fonts[i]->style_name, id);
Logger_Info("[font] add family: %s, style name: %s, id: %d\n",
fonts[i]->family_name, fonts[i]->style_name, id);
}
free(fonts);
return 0;
Expand All @@ -648,11 +650,11 @@ int LCUIFont_LoadFile(const char *filepath)
/** 打印字体位图的信息 */
void FontBitmap_PrintInfo(LCUI_FontBitmap *bitmap)
{
LOG("address:%p\n", bitmap);
printf("address:%p\n", bitmap);
if (!bitmap) {
return;
}
LOG("top: %d, left: %d, width:%d, rows:%d\n", bitmap->top, bitmap->left,
printf("top: %d, left: %d, width:%d, rows:%d\n", bitmap->top, bitmap->left,
bitmap->width, bitmap->rows);
}

Expand Down Expand Up @@ -704,16 +706,16 @@ int FontBitmap_Print(LCUI_FontBitmap *fontbmp)
m = y * fontbmp->width;
for (x = 0; x < fontbmp->width; ++x, ++m) {
if (fontbmp->buffer[m] > 128) {
LOG("#");
printf("#");
} else if (fontbmp->buffer[m] > 64) {
LOG("-");
printf("-");
} else {
LOG(" ");
printf(" ");
}
}
LOG("\n");
printf("\n");
}
LOG("\n");
printf("\n");
return 0;
}

Expand Down Expand Up @@ -857,10 +859,10 @@ static void LCUIFont_InitEngine(void)
}
#endif
if (fontlib.engine && fontlib.engine != &fontlib.engines[0]) {
LOG("[font] current font engine is: %s\n",
Logger_Info("[font] current font engine is: %s\n",
fontlib.engine->name);
} else {
LOG("[font] warning: not font engine support!\n");
Logger_Warning("[font] warning: not font engine support!\n");
}
}

Expand Down Expand Up @@ -976,7 +978,7 @@ static void LCUIFont_LoadDefaultFonts(void)
#ifdef LCUI_BUILD_IN_WIN32
LCUIFont_LoadFontsForWindows();
#elif defined(USE_FONTCONFIG)
LOG("[font] fontconfig enabled\n");
Logger_Info("[font] fontconfig enabled\n");
LCUIFont_LoadFontsByFontConfig();
#else
LCUIFont_LoadFontsForLinux();
Expand Down
1 change: 0 additions & 1 deletion src/font/textstyle.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ const wchar_t *ScanStyleEndingTag(const wchar_t *wstr, wchar_t *name)
size_t i, j, len;

len = wcslen(wstr);
//LOG("string: %S\n", wstr);
if (wstr[0] != '[' || wstr[1] != '/') {
return NULL;
}
Expand Down
19 changes: 10 additions & 9 deletions src/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,18 @@

void Graph_PrintInfo(LCUI_Graph *graph)
{
LOG("address:%p\n", graph);
printf("address:%p\n", graph);
if (!graph) {
return;
}

LOG("width:%d, ", graph->width);
LOG("height:%d, ", graph->height);
LOG("opacity:%.2f, ", graph->opacity);
LOG("%s\n", graph->color_type == LCUI_COLOR_TYPE_ARGB ? "RGBA" : "RGB");
printf("width:%d, ", graph->width);
printf("height:%d, ", graph->height);
printf("opacity:%.2f, ", graph->opacity);
printf("%s\n",
graph->color_type == LCUI_COLOR_TYPE_ARGB ? "RGBA" : "RGB");
if (graph->quote.is_valid) {
LOG("graph src:");
printf("graph src:");
Graph_PrintInfo(Graph_GetQuote(graph));
}
}
Expand Down Expand Up @@ -701,7 +702,7 @@ static uchar_t Graph_BilinearResamplingCore(uchar_t a, uchar_t b, uchar_t c,
uchar_t d, float dx, float dy)
{
return (uchar_t)(a * (1 - dx) * (1 - dy) + b * (dx) * (1 - dy) +
c * (dy) * (1 - dx) + d * (dx * dy));
c * (dy) * (1 - dx) + d * (dx * dy));
}

/*-------------------------------- End ARGB --------------------------------*/
Expand Down Expand Up @@ -1043,8 +1044,8 @@ int Graph_ZoomBilinear(const LCUI_Graph *graph, LCUI_Graph *buff,
if (graph->color_type != LCUI_COLOR_TYPE_RGB &&
graph->color_type != LCUI_COLOR_TYPE_ARGB) {
/* fall back to nearest scaling */
LOG("[graph] unable to perform bilinear scaling, "
"fallback...\n");
Logger_Debug("[graph] unable to perform bilinear scaling, "
"fallback...\n");
return Graph_Zoom(graph, buff, keep_scale, width, height);
}
if (!Graph_IsValid(graph) || (width <= 0 && height <= 0)) {
Expand Down
28 changes: 16 additions & 12 deletions src/gui/builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,20 +272,23 @@ static void ParseNode(XMLParserContext ctx, xmlNodePtr node)
if (node->type == XML_ELEMENT_NODE) {
p = RBTree_CustomGetData(&self.parsers, node->name);
if (!p) {
proto = LCUIWidget_GetPrototype(node->name);
proto =
LCUIWidget_GetPrototype((char *)node->name);
/* If there is no suitable parser, but a widget
* prototype with the same name already exists,
* use the widget parser
*/
if (proto) {
p = &parser_list[1];
} else {
continue;
}
}
} else {
p = ctx->parent_parser;
}
if (!p) {
continue;
if (!p) {
continue;
}
}
cur_ctx = *ctx;
cur_ctx.parent_widget = ctx->widget;
Expand All @@ -298,12 +301,12 @@ static void ParseNode(XMLParserContext ctx, xmlNodePtr node)
case PB_NEXT:
break;
case PB_WARNING:
LOG("[builder] %s (%d): warning: %s node.\n",
Logger_Warning("[builder] %s (%d): warning: %s node.\n",
node->doc->name, node->line, node->name);
break;
case PB_ERROR:
default:
LOG("[builder] %s (%d): error: %s node.\n",
Logger_Error("[builder] %s (%d): error: %s node.\n",
node->doc->name, node->line, node->name);
break;
}
Expand All @@ -317,7 +320,7 @@ static void ParseNode(XMLParserContext ctx, xmlNodePtr node)
LCUI_Widget LCUIBuilder_LoadString(const char *str, int size)
{
#ifndef USE_LCUI_BUILDER
LOG(WARN_TXT);
Logger_Warning(WARN_TXT);
#else
xmlDocPtr doc;
xmlNodePtr cur;
Expand All @@ -326,12 +329,12 @@ LCUI_Widget LCUIBuilder_LoadString(const char *str, int size)
memset(&ctx, 0, sizeof(ctx));
doc = xmlParseMemory(str, size);
if (!doc) {
LOG("[builder] Failed to parse xml form memory\n");
Logger_Error("[builder] Failed to parse xml form memory\n");
goto FAILED;
}
cur = xmlDocGetRootElement(doc);
if (xmlStrcasecmp(cur->name, BAD_CAST "lcui-app")) {
LOG("[builder] error root node name: %s\n", cur->name);
Logger_Error("[builder] error root node name: %s\n", cur->name);
goto FAILED;
}
if (!self.active) {
Expand All @@ -350,7 +353,7 @@ LCUI_Widget LCUIBuilder_LoadString(const char *str, int size)
LCUI_Widget LCUIBuilder_LoadFile(const char *filepath)
{
#ifndef USE_LCUI_BUILDER
LOG(WARN_TXT);
Logger_Warning(WARN_TXT);
#else
xmlDocPtr doc;
xmlNodePtr cur;
Expand All @@ -360,12 +363,13 @@ LCUI_Widget LCUIBuilder_LoadFile(const char *filepath)
ctx.space = filepath;
doc = xmlParseFile(filepath);
if (!doc) {
LOG("[builder] Failed to parse xml file: %s\n", filepath);
Logger_Error("[builder] Failed to parse xml file: %s\n",
filepath);
goto FAILED;
}
cur = xmlDocGetRootElement(doc);
if (xmlStrcasecmp(cur->name, BAD_CAST "lcui-app")) {
LOG("[builder] error root node name: %s\n", cur->name);
Logger_Error("[builder] error root node name: %s\n", cur->name);
goto FAILED;
}
if (!self.active) {
Expand Down
Loading

0 comments on commit 173b92f

Please sign in to comment.