FreeRDP
SyslogAppender.c
1 
21 #include <winpr/config.h>
22 
23 #include "SyslogAppender.h"
24 #include <syslog.h>
25 
26 typedef struct
27 {
28  WLOG_APPENDER_COMMON();
29 } wLogSyslogAppender;
30 
31 static int getSyslogLevel(DWORD level)
32 {
33  switch (level)
34  {
35  case WLOG_TRACE:
36  case WLOG_DEBUG:
37  return LOG_DEBUG;
38  case WLOG_INFO:
39  return LOG_INFO;
40  case WLOG_WARN:
41  return LOG_WARNING;
42  case WLOG_ERROR:
43  return LOG_ERR;
44  case WLOG_FATAL:
45  return LOG_CRIT;
46  case WLOG_OFF:
47  default:
48  return -1;
49  }
50 }
51 
52 static BOOL WLog_SyslogAppender_Open(wLog* log, wLogAppender* appender)
53 {
54  if (!log || !appender)
55  return FALSE;
56 
57  return TRUE;
58 }
59 
60 static BOOL WLog_SyslogAppender_Close(wLog* log, wLogAppender* appender)
61 {
62  if (!log || !appender)
63  return FALSE;
64 
65  return TRUE;
66 }
67 
68 static BOOL WLog_SyslogAppender_WriteMessage(wLog* log, wLogAppender* appender,
69  wLogMessage* message)
70 {
71  int syslogLevel = 0;
72 
73  if (!log || !appender || !message)
74  return FALSE;
75 
76  syslogLevel = getSyslogLevel(message->Level);
77  if (syslogLevel >= 0)
78  syslog(syslogLevel, "%s", message->TextString);
79 
80  return TRUE;
81 }
82 
83 static BOOL WLog_SyslogAppender_WriteDataMessage(wLog* log, wLogAppender* appender,
84  wLogMessage* message)
85 {
86  int syslogLevel = 0;
87 
88  if (!log || !appender || !message)
89  return FALSE;
90 
91  syslogLevel = getSyslogLevel(message->Level);
92  if (syslogLevel >= 0)
93  syslog(syslogLevel, "skipped data message of %" PRIuz " bytes", message->Length);
94 
95  return TRUE;
96 }
97 
98 static BOOL WLog_SyslogAppender_WriteImageMessage(wLog* log, wLogAppender* appender,
99  wLogMessage* message)
100 {
101  int syslogLevel = 0;
102 
103  if (!log || !appender || !message)
104  return FALSE;
105 
106  syslogLevel = getSyslogLevel(message->Level);
107  if (syslogLevel >= 0)
108  syslog(syslogLevel, "skipped image (%" PRIuz "x%" PRIuz "x%" PRIuz ")", message->ImageWidth,
109  message->ImageHeight, message->ImageBpp);
110 
111  return TRUE;
112 }
113 
114 static void WLog_SyslogAppender_Free(wLogAppender* appender)
115 {
116  free(appender);
117 }
118 
119 wLogAppender* WLog_SyslogAppender_New(wLog* log)
120 {
121  wLogSyslogAppender* appender = NULL;
122 
123  appender = (wLogSyslogAppender*)calloc(1, sizeof(wLogSyslogAppender));
124  if (!appender)
125  return NULL;
126 
127  appender->Type = WLOG_APPENDER_SYSLOG;
128 
129  appender->Open = WLog_SyslogAppender_Open;
130  appender->Close = WLog_SyslogAppender_Close;
131  appender->WriteMessage = WLog_SyslogAppender_WriteMessage;
132  appender->WriteDataMessage = WLog_SyslogAppender_WriteDataMessage;
133  appender->WriteImageMessage = WLog_SyslogAppender_WriteImageMessage;
134  appender->Free = WLog_SyslogAppender_Free;
135 
136  return (wLogAppender*)appender;
137 }