FreeRDP
TestWLogCallback.c
1 #include <winpr/crt.h>
2 #include <winpr/tchar.h>
3 #include <winpr/path.h>
4 #include <winpr/wlog.h>
5 
6 typedef struct
7 {
8  UINT32 level;
9  char* msg;
10  char* channel;
11 } test_t;
12 
13 static const char* function = NULL;
14 static const char* channels[] = { "com.test.channelA", "com.test.channelB" };
15 
16 static const test_t messages[] = { { WLOG_INFO, "this is a test", "com.test.channelA" },
17  { WLOG_INFO, "Just some info", "com.test.channelB" },
18  { WLOG_WARN, "this is a %dnd %s", "com.test.channelA" },
19  { WLOG_WARN, "we're warning a %dnd %s", "com.test.channelB" },
20  { WLOG_ERROR, "this is an error", "com.test.channelA" },
21  { WLOG_ERROR, "we've got an error", "com.test.channelB" },
22  { WLOG_TRACE, "this is a trace output", "com.test.channelA" },
23  { WLOG_TRACE, "leaving a trace behind", "com.test.channelB" } };
24 
25 static BOOL success = TRUE;
26 static int pos = 0;
27 
28 static BOOL check(const wLogMessage* msg)
29 {
30  BOOL rc = TRUE;
31  if (!msg)
32  rc = FALSE;
33  else if (strcmp(msg->FileName, __FILE__) != 0)
34  rc = FALSE;
35  else if (strcmp(msg->FunctionName, function) != 0)
36  rc = FALSE;
37  else if (strcmp(msg->PrefixString, messages[pos].channel) != 0)
38  rc = FALSE;
39  else if (msg->Level != messages[pos].level)
40  rc = FALSE;
41  else if (strcmp(msg->FormatString, messages[pos].msg) != 0)
42  rc = FALSE;
43  pos++;
44 
45  if (!rc)
46  {
47  (void)fprintf(stderr, "Test failed!\n");
48  success = FALSE;
49  }
50  return rc;
51 }
52 
53 static BOOL CallbackAppenderMessage(const wLogMessage* msg)
54 {
55  check(msg);
56  return TRUE;
57 }
58 
59 static BOOL CallbackAppenderData(const wLogMessage* msg)
60 {
61  (void)fprintf(stdout, "%s\n", __func__);
62  return TRUE;
63 }
64 
65 static BOOL CallbackAppenderImage(const wLogMessage* msg)
66 {
67  (void)fprintf(stdout, "%s\n", __func__);
68  return TRUE;
69 }
70 
71 static BOOL CallbackAppenderPackage(const wLogMessage* msg)
72 {
73  (void)fprintf(stdout, "%s\n", __func__);
74  return TRUE;
75 }
76 
77 int TestWLogCallback(int argc, char* argv[])
78 {
79  wLog* root = NULL;
80  wLog* logA = NULL;
81  wLog* logB = NULL;
82  wLogLayout* layout = NULL;
83  wLogAppender* appender = NULL;
84  wLogCallbacks callbacks;
85 
86  WINPR_UNUSED(argc);
87  WINPR_UNUSED(argv);
88 
89  function = __func__;
90 
91  root = WLog_GetRoot();
92 
93  WLog_SetLogAppenderType(root, WLOG_APPENDER_CALLBACK);
94 
95  appender = WLog_GetLogAppender(root);
96 
97  callbacks.data = CallbackAppenderData;
98  callbacks.image = CallbackAppenderImage;
99  callbacks.message = CallbackAppenderMessage;
100  callbacks.package = CallbackAppenderPackage;
101 
102  if (!WLog_ConfigureAppender(appender, "callbacks", (void*)&callbacks))
103  return -1;
104 
105  layout = WLog_GetLogLayout(root);
106  WLog_Layout_SetPrefixFormat(root, layout, "%mn");
107 
108  WLog_OpenAppender(root);
109 
110  logA = WLog_Get(channels[0]);
111  logB = WLog_Get(channels[1]);
112 
113  WLog_SetLogLevel(logA, WLOG_TRACE);
114  WLog_SetLogLevel(logB, WLOG_TRACE);
115 
116  WLog_Print(logA, messages[0].level, messages[0].msg);
117  WLog_Print(logB, messages[1].level, messages[1].msg);
118  WLog_Print(logA, messages[2].level, messages[2].msg, 2, "test");
119  WLog_Print(logB, messages[3].level, messages[3].msg, 2, "time");
120  WLog_Print(logA, messages[4].level, messages[4].msg);
121  WLog_Print(logB, messages[5].level, messages[5].msg);
122  WLog_Print(logA, messages[6].level, messages[6].msg);
123  WLog_Print(logB, messages[7].level, messages[7].msg);
124 
125  WLog_CloseAppender(root);
126 
127  return success ? 0 : -1;
128 }