FreeRDP
TestWLog.c
1 #include <winpr/crt.h>
2 #include <winpr/tchar.h>
3 #include <winpr/path.h>
4 #include <winpr/file.h>
5 #include <winpr/wlog.h>
6 
7 int TestWLog(int argc, char* argv[])
8 {
9  wLog* root = NULL;
10  wLog* logA = NULL;
11  wLog* logB = NULL;
12  wLogLayout* layout = NULL;
13  wLogAppender* appender = NULL;
14  char* tmp_path = NULL;
15  char* wlog_file = NULL;
16  int result = 1;
17 
18  WINPR_UNUSED(argc);
19  WINPR_UNUSED(argv);
20 
21  if (!(tmp_path = GetKnownPath(KNOWN_PATH_TEMP)))
22  {
23  (void)fprintf(stderr, "Failed to get temporary directory!\n");
24  goto out;
25  }
26 
27  root = WLog_GetRoot();
28 
29  WLog_SetLogAppenderType(root, WLOG_APPENDER_BINARY);
30 
31  appender = WLog_GetLogAppender(root);
32  if (!WLog_ConfigureAppender(appender, "outputfilename", "test_w.log"))
33  goto out;
34  if (!WLog_ConfigureAppender(appender, "outputfilepath", tmp_path))
35  goto out;
36 
37  layout = WLog_GetLogLayout(root);
38  WLog_Layout_SetPrefixFormat(root, layout, "[%lv:%mn] [%fl|%fn|%ln] - ");
39 
40  WLog_OpenAppender(root);
41 
42  logA = WLog_Get("com.test.ChannelA");
43  logB = WLog_Get("com.test.ChannelB");
44 
45  WLog_SetLogLevel(logA, WLOG_INFO);
46  WLog_SetLogLevel(logB, WLOG_ERROR);
47 
48  WLog_Print(logA, WLOG_INFO, "this is a test");
49  WLog_Print(logA, WLOG_WARN, "this is a %dnd %s", 2, "test");
50  WLog_Print(logA, WLOG_ERROR, "this is an error");
51  WLog_Print(logA, WLOG_TRACE, "this is a trace output");
52 
53  WLog_Print(logB, WLOG_INFO, "just some info");
54  WLog_Print(logB, WLOG_WARN, "we're warning a %dnd %s", 2, "time");
55  WLog_Print(logB, WLOG_ERROR, "we've got an error");
56  WLog_Print(logB, WLOG_TRACE, "leaving a trace behind");
57 
58  WLog_CloseAppender(root);
59 
60  if ((wlog_file = GetCombinedPath(tmp_path, "test_w.log")))
61  winpr_DeleteFile(wlog_file);
62 
63  result = 0;
64 out:
65  free(wlog_file);
66  free(tmp_path);
67 
68  return result;
69 }