FreeRDP
Appender.c
1 
20 #include <winpr/config.h>
21 
22 #include "Appender.h"
23 
24 void WLog_Appender_Free(wLog* log, wLogAppender* appender)
25 {
26  if (!appender)
27  return;
28 
29  if (appender->Layout)
30  {
31  WLog_Layout_Free(log, appender->Layout);
32  appender->Layout = NULL;
33  }
34 
35  DeleteCriticalSection(&appender->lock);
36  appender->Free(appender);
37 }
38 
39 wLogAppender* WLog_GetLogAppender(wLog* log)
40 {
41  if (!log)
42  return NULL;
43 
44  if (!log->Appender)
45  return WLog_GetLogAppender(log->Parent);
46 
47  return log->Appender;
48 }
49 
50 BOOL WLog_OpenAppender(wLog* log)
51 {
52  int status = 0;
53  wLogAppender* appender = NULL;
54 
55  appender = WLog_GetLogAppender(log);
56 
57  if (!appender)
58  return FALSE;
59 
60  if (!appender->Open)
61  return TRUE;
62 
63  if (!appender->active)
64  {
65  status = appender->Open(log, appender);
66  appender->active = TRUE;
67  }
68 
69  return status;
70 }
71 
72 BOOL WLog_CloseAppender(wLog* log)
73 {
74  int status = 0;
75  wLogAppender* appender = NULL;
76 
77  appender = WLog_GetLogAppender(log);
78 
79  if (!appender)
80  return FALSE;
81 
82  if (!appender->Close)
83  return TRUE;
84 
85  if (appender->active)
86  {
87  status = appender->Close(log, appender);
88  appender->active = FALSE;
89  }
90 
91  return status;
92 }
93 
94 static wLogAppender* WLog_Appender_New(wLog* log, DWORD logAppenderType)
95 {
96  wLogAppender* appender = NULL;
97 
98  if (!log)
99  return NULL;
100 
101  switch (logAppenderType)
102  {
103  case WLOG_APPENDER_CONSOLE:
104  appender = WLog_ConsoleAppender_New(log);
105  break;
106  case WLOG_APPENDER_FILE:
107  appender = WLog_FileAppender_New(log);
108  break;
109  case WLOG_APPENDER_BINARY:
110  appender = WLog_BinaryAppender_New(log);
111  break;
112  case WLOG_APPENDER_CALLBACK:
113  appender = WLog_CallbackAppender_New(log);
114  break;
115 #ifdef WINPR_HAVE_SYSLOG_H
116  case WLOG_APPENDER_SYSLOG:
117  appender = WLog_SyslogAppender_New(log);
118  break;
119 #endif
120 #ifdef WINPR_HAVE_JOURNALD_H
121  case WLOG_APPENDER_JOURNALD:
122  appender = WLog_JournaldAppender_New(log);
123  break;
124 #endif
125  case WLOG_APPENDER_UDP:
126  appender = WLog_UdpAppender_New(log);
127  break;
128  default:
129  (void)fprintf(stderr, "%s: unknown handler type %" PRIu32 "\n", __func__,
130  logAppenderType);
131  appender = NULL;
132  break;
133  }
134 
135  if (!appender)
136  appender = WLog_ConsoleAppender_New(log);
137 
138  if (!appender)
139  return NULL;
140 
141  if (!(appender->Layout = WLog_Layout_New(log)))
142  {
143  WLog_Appender_Free(log, appender);
144  return NULL;
145  }
146 
147  InitializeCriticalSectionAndSpinCount(&appender->lock, 4000);
148 
149  return appender;
150 }
151 
152 BOOL WLog_SetLogAppenderType(wLog* log, DWORD logAppenderType)
153 {
154  if (!log)
155  return FALSE;
156 
157  if (log->Appender)
158  {
159  WLog_Appender_Free(log, log->Appender);
160  log->Appender = NULL;
161  }
162 
163  log->Appender = WLog_Appender_New(log, logAppenderType);
164  return log->Appender != NULL;
165 }
166 
167 BOOL WLog_ConfigureAppender(wLogAppender* appender, const char* setting, void* value)
168 {
169  /* Just check the settings string is not empty */
170  if (!appender || !setting || (strnlen(setting, 2) == 0))
171  return FALSE;
172 
173  if (appender->Set)
174  return appender->Set(appender, setting, value);
175  else
176  return FALSE;
177 }