Skip to content

Commit 173b92f

Browse files
committed
feat(logger): support setting logging level
1 parent 4f6a01c commit 173b92f

22 files changed

+270
-227
lines changed

include/LCUI/util/logger.h

+31-4
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,40 @@
3333

3434
LCUI_BEGIN_HEADER
3535

36-
LCUI_API int Logger_Log(const char* fmt, ...);
36+
typedef enum LoggerLevel {
37+
LOGGER_LEVEL_ALL,
38+
LOGGER_LEVEL_DEBUG,
39+
LOGGER_LEVEL_INFO,
40+
LOGGER_LEVEL_WARNING,
41+
LOGGER_LEVEL_ERROR,
42+
LOGGER_LEVEL_OFF
43+
} LoggerLevel;
3744

38-
LCUI_API int Logger_LogW(const wchar_t* fmt, ...);
45+
LCUI_API void Logger_SetLevel(LoggerLevel level);
3946

40-
LCUI_API void Logger_SetHandler(void(*handler)(const char*));
47+
LCUI_API int Logger_Log(LoggerLevel level, const char* fmt, ...);
4148

42-
LCUI_API void Logger_SetHandlerW(void(*handler)(const wchar_t*));
49+
LCUI_API int Logger_LogW(LoggerLevel level, const wchar_t* fmt, ...);
50+
51+
LCUI_API void Logger_SetHandler(void (*handler)(const char*));
52+
53+
LCUI_API void Logger_SetHandlerW(void (*handler)(const wchar_t*));
54+
55+
#define Logger_Info(fmt, ...) Logger_Log(LOGGER_LEVEL_INFO, fmt, ##__VA_ARGS__)
56+
#define Logger_Debug(fmt, ...) \
57+
Logger_Log(LOGGER_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
58+
#define Logger_Warning(fmt, ...) \
59+
Logger_Log(LOGGER_LEVEL_WARNING, fmt, ##__VA_ARGS__)
60+
#define Logger_Error(fmt, ...) \
61+
Logger_Log(LOGGER_LEVEL_ERROR, fmt, ##__VA_ARGS__)
62+
#define Logger_InfoW(fmt, ...) \
63+
Logger_LogW(LOGGER_LEVEL_INFO, fmt, ##__VA_ARGS__)
64+
#define Logger_DebugW(fmt, ...) \
65+
Logger_LogW(LOGGER_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
66+
#define Logger_WarningW(fmt, ...) \
67+
Logger_LogW(LOGGER_LEVEL_WARNING, fmt, ##__VA_ARGS__)
68+
#define Logger_ErrorW(fmt, ...) \
69+
Logger_LogW(LOGGER_LEVEL_ERROR, fmt, ##__VA_ARGS__)
4370

4471
LCUI_END_HEADER
4572

include/LCUI_Build.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,9 @@
5252
#define DEBUG_MSG(format, ...)
5353
#endif
5454

55-
#define LOG Logger_Log
56-
#define LOGW Logger_LogW
57-
#define _DEBUG_MSG(format, ...) \
58-
Logger_Log(__FILE__ " %d: %s(): " format, __LINE__, __FUNCTION__, \
59-
##__VA_ARGS__)
55+
#define _DEBUG_MSG(format, ...) \
56+
Logger_Log(LOGGER_LEVEL_DEBUG, __FILE__ " %d: %s(): " format, \
57+
__LINE__, __FUNCTION__, ##__VA_ARGS__)
6058

6159
#if defined(WIN32) || defined(_WIN32)
6260
#define LCUI_BUILD_IN_WIN32
@@ -69,7 +67,7 @@
6967
#define ENABLE_TOUCH_SUPPORT
7068
#undef LCUI_THREAD_PTHREAD
7169
#undef LCUI_VIDEO_DRIVER_FRAMEBUFFER
72-
#define PACKAGE_VERSION "1.1.0-beta"
70+
#define PACKAGE_VERSION "1.3.0"
7371
#else
7472
#include <LCUI/config.h>
7573
#ifndef _GNU_SOURCE

src/display.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,7 @@ static void OnSurfaceEvent(LCUI_Widget w, LCUI_WidgetEvent e, void *arg)
625625
case LCUI_WEVENT_HIDE:
626626
Surface_Hide(surface);
627627
break;
628-
case LCUI_WEVENT_RESIZE:
629-
{
628+
case LCUI_WEVENT_RESIZE: {
630629
LCUI_Rect area;
631630
RectFToInvalidArea(rect, &area);
632631
if (sync_props) {
@@ -671,8 +670,8 @@ static void OnResize(LCUI_Event e, void *arg)
671670
Widget_Resize(widget, width, height);
672671
}
673672
LCUI_RunFrame();
674-
LOG("[display] resize: (%d,%d)\n", dpy_ev->resize.width,
675-
dpy_ev->resize.height);
673+
Logger_Debug("[display] resize: (%d,%d)\n", dpy_ev->resize.width,
674+
dpy_ev->resize.height);
676675
}
677676

678677
static void OnMinMaxInfo(LCUI_Event e, void *arg)
@@ -709,7 +708,7 @@ static void OnMinMaxInfo(LCUI_Event e, void *arg)
709708
}
710709

711710
int LCUIDisplay_BindEvent(int event_id, LCUI_EventFunc func, void *arg,
712-
void *data, void(*destroy_data)(void *))
711+
void *data, void (*destroy_data)(void *))
713712
{
714713
if (display.active) {
715714
return display.driver->bindEvent(event_id, func, data,
@@ -724,7 +723,7 @@ int LCUI_InitDisplay(LCUI_DisplayDriver driver)
724723
if (display.active) {
725724
return -1;
726725
}
727-
LOG("[display] init ...\n");
726+
Logger_Info("[display] init ...\n");
728727
display.mode = 0;
729728
display.driver = driver;
730729
display.active = TRUE;
@@ -736,7 +735,7 @@ int LCUI_InitDisplay(LCUI_DisplayDriver driver)
736735
display.driver = LCUI_CreateDisplayDriver();
737736
}
738737
if (!display.driver) {
739-
LOG("[display] init failed\n");
738+
Logger_Warning("[display] init failed\n");
740739
LCUIDisplay_SetMode(LCUI_DMODE_DEFAULT);
741740
LCUIDisplay_Update();
742741
return -2;
@@ -749,7 +748,8 @@ int LCUI_InitDisplay(LCUI_DisplayDriver driver)
749748
Widget_BindEvent(root, "surface", OnSurfaceEvent, NULL, NULL);
750749
LCUIDisplay_SetMode(LCUI_DMODE_DEFAULT);
751750
LCUIDisplay_Update();
752-
LOG("[display] init ok, driver name: %s\n", display.driver->name);
751+
Logger_Info("[display] init ok, driver name: %s\n",
752+
display.driver->name);
753753
return 0;
754754
}
755755

src/font/fontlibrary.c

+20-18
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ static struct LCUI_FontLibraryModule {
9090

9191
/* clang-format on */
9292

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

147148
if (font->id > FONT_CACHE_MAX_SIZE) {
148-
LOG("[font] font cache size is the max size\n");
149+
Logger_Error("[font] font cache size is the max size\n");
149150
return -1;
150151
}
151152
while (font->id >= fontlib.font_cache_num * FONT_CACHE_SIZE) {
@@ -310,7 +311,8 @@ size_t LCUIFont_UpdateWeight(const int *font_ids, LCUI_FontWeight weight,
310311
if (!font_ids) {
311312
return 0;
312313
}
313-
for (len = 0; font_ids[len]; ++len);
314+
for (len = 0; font_ids[len]; ++len)
315+
;
314316
if (len < 1) {
315317
return 0;
316318
}
@@ -515,7 +517,7 @@ void LCUIFont_SetDefault(int id)
515517
LCUI_Font font = LCUIFont_GetById(id);
516518
if (font) {
517519
fontlib.default_font = font;
518-
LOG("[font] select: %s\n", font->family_name);
520+
Logger_Info("[font] select: %s\n", font->family_name);
519521
}
520522
}
521523

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

624-
LOG("[font] load file: %s\n", file);
626+
Logger_Debug("[font] load file: %s\n", file);
625627
if (!engine) {
626628
return -1;
627629
}
628630
num_fonts = fontlib.engine->open(file, &fonts);
629631
if (num_fonts < 1) {
630-
LOG("[font] failed to load file: %s\n", file);
632+
Logger_Debug("[font] failed to load file: %s\n", file);
631633
return -2;
632634
}
633635
for (i = 0; i < num_fonts; ++i) {
634636
fonts[i]->engine = engine;
635637
id = LCUIFont_Add(fonts[i]);
636-
LOG("[font] add family: %s, style name: %s, id: %d\n",
637-
fonts[i]->family_name, fonts[i]->style_name, id);
638+
Logger_Info("[font] add family: %s, style name: %s, id: %d\n",
639+
fonts[i]->family_name, fonts[i]->style_name, id);
638640
}
639641
free(fonts);
640642
return 0;
@@ -648,11 +650,11 @@ int LCUIFont_LoadFile(const char *filepath)
648650
/** 打印字体位图的信息 */
649651
void FontBitmap_PrintInfo(LCUI_FontBitmap *bitmap)
650652
{
651-
LOG("address:%p\n", bitmap);
653+
printf("address:%p\n", bitmap);
652654
if (!bitmap) {
653655
return;
654656
}
655-
LOG("top: %d, left: %d, width:%d, rows:%d\n", bitmap->top, bitmap->left,
657+
printf("top: %d, left: %d, width:%d, rows:%d\n", bitmap->top, bitmap->left,
656658
bitmap->width, bitmap->rows);
657659
}
658660

@@ -704,16 +706,16 @@ int FontBitmap_Print(LCUI_FontBitmap *fontbmp)
704706
m = y * fontbmp->width;
705707
for (x = 0; x < fontbmp->width; ++x, ++m) {
706708
if (fontbmp->buffer[m] > 128) {
707-
LOG("#");
709+
printf("#");
708710
} else if (fontbmp->buffer[m] > 64) {
709-
LOG("-");
711+
printf("-");
710712
} else {
711-
LOG(" ");
713+
printf(" ");
712714
}
713715
}
714-
LOG("\n");
716+
printf("\n");
715717
}
716-
LOG("\n");
718+
printf("\n");
717719
return 0;
718720
}
719721

@@ -857,10 +859,10 @@ static void LCUIFont_InitEngine(void)
857859
}
858860
#endif
859861
if (fontlib.engine && fontlib.engine != &fontlib.engines[0]) {
860-
LOG("[font] current font engine is: %s\n",
862+
Logger_Info("[font] current font engine is: %s\n",
861863
fontlib.engine->name);
862864
} else {
863-
LOG("[font] warning: not font engine support!\n");
865+
Logger_Warning("[font] warning: not font engine support!\n");
864866
}
865867
}
866868

@@ -976,7 +978,7 @@ static void LCUIFont_LoadDefaultFonts(void)
976978
#ifdef LCUI_BUILD_IN_WIN32
977979
LCUIFont_LoadFontsForWindows();
978980
#elif defined(USE_FONTCONFIG)
979-
LOG("[font] fontconfig enabled\n");
981+
Logger_Info("[font] fontconfig enabled\n");
980982
LCUIFont_LoadFontsByFontConfig();
981983
#else
982984
LCUIFont_LoadFontsForLinux();

src/font/textstyle.c

-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,6 @@ const wchar_t *ScanStyleEndingTag(const wchar_t *wstr, wchar_t *name)
314314
size_t i, j, len;
315315

316316
len = wcslen(wstr);
317-
//LOG("string: %S\n", wstr);
318317
if (wstr[0] != '[' || wstr[1] != '/') {
319318
return NULL;
320319
}

src/graph.c

+10-9
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,18 @@
3939

4040
void Graph_PrintInfo(LCUI_Graph *graph)
4141
{
42-
LOG("address:%p\n", graph);
42+
printf("address:%p\n", graph);
4343
if (!graph) {
4444
return;
4545
}
4646

47-
LOG("width:%d, ", graph->width);
48-
LOG("height:%d, ", graph->height);
49-
LOG("opacity:%.2f, ", graph->opacity);
50-
LOG("%s\n", graph->color_type == LCUI_COLOR_TYPE_ARGB ? "RGBA" : "RGB");
47+
printf("width:%d, ", graph->width);
48+
printf("height:%d, ", graph->height);
49+
printf("opacity:%.2f, ", graph->opacity);
50+
printf("%s\n",
51+
graph->color_type == LCUI_COLOR_TYPE_ARGB ? "RGBA" : "RGB");
5152
if (graph->quote.is_valid) {
52-
LOG("graph src:");
53+
printf("graph src:");
5354
Graph_PrintInfo(Graph_GetQuote(graph));
5455
}
5556
}
@@ -701,7 +702,7 @@ static uchar_t Graph_BilinearResamplingCore(uchar_t a, uchar_t b, uchar_t c,
701702
uchar_t d, float dx, float dy)
702703
{
703704
return (uchar_t)(a * (1 - dx) * (1 - dy) + b * (dx) * (1 - dy) +
704-
c * (dy) * (1 - dx) + d * (dx * dy));
705+
c * (dy) * (1 - dx) + d * (dx * dy));
705706
}
706707

707708
/*-------------------------------- End ARGB --------------------------------*/
@@ -1043,8 +1044,8 @@ int Graph_ZoomBilinear(const LCUI_Graph *graph, LCUI_Graph *buff,
10431044
if (graph->color_type != LCUI_COLOR_TYPE_RGB &&
10441045
graph->color_type != LCUI_COLOR_TYPE_ARGB) {
10451046
/* fall back to nearest scaling */
1046-
LOG("[graph] unable to perform bilinear scaling, "
1047-
"fallback...\n");
1047+
Logger_Debug("[graph] unable to perform bilinear scaling, "
1048+
"fallback...\n");
10481049
return Graph_Zoom(graph, buff, keep_scale, width, height);
10491050
}
10501051
if (!Graph_IsValid(graph) || (width <= 0 && height <= 0)) {

src/gui/builder.c

+16-12
Original file line numberDiff line numberDiff line change
@@ -272,20 +272,23 @@ static void ParseNode(XMLParserContext ctx, xmlNodePtr node)
272272
if (node->type == XML_ELEMENT_NODE) {
273273
p = RBTree_CustomGetData(&self.parsers, node->name);
274274
if (!p) {
275-
proto = LCUIWidget_GetPrototype(node->name);
275+
proto =
276+
LCUIWidget_GetPrototype((char *)node->name);
276277
/* If there is no suitable parser, but a widget
277278
* prototype with the same name already exists,
278279
* use the widget parser
279280
*/
280281
if (proto) {
281282
p = &parser_list[1];
283+
} else {
284+
continue;
282285
}
283286
}
284287
} else {
285288
p = ctx->parent_parser;
286-
}
287-
if (!p) {
288-
continue;
289+
if (!p) {
290+
continue;
291+
}
289292
}
290293
cur_ctx = *ctx;
291294
cur_ctx.parent_widget = ctx->widget;
@@ -298,12 +301,12 @@ static void ParseNode(XMLParserContext ctx, xmlNodePtr node)
298301
case PB_NEXT:
299302
break;
300303
case PB_WARNING:
301-
LOG("[builder] %s (%d): warning: %s node.\n",
304+
Logger_Warning("[builder] %s (%d): warning: %s node.\n",
302305
node->doc->name, node->line, node->name);
303306
break;
304307
case PB_ERROR:
305308
default:
306-
LOG("[builder] %s (%d): error: %s node.\n",
309+
Logger_Error("[builder] %s (%d): error: %s node.\n",
307310
node->doc->name, node->line, node->name);
308311
break;
309312
}
@@ -317,7 +320,7 @@ static void ParseNode(XMLParserContext ctx, xmlNodePtr node)
317320
LCUI_Widget LCUIBuilder_LoadString(const char *str, int size)
318321
{
319322
#ifndef USE_LCUI_BUILDER
320-
LOG(WARN_TXT);
323+
Logger_Warning(WARN_TXT);
321324
#else
322325
xmlDocPtr doc;
323326
xmlNodePtr cur;
@@ -326,12 +329,12 @@ LCUI_Widget LCUIBuilder_LoadString(const char *str, int size)
326329
memset(&ctx, 0, sizeof(ctx));
327330
doc = xmlParseMemory(str, size);
328331
if (!doc) {
329-
LOG("[builder] Failed to parse xml form memory\n");
332+
Logger_Error("[builder] Failed to parse xml form memory\n");
330333
goto FAILED;
331334
}
332335
cur = xmlDocGetRootElement(doc);
333336
if (xmlStrcasecmp(cur->name, BAD_CAST "lcui-app")) {
334-
LOG("[builder] error root node name: %s\n", cur->name);
337+
Logger_Error("[builder] error root node name: %s\n", cur->name);
335338
goto FAILED;
336339
}
337340
if (!self.active) {
@@ -350,7 +353,7 @@ LCUI_Widget LCUIBuilder_LoadString(const char *str, int size)
350353
LCUI_Widget LCUIBuilder_LoadFile(const char *filepath)
351354
{
352355
#ifndef USE_LCUI_BUILDER
353-
LOG(WARN_TXT);
356+
Logger_Warning(WARN_TXT);
354357
#else
355358
xmlDocPtr doc;
356359
xmlNodePtr cur;
@@ -360,12 +363,13 @@ LCUI_Widget LCUIBuilder_LoadFile(const char *filepath)
360363
ctx.space = filepath;
361364
doc = xmlParseFile(filepath);
362365
if (!doc) {
363-
LOG("[builder] Failed to parse xml file: %s\n", filepath);
366+
Logger_Error("[builder] Failed to parse xml file: %s\n",
367+
filepath);
364368
goto FAILED;
365369
}
366370
cur = xmlDocGetRootElement(doc);
367371
if (xmlStrcasecmp(cur->name, BAD_CAST "lcui-app")) {
368-
LOG("[builder] error root node name: %s\n", cur->name);
372+
Logger_Error("[builder] error root node name: %s\n", cur->name);
369373
goto FAILED;
370374
}
371375
if (!self.active) {

0 commit comments

Comments
 (0)