EtcPal  0.4.1
ETC Platform Abstraction Layer (EtcPal)
View other versions:
log.h
1 /******************************************************************************
2  * Copyright 2022 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of EtcPal. For more information, go to:
17  * https://github.com/ETCLabs/EtcPal
18  ******************************************************************************/
19 
20 /* etcpal/log.h: Common logging functions. */
21 
22 #ifndef ETCPAL_LOG_H_
23 #define ETCPAL_LOG_H_
24 
25 #include <limits.h>
26 #include <stdarg.h>
27 #include <stdbool.h>
28 #include <stddef.h>
29 #include "etcpal/common.h"
30 
134 /* clang-format off */
135 
140 #define ETCPAL_LOG_KERN (0 << 3)
141 #define ETCPAL_LOG_USER (1 << 3)
142 #define ETCPAL_LOG_MAIL (2 << 3)
143 #define ETCPAL_LOG_DAEMON (3 << 3)
144 #define ETCPAL_LOG_AUTH (4 << 3)
145 #define ETCPAL_LOG_SYSLOG (5 << 3)
146 #define ETCPAL_LOG_LPR (6 << 3)
147 #define ETCPAL_LOG_NEWS (7 << 3)
148 #define ETCPAL_LOG_UUCP (8 << 3)
149 #define ETCPAL_LOG_CRON (9 << 3)
150 #define ETCPAL_LOG_AUTHPRIV (10 << 3)
151 #define ETCPAL_LOG_FTP (11 << 3)
153 #define ETCPAL_LOG_LOCAL0 (16 << 3)
154 #define ETCPAL_LOG_LOCAL1 (17 << 3)
155 #define ETCPAL_LOG_LOCAL2 (18 << 3)
156 #define ETCPAL_LOG_LOCAL3 (19 << 3)
157 #define ETCPAL_LOG_LOCAL4 (20 << 3)
158 #define ETCPAL_LOG_LOCAL5 (21 << 3)
159 #define ETCPAL_LOG_LOCAL6 (22 << 3)
160 #define ETCPAL_LOG_LOCAL7 (23 << 3)
165 #define ETCPAL_LOG_NFACILITIES 24
167 #define ETCPAL_LOG_FACMASK 0x03f8 /* Mask off the facility part of a prival */
169 #define ETCPAL_LOG_FAC(p) (((p) & ETCPAL_LOG_FACMASK) >> 3)
175 #define ETCPAL_LOG_EMERG 0
176 #define ETCPAL_LOG_ALERT 1
177 #define ETCPAL_LOG_CRIT 2
178 #define ETCPAL_LOG_ERR 3
179 #define ETCPAL_LOG_WARNING 4
180 #define ETCPAL_LOG_NOTICE 5
181 #define ETCPAL_LOG_INFO 6
182 #define ETCPAL_LOG_DEBUG 7
188 #define ETCPAL_LOG_PRIMASK 0x07 /* mask off the priority part of a prival */
190 #define ETCPAL_LOG_PRI(p) ((p) & ETCPAL_LOG_PRIMASK)
191 #define ETCPAL_LOG_MAKEPRI(fac, pri) (((fac) << 3) | (pri))
193 #define ETCPAL_LOG_MASK(pri) (1 << (pri))
194 #define ETCPAL_LOG_UPTO(pri) ((1 << ((pri) + 1)) - 1)
196 #define ETCPAL_LOG_HOSTNAME_MAX_LEN 256u
197 #define ETCPAL_LOG_APP_NAME_MAX_LEN 49u
198 #define ETCPAL_LOG_PROCID_MAX_LEN 129u
201 #define ETCPAL_RAW_LOG_MSG_MAX_LEN 480u
202 
203 /* clang-format on */
204 
205 /*
206  * Timestamp length:
207  * Date: 10 (1970-01-01)
208  * "T" or " ": 1
209  * Time: 12 (00:00:00.001)
210  * UTC Offset: 6 (-05:00)
211  * Nullterm: 1
212  * --------------
213  * Total: 30
214  */
216 #define ETCPAL_LOG_TIMESTAMP_LEN 30u
217 
218 /*
219  * Syslog header length (from RFC 5424):
220  * PRIVAL: 5
221  * VERSION: 3
222  * SP: 1
223  * TIMESTAMP: [Referenced]
224  * SP: 1
225  * HOSTNAME: [Referenced]
226  * SP: 1
227  * APP-NAME: [Referenced]
228  * SP: 1
229  * PROCID: [Referenced]
230  * SP: 1
231  * MSGID (not used): 1
232  * SP: 1
233  * STRUCTURED-DATA (not used): 1
234  * SP: 1
235  * -----------------------------
236  * Total non-referenced: 17
237  */
239 #define ETCPAL_SYSLOG_HEADER_MAX_LEN \
240  (17u + (ETCPAL_LOG_TIMESTAMP_LEN - 1u) + (ETCPAL_LOG_HOSTNAME_MAX_LEN - 1u) + (ETCPAL_LOG_APP_NAME_MAX_LEN - 1u) + \
241  (ETCPAL_LOG_PROCID_MAX_LEN - 1u))
242 
244 #define ETCPAL_SYSLOG_STR_MIN_LEN ETCPAL_SYSLOG_HEADER_MAX_LEN
246 #define ETCPAL_LOG_STR_MIN_LEN (ETCPAL_LOG_TIMESTAMP_LEN + 1u /*SP*/ + 6u /*pri*/ + 1u /*SP*/)
247 
249 #define ETCPAL_SYSLOG_STR_MAX_LEN (ETCPAL_SYSLOG_HEADER_MAX_LEN + ETCPAL_RAW_LOG_MSG_MAX_LEN)
250 
251 /*
252  * Human-reaadable log string max length:
253  * Timestamp: [Referenced]
254  * Space: 1
255  * Priority: 6 ([CRIT])
256  * Space: 1
257  * Message length: [Referenced]
258  * ----------------------------
259  * Total non-referenced: 8
260  */
265 #define ETCPAL_LOG_STR_MAX_LEN (8u + (ETCPAL_LOG_TIMESTAMP_LEN - 1u) + ETCPAL_RAW_LOG_MSG_MAX_LEN)
266 
271 typedef struct EtcPalLogTimestamp
272 {
273  unsigned int year;
274  unsigned int month;
275  unsigned int day;
276  unsigned int hour;
277  unsigned int minute;
278  unsigned int second;
279  unsigned int msec;
282 
287 typedef struct EtcPalLogStrings
288 {
290  const char* syslog;
292  const char* legacy_syslog;
294  const char* human_readable;
299  const char* raw;
304  int priority;
306 
326 typedef void (*EtcPalLogCallback)(void* context, const EtcPalLogStrings* strings);
327 
337 typedef void (*EtcPalLogTimeFn)(void* context, EtcPalLogTimestamp* timestamp);
338 
340 #define ETCPAL_LOG_CREATE_HUMAN_READABLE 0x01
342 #define ETCPAL_LOG_CREATE_SYSLOG 0x02
344 #define ETCPAL_LOG_CREATE_LEGACY_SYSLOG 0x04
345 
347 typedef struct EtcPalSyslogParams
348 {
350  int facility;
352  char hostname[ETCPAL_LOG_HOSTNAME_MAX_LEN]; // NOLINT(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
354  char app_name[ETCPAL_LOG_APP_NAME_MAX_LEN]; // NOLINT(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
356  char procid[ETCPAL_LOG_PROCID_MAX_LEN]; // NOLINT(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
358 
368 #define ETCPAL_SYSLOG_PARAMS_INIT \
369  { \
370  0, {'\0'}, {'\0'}, { '\0' } \
371  }
372 
374 typedef struct EtcPalLogParams
375 {
377  int action;
383  int log_mask;
390  void* context;
392 
402 #define ETCPAL_LOG_PARAMS_INIT \
403  { \
404  0, NULL, ETCPAL_SYSLOG_PARAMS_INIT, 0, NULL, NULL \
405  }
406 
407 #ifdef __cplusplus
408 extern "C" {
409 #endif
410 
411 /*
412  * The various compiler ifdefs are to use each compiler's method of checking printf-style
413  * arguments.
414  */
415 
416 #ifdef __ICCARM__
417 #pragma __printf_args
418 #endif
419 bool etcpal_create_log_str(char* buf,
420  size_t buflen,
421  const EtcPalLogTimestamp* timestamp,
422  int pri,
423  const char* format,
424  ...)
425 #ifdef __GNUC__
426  __attribute__((__format__(__printf__, 5, 6)))
427 #endif
428  ;
429 
430 bool etcpal_vcreate_log_str(char* buf,
431  size_t buflen,
432  const EtcPalLogTimestamp* timestamp,
433  int pri,
434  const char* format,
435  va_list args);
436 
437 #ifdef __ICCARM__
438 #pragma __printf_args
439 #endif
440 bool etcpal_create_syslog_str(char* buf,
441  size_t buflen,
442  const EtcPalLogTimestamp* timestamp,
443  const EtcPalSyslogParams* syslog_params,
444  int pri,
445  const char* format,
446  ...)
447 #ifdef __GNUC__
448  __attribute__((__format__(__printf__, 6, 7)))
449 #endif
450  ;
451 
452 bool etcpal_vcreate_syslog_str(char* buf,
453  size_t buflen,
454  const EtcPalLogTimestamp* timestamp,
455  const EtcPalSyslogParams* syslog_params,
456  int pri,
457  const char* format,
458  va_list args);
459 
460 #ifdef __ICCARM__
461 #pragma __printf_args
462 #endif
463 bool etcpal_create_legacy_syslog_str(char* buf,
464  size_t buflen,
465  const EtcPalLogTimestamp* timestamp,
466  const EtcPalSyslogParams* syslog_params,
467  int pri,
468  const char* format,
469  ...)
470 #ifdef __GNUC__
471  __attribute__((__format__(__printf__, 6, 7)))
472 #endif
473  ;
474 
475 bool etcpal_vcreate_legacy_syslog_str(char* buf,
476  size_t buflen,
477  const EtcPalLogTimestamp* timestamp,
478  const EtcPalSyslogParams* syslog_params,
479  int pri,
480  const char* format,
481  va_list args);
482 
486 
487 bool etcpal_can_log(const EtcPalLogParams* params, int pri);
488 
489 #ifdef __ICCARM__
490 #pragma __printf_args
491 #endif
492 void etcpal_log(const EtcPalLogParams* params, int pri, const char* format, ...)
493 #ifdef __GNUC__
494  __attribute__((__format__(__printf__, 3, 4)))
495 #endif
496  ;
497 
498 void etcpal_vlog(const EtcPalLogParams* params, int pri, const char* format, va_list args);
499 
500 #ifdef __cplusplus
501 }
502 #endif
503 
508 #endif /* ETCPAL_LOG_H_ */
#define ETCPAL_LOG_PROCID_MAX_LEN
Max length of the procid param.
Definition: log.h:198
struct EtcPalLogStrings EtcPalLogStrings
The set of log strings passed with a call to an etcpal_log_callback function.
#define ETCPAL_LOG_APP_NAME_MAX_LEN
Max length of the app_name param.
Definition: log.h:197
void etcpal_vlog(const EtcPalLogParams *params, int pri, const char *format, va_list args)
Log a message with the list of format arguments already generated.
Definition: log.c:424
bool etcpal_vcreate_legacy_syslog_str(char *buf, size_t buflen, const EtcPalLogTimestamp *timestamp, const EtcPalSyslogParams *syslog_params, int pri, const char *format, va_list args)
Create a log a message with RFC 3164 syslog header in the given buffer.
Definition: log.c:307
struct EtcPalLogParams EtcPalLogParams
A set of parameters used for the etcpal_*log() functions.
bool etcpal_create_legacy_syslog_str(char *buf, size_t buflen, const EtcPalLogTimestamp *timestamp, const EtcPalSyslogParams *syslog_params, int pri, const char *format,...)
Create a log message with RFC 3164 syslog header in the given buffer.
Definition: log.c:275
struct EtcPalSyslogParams EtcPalSyslogParams
A set of parameters for the syslog header.
void etcpal_sanitize_syslog_params(EtcPalSyslogParams *params)
Ensure that the given syslog parameters are compliant with the syslog RFC (modifying them if necessar...
Definition: log.c:328
bool etcpal_validate_log_timestamp(const EtcPalLogTimestamp *timestamp)
Determine whether the given EtcPalLogTimestamp is valid.
Definition: log.c:369
struct EtcPalLogTimestamp EtcPalLogTimestamp
A set of parameters which represent the current local time with millisecond resolution.
bool etcpal_vcreate_syslog_str(char *buf, size_t buflen, const EtcPalLogTimestamp *timestamp, const EtcPalSyslogParams *syslog_params, int pri, const char *format, va_list args)
Create a log a message with RFC 5424 syslog header in the given buffer.
Definition: log.c:250
bool etcpal_validate_log_params(EtcPalLogParams *params)
Ensure that the given EtcPalLogParams are valid.
Definition: log.c:347
bool etcpal_create_syslog_str(char *buf, size_t buflen, const EtcPalLogTimestamp *timestamp, const EtcPalSyslogParams *syslog_params, int pri, const char *format,...)
Create a log message with RFC 5424 syslog header in the given buffer.
Definition: log.c:218
void etcpal_log(const EtcPalLogParams *params, int pri, const char *format,...)
Log a message.
Definition: log.c:405
bool etcpal_can_log(const EtcPalLogParams *params, int pri)
Determine whether a priority level can be logged given the mask present in the log params.
Definition: log.c:388
bool etcpal_create_log_str(char *buf, size_t buflen, const EtcPalLogTimestamp *timestamp, int pri, const char *format,...)
Create a log message with a human-readable prefix in the given buffer.
Definition: log.c:164
bool etcpal_vcreate_log_str(char *buf, size_t buflen, const EtcPalLogTimestamp *timestamp, int pri, const char *format, va_list args)
Create a log a message with a human-readable prefix in the given buffer.
Definition: log.c:194
void(* EtcPalLogTimeFn)(void *context, EtcPalLogTimestamp *timestamp)
Time callback function.
Definition: log.h:337
void(* EtcPalLogCallback)(void *context, const EtcPalLogStrings *strings)
Log callback function.
Definition: log.h:326
#define ETCPAL_LOG_HOSTNAME_MAX_LEN
Max length of the hostname param.
Definition: log.h:196
A set of parameters used for the etcpal_*log() functions.
Definition: log.h:375
EtcPalSyslogParams syslog_params
The syslog header parameters.
Definition: log.h:381
EtcPalLogCallback log_fn
A callback function for the finished log string(s).
Definition: log.h:379
int action
What should be done when etcpal_log() or etcpal_vlog() is called.
Definition: log.h:377
int log_mask
A mask value that determines which priority messages can be logged.
Definition: log.h:383
EtcPalLogTimeFn time_fn
A callback function for the etcpal_log() and etcpal_vlog() functions to obtain the time from the appl...
Definition: log.h:388
void * context
Application context that will be passed back with the log callback function.
Definition: log.h:390
The set of log strings passed with a call to an etcpal_log_callback function.
Definition: log.h:288
const char * human_readable
Log string formatted for readability per ETC convention.
Definition: log.h:294
const char * syslog
Log string formatted compliant to RFC 5424.
Definition: log.h:290
const char * legacy_syslog
Log string formatted compliant to RFC 3164.
Definition: log.h:292
const char * raw
The original log string that was passed to etcpal_log() or etcpal_vlog().
Definition: log.h:299
int priority
The original log priority that was passed to etcpal_log() or etcpal_vlog().
Definition: log.h:304
A set of parameters which represent the current local time with millisecond resolution.
Definition: log.h:272
int utc_offset
The local offset from UTC in minutes.
Definition: log.h:280
unsigned int day
Day of the month.
Definition: log.h:275
unsigned int second
Seconds past the current minute.
Definition: log.h:278
unsigned int minute
Minutes past the current hour.
Definition: log.h:277
unsigned int month
Month of the year.
Definition: log.h:274
unsigned int year
Absolute year.
Definition: log.h:273
unsigned int hour
Hours since midnight.
Definition: log.h:276
unsigned int msec
Milliseconds past the current second.
Definition: log.h:279
A set of parameters for the syslog header.
Definition: log.h:348
int facility
Syslog Facility; see RFC 5424 § 6.2.1.
Definition: log.h:350
char app_name[ETCPAL_LOG_APP_NAME_MAX_LEN]
Syslog APP-NAME; see RFC 5424 § 6.2.5.
Definition: log.h:354
char procid[ETCPAL_LOG_PROCID_MAX_LEN]
Syslog PROCID; see RFC 5424 § 6.2.6.
Definition: log.h:356
char hostname[ETCPAL_LOG_HOSTNAME_MAX_LEN]
Syslog HOSTNAME; see RFC 5424 § 6.2.4.
Definition: log.h:352