FreeRDP
BinaryAppender.c
1 
22 #include <winpr/config.h>
23 
24 #include "BinaryAppender.h"
25 #include <winpr/crt.h>
26 #include <winpr/assert.h>
27 #include <winpr/file.h>
28 #include <winpr/path.h>
29 #include <winpr/stream.h>
30 
31 typedef struct
32 {
33  WLOG_APPENDER_COMMON();
34 
35  char* FileName;
36  char* FilePath;
37  char* FullFileName;
38  FILE* FileDescriptor;
39 } wLogBinaryAppender;
40 
41 static BOOL WLog_BinaryAppender_Open(wLog* log, wLogAppender* appender)
42 {
43  wLogBinaryAppender* binaryAppender = NULL;
44  if (!log || !appender)
45  return FALSE;
46 
47  binaryAppender = (wLogBinaryAppender*)appender;
48  if (!binaryAppender->FileName)
49  {
50  binaryAppender->FileName = (char*)malloc(MAX_PATH);
51  if (!binaryAppender->FileName)
52  return FALSE;
53  (void)sprintf_s(binaryAppender->FileName, MAX_PATH, "%" PRIu32 ".wlog",
54  GetCurrentProcessId());
55  }
56 
57  if (!binaryAppender->FilePath)
58  {
59  binaryAppender->FilePath = GetKnownSubPath(KNOWN_PATH_TEMP, "wlog");
60  if (!binaryAppender->FilePath)
61  return FALSE;
62  }
63 
64  if (!binaryAppender->FullFileName)
65  {
66  binaryAppender->FullFileName =
67  GetCombinedPath(binaryAppender->FilePath, binaryAppender->FileName);
68  if (!binaryAppender->FullFileName)
69  return FALSE;
70  }
71 
72  if (!winpr_PathFileExists(binaryAppender->FilePath))
73  {
74  if (!winpr_PathMakePath(binaryAppender->FilePath, 0))
75  return FALSE;
76  UnixChangeFileMode(binaryAppender->FilePath, 0xFFFF);
77  }
78 
79  binaryAppender->FileDescriptor = winpr_fopen(binaryAppender->FullFileName, "a+");
80 
81  if (!binaryAppender->FileDescriptor)
82  return FALSE;
83 
84  return TRUE;
85 }
86 
87 static BOOL WLog_BinaryAppender_Close(wLog* log, wLogAppender* appender)
88 {
89  wLogBinaryAppender* binaryAppender = NULL;
90 
91  if (!appender)
92  return FALSE;
93 
94  binaryAppender = (wLogBinaryAppender*)appender;
95  if (!binaryAppender->FileDescriptor)
96  return TRUE;
97 
98  if (binaryAppender->FileDescriptor)
99  (void)fclose(binaryAppender->FileDescriptor);
100 
101  binaryAppender->FileDescriptor = NULL;
102 
103  return TRUE;
104 }
105 
106 static BOOL WLog_BinaryAppender_WriteMessage(wLog* log, wLogAppender* appender,
107  wLogMessage* message)
108 {
109  FILE* fp = NULL;
110  wStream* s = NULL;
111  size_t MessageLength = 0;
112  size_t FileNameLength = 0;
113  size_t FunctionNameLength = 0;
114  size_t TextStringLength = 0;
115  BOOL ret = TRUE;
116  wLogBinaryAppender* binaryAppender = NULL;
117 
118  if (!log || !appender || !message)
119  return FALSE;
120 
121  binaryAppender = (wLogBinaryAppender*)appender;
122 
123  fp = binaryAppender->FileDescriptor;
124 
125  if (!fp)
126  return FALSE;
127 
128  FileNameLength = strnlen(message->FileName, INT_MAX);
129  FunctionNameLength = strnlen(message->FunctionName, INT_MAX);
130  TextStringLength = strnlen(message->TextString, INT_MAX);
131 
132  MessageLength =
133  16 + (4 + FileNameLength + 1) + (4 + FunctionNameLength + 1) + (4 + TextStringLength + 1);
134 
135  if ((MessageLength > UINT32_MAX) || (FileNameLength > UINT32_MAX) ||
136  (FunctionNameLength > UINT32_MAX) || (TextStringLength > UINT32_MAX))
137  return FALSE;
138 
139  s = Stream_New(NULL, MessageLength);
140  if (!s)
141  return FALSE;
142 
143  Stream_Write_UINT32(s, (UINT32)MessageLength);
144 
145  Stream_Write_UINT32(s, message->Type);
146  Stream_Write_UINT32(s, message->Level);
147 
148  WINPR_ASSERT(message->LineNumber <= UINT32_MAX);
149  Stream_Write_UINT32(s, (UINT32)message->LineNumber);
150 
151  Stream_Write_UINT32(s, (UINT32)FileNameLength);
152  Stream_Write(s, message->FileName, FileNameLength + 1);
153 
154  Stream_Write_UINT32(s, (UINT32)FunctionNameLength);
155  Stream_Write(s, message->FunctionName, FunctionNameLength + 1);
156 
157  Stream_Write_UINT32(s, (UINT32)TextStringLength);
158  Stream_Write(s, message->TextString, TextStringLength + 1);
159 
160  Stream_SealLength(s);
161 
162  if (fwrite(Stream_Buffer(s), MessageLength, 1, fp) != 1)
163  ret = FALSE;
164 
165  Stream_Free(s, TRUE);
166 
167  return ret;
168 }
169 
170 static BOOL WLog_BinaryAppender_WriteDataMessage(wLog* log, wLogAppender* appender,
171  wLogMessage* message)
172 {
173  return TRUE;
174 }
175 
176 static BOOL WLog_BinaryAppender_WriteImageMessage(wLog* log, wLogAppender* appender,
177  wLogMessage* message)
178 {
179  return TRUE;
180 }
181 
182 static BOOL WLog_BinaryAppender_Set(wLogAppender* appender, const char* setting, void* value)
183 {
184  wLogBinaryAppender* binaryAppender = (wLogBinaryAppender*)appender;
185 
186  /* Just check if the value string is longer than 0 */
187  if (!value || (strnlen(value, 2) == 0))
188  return FALSE;
189 
190  if (!strcmp("outputfilename", setting))
191  {
192  binaryAppender->FileName = _strdup((const char*)value);
193  if (!binaryAppender->FileName)
194  return FALSE;
195  }
196  else if (!strcmp("outputfilepath", setting))
197  {
198  binaryAppender->FilePath = _strdup((const char*)value);
199  if (!binaryAppender->FilePath)
200  return FALSE;
201  }
202  else
203  return FALSE;
204 
205  return TRUE;
206 }
207 
208 static void WLog_BinaryAppender_Free(wLogAppender* appender)
209 {
210  wLogBinaryAppender* binaryAppender = NULL;
211  if (appender)
212  {
213  binaryAppender = (wLogBinaryAppender*)appender;
214  free(binaryAppender->FileName);
215  free(binaryAppender->FilePath);
216  free(binaryAppender->FullFileName);
217  free(binaryAppender);
218  }
219 }
220 
221 wLogAppender* WLog_BinaryAppender_New(wLog* log)
222 {
223  wLogBinaryAppender* BinaryAppender = NULL;
224 
225  BinaryAppender = (wLogBinaryAppender*)calloc(1, sizeof(wLogBinaryAppender));
226  if (!BinaryAppender)
227  return NULL;
228 
229  BinaryAppender->Type = WLOG_APPENDER_BINARY;
230  BinaryAppender->Open = WLog_BinaryAppender_Open;
231  BinaryAppender->Close = WLog_BinaryAppender_Close;
232  BinaryAppender->WriteMessage = WLog_BinaryAppender_WriteMessage;
233  BinaryAppender->WriteDataMessage = WLog_BinaryAppender_WriteDataMessage;
234  BinaryAppender->WriteImageMessage = WLog_BinaryAppender_WriteImageMessage;
235  BinaryAppender->Free = WLog_BinaryAppender_Free;
236  BinaryAppender->Set = WLog_BinaryAppender_Set;
237 
238  return (wLogAppender*)BinaryAppender;
239 }