FreeRDP
CallbackAppender.c
1 
20 #include <winpr/config.h>
21 
22 #include "CallbackAppender.h"
23 
24 typedef struct
25 {
26  WLOG_APPENDER_COMMON();
27 
28  wLogCallbacks* callbacks;
29 } wLogCallbackAppender;
30 
31 static BOOL WLog_CallbackAppender_Open(wLog* log, wLogAppender* appender)
32 {
33  return TRUE;
34 }
35 
36 static BOOL WLog_CallbackAppender_Close(wLog* log, wLogAppender* appender)
37 {
38  return TRUE;
39 }
40 
41 static BOOL WLog_CallbackAppender_WriteMessage(wLog* log, wLogAppender* appender,
42  wLogMessage* message)
43 {
44  char prefix[WLOG_MAX_PREFIX_SIZE] = { 0 };
45  wLogCallbackAppender* callbackAppender = NULL;
46 
47  if (!appender)
48  return FALSE;
49 
50  message->PrefixString = prefix;
51  WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
52 
53  callbackAppender = (wLogCallbackAppender*)appender;
54 
55  if (callbackAppender->callbacks && callbackAppender->callbacks->message)
56  return callbackAppender->callbacks->message(message);
57  else
58  return FALSE;
59 }
60 
61 static BOOL WLog_CallbackAppender_WriteDataMessage(wLog* log, wLogAppender* appender,
62  wLogMessage* message)
63 {
64  char prefix[WLOG_MAX_PREFIX_SIZE] = { 0 };
65  wLogCallbackAppender* callbackAppender = NULL;
66 
67  if (!appender)
68  return FALSE;
69 
70  message->PrefixString = prefix;
71  WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
72 
73  callbackAppender = (wLogCallbackAppender*)appender;
74  if (callbackAppender->callbacks && callbackAppender->callbacks->data)
75  return callbackAppender->callbacks->data(message);
76  else
77  return FALSE;
78 }
79 
80 static BOOL WLog_CallbackAppender_WriteImageMessage(wLog* log, wLogAppender* appender,
81  wLogMessage* message)
82 {
83  char prefix[WLOG_MAX_PREFIX_SIZE] = { 0 };
84  wLogCallbackAppender* callbackAppender = NULL;
85 
86  if (!appender)
87  return FALSE;
88 
89  message->PrefixString = prefix;
90  WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
91 
92  callbackAppender = (wLogCallbackAppender*)appender;
93  if (callbackAppender->callbacks && callbackAppender->callbacks->image)
94  return callbackAppender->callbacks->image(message);
95  else
96  return FALSE;
97 }
98 
99 static BOOL WLog_CallbackAppender_WritePacketMessage(wLog* log, wLogAppender* appender,
100  wLogMessage* message)
101 {
102  char prefix[WLOG_MAX_PREFIX_SIZE] = { 0 };
103  wLogCallbackAppender* callbackAppender = NULL;
104 
105  if (!appender)
106  return FALSE;
107 
108  message->PrefixString = prefix;
109  WLog_Layout_GetMessagePrefix(log, appender->Layout, message);
110 
111  callbackAppender = (wLogCallbackAppender*)appender;
112  if (callbackAppender->callbacks && callbackAppender->callbacks->package)
113  return callbackAppender->callbacks->package(message);
114  else
115  return FALSE;
116 }
117 
118 static BOOL WLog_CallbackAppender_Set(wLogAppender* appender, const char* setting, void* value)
119 {
120  wLogCallbackAppender* callbackAppender = (wLogCallbackAppender*)appender;
121 
122  if (!value || (strcmp(setting, "callbacks") != 0))
123  return FALSE;
124 
125  if (!(callbackAppender->callbacks = calloc(1, sizeof(wLogCallbacks))))
126  {
127  return FALSE;
128  }
129 
130  callbackAppender->callbacks = memcpy(callbackAppender->callbacks, value, sizeof(wLogCallbacks));
131  return TRUE;
132 }
133 
134 static void WLog_CallbackAppender_Free(wLogAppender* appender)
135 {
136  wLogCallbackAppender* callbackAppender = NULL;
137  if (!appender)
138  {
139  return;
140  }
141 
142  callbackAppender = (wLogCallbackAppender*)appender;
143 
144  free(callbackAppender->callbacks);
145  free(appender);
146 }
147 
148 wLogAppender* WLog_CallbackAppender_New(wLog* log)
149 {
150  wLogCallbackAppender* CallbackAppender = NULL;
151 
152  CallbackAppender = (wLogCallbackAppender*)calloc(1, sizeof(wLogCallbackAppender));
153  if (!CallbackAppender)
154  return NULL;
155 
156  CallbackAppender->Type = WLOG_APPENDER_CALLBACK;
157 
158  CallbackAppender->Open = WLog_CallbackAppender_Open;
159  CallbackAppender->Close = WLog_CallbackAppender_Close;
160  CallbackAppender->WriteMessage = WLog_CallbackAppender_WriteMessage;
161  CallbackAppender->WriteDataMessage = WLog_CallbackAppender_WriteDataMessage;
162  CallbackAppender->WriteImageMessage = WLog_CallbackAppender_WriteImageMessage;
163  CallbackAppender->WritePacketMessage = WLog_CallbackAppender_WritePacketMessage;
164  CallbackAppender->Free = WLog_CallbackAppender_Free;
165  CallbackAppender->Set = WLog_CallbackAppender_Set;
166 
167  return (wLogAppender*)CallbackAppender;
168 }