log_util.c
Go to the documentation of this file.
1 /*!**********************************************************************
2  *
3  * @copyright Copyright (C) 2016 Siemens Aktiengesellschaft.\n
4  * All rights reserved.
5  *
6  *************************************************************************
7  *
8  * @file log_util.c
9  * @date Jun 27, 2016
10  * @brief Log utility module implementation file.
11  *
12  ************************************************************************/
13 
14 #include "mcl/mcl_config_setup.h"
15 #include "log_util.h"
16 #include "mcl/mcl_log_util.h"
17 #include "file_util.h"
18 #include "definitions.h"
19 #include "memory.h"
20 
21 #include <stdio.h>
22 FILE *log_file = NULL;
23 
24 #if HAVE_SYSLOG_H_
25 #include <syslog.h>
26 #endif
27 
28 #define LOG_UTIL_MCL_SYSLOG "mcl_syslog"
29 
31 
32 // log_output_global is set to E_LOG_OUTPUT_STDERR as default.
34 
36 {
37  "MCL_OK",
38  "MCL_FAIL",
39  "MCL_TRIGGERED_WITH_NULL",
40  "MCL_OUT_OF_MEMORY",
41  "MCL_INVALID_PARAMETER",
42  "MCL_NO_FILE_SUPPORT",
43  "MCL_NO_STORAGE_MEDIUM",
44  "MCL_INVALID_LOG_LEVEL",
45  "MCL_OPERATION_IS_NOT_SUPPORTED",
46  "MCL_FILE_CANNOT_BE_OPENED",
47  "MCL_JSON_NAME_DUPLICATION",
48  "MCL_INDEX_OUT_OF_RANGE",
49  "MCL_ITEM_ALREADY_EXISTS",
50  "MCL_INVALID_EVENT_SEVERITY_LEVEL",
51  "MCL_NO_SERVER_TIME",
52  "MCL_NO_ACCESS_TOKEN_EXISTS",
53  "MCL_INVALID_HOST_NAME",
54  "MCL_INVALID_CERTIFICATE",
55  "MCL_INVALID_PROXY_HOST_NAME",
56  "MCL_INVALID_PROXY_USER_NAME",
57  "MCL_INVALID_PROXY_PASSWORD",
58  "MCL_INVALID_PROXY_DOMAIN",
59  "MCL_INVALID_SECURITY_PROFILE",
60  "MCL_INVALID_USER_AGENT",
61  "MCL_INVALID_TENANT",
62  "MCL_INVALID_MAX_HTTP_PAYLOAD_SIZE",
63  "MCL_NO_ACCESS_TOKEN_PROVIDED",
64  "MCL_PATH_NOT_ACCESSIBLE",
65  "MCL_COULD_NOT_RESOLVE_PROXY",
66  "MCL_COULD_NOT_RESOLVE_HOST",
67  "MCL_COULD_NOT_CONNECT",
68  "MCL_SSL_HANDSHAKE_FAIL",
69  "MCL_NETWORK_SEND_FAIL",
70  "MCL_NETWORK_RECEIVE_FAIL",
71  "MCL_SERVER_CERTIFICATE_NOT_VERIFIED",
72  "MCL_IMPROPER_CERTIFICATE",
73  "MCL_REQUEST_TIMEOUT",
74  "MCL_SERVER_FAIL",
75  "MCL_CREATED",
76  "MCL_PARTIAL_CONTENT",
77  "MCL_BAD_REQUEST",
78  "MCL_UNAUTHORIZED",
79  "MCL_FORBIDDEN",
80  "MCL_NOT_FOUND",
81  "MCL_CONFLICT",
82  "MCL_PRECONDITION_FAIL",
83  "MCL_REQUEST_PAYLOAD_TOO_LARGE",
84  "MCL_TOO_MANY_REQUESTS",
85  "MCL_UNEXPECTED_RESULT_CODE",
86  "MCL_NOT_INITIALIZED",
87  "MCL_NOT_ONBOARDED",
88  "MCL_ALREADY_ONBOARDED",
89  "MCL_STORE_IS_EMPTY",
90  "MCL_EXCHANGE_STREAMING_IS_ACTIVE",
91  "MCL_SECURITY_UP_TO_DATE",
92  "MCL_CANNOT_ENTER_CRITICAL_SECTION",
93  "MCL_INITIALIZATION_FAIL",
94  "MCL_STORE_ITEM_EXCEEDS_MAX_HTTP_REQUEST_SIZE",
95  "MCL_UNDEFINED_ENDPOINT",
96  "MCL_BAD_CONTENT_ENCODING",
97  "MCL_NON_EXISTING_JSON_CHILD",
98  "MCL_SHA256_CALCULATION_FAIL",
99  "MCL_SERVER_PROOF_FAIL",
100  "MCL_REGISTRATION_INFO_IS_NOT_SAVED",
101  "MCL_REGISTRATION_INFO_IS_NOT_LOADED",
102  "MCL_BUFFER_OVERFLOW_ERROR",
103  "MCL_HTTP_REQUEST_NO_MORE_SPACE",
104  "MCL_LIMIT_EXCEEDED",
105  "MCL_ARRAY_IS_EMPTY",
106  "MCL_PARTIALLY_WRITTEN",
107  "MCL_HTTP_REQUEST_FINALIZE_FAILED"
108 };
109 
110 #if (1 == HAVE_SYSLOG_H_)
111 static int log_util_convert_to_syslog_level(const int lvl)
112 {
113  switch (lvl)
114  {
115  case ZF_LOG_VERBOSE :
116  return LOG_DEBUG;
117  case ZF_LOG_DEBUG :
118  return LOG_DEBUG;
119  case ZF_LOG_INFO :
120  return LOG_INFO;
121  case ZF_LOG_WARN :
122  return LOG_WARNING;
123  case ZF_LOG_ERROR :
124  return LOG_ERR;
125  case ZF_LOG_FATAL :
126  return LOG_EMERG;
127  default :
128  return LOG_EMERG;
129  }
130 }
131 #endif
132 
133 static void log_util_default_callback(const zf_log_message * const message, void *user_context)
134 {
135  switch (log_output_global)
136  {
137  case E_LOG_OUTPUT_FILE :
138  if (MCL_NULL != log_file)
139  {
140  // Terminate message with new line
141  *message->p = '\n';
142 
143  file_util_fwrite_without_log(message->buf, message->p - message->buf + 1, 1, log_file);
144 
146  }
147 
148  break;
149  case E_LOG_OUTPUT_CALLBACK :
150  if (MCL_NULL != user_callback)
151  {
152  user_callback(message->lvl, message->tag, message->buf, user_context);
153  }
154 
155  break;
156  case E_LOG_OUTPUT_SYSLOG :
157 
158  #if (1 == HAVE_SYSLOG_H_)
159 
160  // Terminate message string
161  *message->p = '\0';
162 
163  syslog(log_util_convert_to_syslog_level(message->lvl), "%s", message->buf);
164  #endif
165 
166  break;
167  case E_LOG_OUTPUT_STDERR :
168  zf_log_out_stderr_callback(message, user_context);
169 
170  break;
171  }
172 }
173 
175 {
176  if (((LOG_UTIL_LEVEL_VERBOSE <= log_level) && (log_level <= LOG_UTIL_LEVEL_FATAL)) || (LOG_UTIL_LEVEL_NONE == log_level))
177  {
178  zf_log_set_output_level(log_level);
179  return MCL_OK;
180  }
181 
182  return MCL_INVALID_LOG_LEVEL;
183 }
184 
186 {
187  va_list valist;
188  void *user_context = MCL_NULL;
189 
190  log_output_global = log_output;
191 
192  va_start(valist, log_output);
193 
195  {
196  // Get file name as variable argument.
197  if (MCL_OK != file_util_fopen_without_log(va_arg(valist, char*), "w", (void **)&log_file))
198  {
199  log_file = NULL;
200  va_end(valist);
202  }
203  }
205  {
206  #if (1 == HAVE_SYSLOG_H_)
207  openlog(LOG_UTIL_MCL_SYSLOG, LOG_PERROR, LOG_USER);
208  #else
209  va_end(valist);
210  return MCL_INVALID_PARAMETER;
211  #endif
212  }
214  {
215  user_callback = (mcl_log_util_callback_t)va_arg(valist, void*);
216  user_context = va_arg(valist, void*);
217  }
218  else
219  {
220  va_end(valist);
221  return MCL_INVALID_PARAMETER;
222  }
223 
224  zf_log_set_output_v(ZF_LOG_PUT_STD, user_context, log_util_default_callback);
225 
226  va_end(valist);
227  return MCL_OK;
228 }
229 
231 {
233  {
235  }
237  {
239  }
241  {
242  #if (1 == HAVE_SYSLOG_H_)
243  closelog();
244  #endif
245  }
246 
248 }
249 
251 {
252  DEBUG_ENTRY("E_MCL_ERROR_CODE error_code = <%d>", error_code)
253 
254  const char *error_string = MCL_NULL;
255  if(MCL_OK <= error_code && error_code < MCL_ERROR_CODE_END)
256  {
257  error_string = error_code_strings[error_code];
258  }
259  else
260  {
261  MCL_ERROR("Given code is invalid.");
262  }
263 
264  DEBUG_LEAVE("retVal = <%p>", error_string);
265  return error_string;
266 }
E_MCL_ERROR_CODE file_util_fflush_without_log(void *file_descriptor)
Definition: file_util.c:192
Memory module header file.
#define DEBUG_LEAVE(...)
Definition: log_util.h:81
E_MCL_ERROR_CODE mcl_log_util_initialize(E_LOG_OUTPUT log_output,...)
This function initializes logging where output channel is set and configured via variable arguments...
Definition: log_util.c:185
#define DEBUG_ENTRY(...)
Definition: log_util.h:80
Log output channel is stderr.
Definition: mcl_log_util.h:35
E_MCL_ERROR_CODE file_util_fopen_without_log(const char *file_name, const char *mode, void **file_descriptor)
Definition: file_util.c:32
Given log level is invalid.
Definition: mcl_common.h:147
Log output channel is file.
Definition: mcl_log_util.h:32
Log utility module header file.
E_MCL_ERROR_CODE
MCL Error code definitions. Every function returning an error code uses this enum values...
Definition: mcl_common.h:137
End of error codes.
Definition: mcl_common.h:221
const char * error_code_strings[MCL_ERROR_CODE_END]
Definition: log_util.c:35
Configuration setup module interface header file.
void mcl_log_util_finalize(void)
Performs finalizing operations for logging.
Definition: log_util.c:230
void(* mcl_log_util_callback_t)(int log_level, const char *tag, const char *message, void *user_context)
Log util callback function definition.
Definition: mcl_log_util.h:46
E_MCL_ERROR_CODE mcl_log_util_set_output_level(const int log_level)
Sets the global output level. The output level is used during runtime check. No logs are written if g...
Definition: log_util.c:174
E_MCL_ERROR_CODE file_util_fwrite_without_log(const void *data, mcl_size_t size, mcl_size_t count, void *file_descriptor)
Definition: file_util.c:88
General invalid parameter fail.
Definition: mcl_common.h:144
const char * mcl_log_util_convert_error_code_to_string(E_MCL_ERROR_CODE error_code)
This function converts the given error code to its string value.
Definition: log_util.c:250
#define LOG_UTIL_LEVEL_VERBOSE
Definition: log_util.h:47
static void log_util_default_callback(const zf_log_message *const message, void *user_context)
Definition: log_util.c:133
FILE * log_file
Definition: log_util.c:22
Definitions module header file.
File utility module header file.
#define LOG_UTIL_LEVEL_NONE
Definition: log_util.h:53
File can not be opened.
Definition: mcl_common.h:149
#define LOG_UTIL_LEVEL_FATAL
Definition: log_util.h:52
Success.
Definition: mcl_common.h:140
Log output channel is callback.
Definition: mcl_log_util.h:33
No log output chosen.
Definition: mcl_log_util.h:36
Log utility module interface header file.
#define MCL_ERROR(...)
Definition: log_util.h:97
E_LOG_OUTPUT log_output_global
Definition: log_util.c:33
#define MCL_NULL
Definition: definitions.h:24
#define LOG_UTIL_MCL_SYSLOG
Definition: log_util.c:28
E_MCL_ERROR_CODE file_util_fclose_without_log(void *file_descriptor)
Definition: file_util.c:62
Log output channel is syslog.
Definition: mcl_log_util.h:34
mcl_log_util_callback_t user_callback
Definition: log_util.c:30
E_LOG_OUTPUT
MCL Log output definitions.
Definition: mcl_log_util.h:30