lwpa  0.1.0
LightWeight Platform Abstraction (lwpa)
View other versions:
lwpa_log.h
1 /******************************************************************************
2  * Copyright 2018 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 lwpa. For more information, go to:
17  * https://github.com/ETCLabs/lwpa
18  ******************************************************************************/
19 
20 /* lwpa_log.h: Common logging functions. */
21 #ifndef _LWPA_LOG_H_
22 #define _LWPA_LOG_H_
23 
24 #include <stdarg.h>
25 #include <stddef.h>
26 #include "lwpa_bool.h"
27 
51 /* clang-format off */
52 
56 #define LWPA_LOG_KERN (0 << 3)
57 #define LWPA_LOG_USER (1 << 3)
58 #define LWPA_LOG_MAIL (2 << 3)
59 #define LWPA_LOG_DAEMON (3 << 3)
60 #define LWPA_LOG_AUTH (4 << 3)
61 #define LWPA_LOG_SYSLOG (5 << 3)
63 #define LWPA_LOG_LPR (6 << 3)
64 #define LWPA_LOG_NEWS (7 << 3)
65 #define LWPA_LOG_UUCP (8 << 3)
66 #define LWPA_LOG_CRON (9 << 3)
67 #define LWPA_LOG_AUTHPRIV (10 << 3)
68 #define LWPA_LOG_FTP (11 << 3)
70 #define LWPA_LOG_LOCAL0 (16 << 3)
71 #define LWPA_LOG_LOCAL1 (17 << 3)
72 #define LWPA_LOG_LOCAL2 (18 << 3)
73 #define LWPA_LOG_LOCAL3 (19 << 3)
74 #define LWPA_LOG_LOCAL4 (20 << 3)
75 #define LWPA_LOG_LOCAL5 (21 << 3)
76 #define LWPA_LOG_LOCAL6 (22 << 3)
77 #define LWPA_LOG_LOCAL7 (23 << 3)
80 #define LWPA_LOG_NFACILITIES 24
81 #define LWPA_LOG_FACMASK 0x03f8 /* Mask to extract facility part of a
82  prival. */
83 #define LWPA_LOG_FAC(p) (((p)&LWPA_LOG_FACMASK) >> 3)
84 
88 #define LWPA_LOG_EMERG 0
89 #define LWPA_LOG_ALERT 1
90 #define LWPA_LOG_CRIT 2
91 #define LWPA_LOG_ERR 3
92 #define LWPA_LOG_WARNING 4
93 #define LWPA_LOG_NOTICE 5
94 #define LWPA_LOG_INFO 6
95 #define LWPA_LOG_DEBUG 7
98 #define LWPA_LOG_PRIMASK 0x07 /* mask to extract priority part (internal) */
99 #define LWPA_LOG_PRI(p) ((p)&LWPA_LOG_PRIMASK) /* extract priority */
100 #define LWPA_LOG_MAKEPRI(fac, pri) (((fac) << 3) | (pri))
101 
102 #define LWPA_LOG_MASK(pri) (1 << (pri)) /* mask for one priority */
103 #define LWPA_LOG_UPTO(pri) \
104  ((1 << ((pri) + 1)) - 1) /* all priorities through pri */
105 
106 #define LWPA_LOG_HOSTNAME_MAX_LEN 256
107 #define LWPA_LOG_APP_NAME_MAX_LEN 49
108 #define LWPA_LOG_PROCID_MAX_LEN 129
111 #define LWPA_LOG_MSG_MAX_LEN 480
112 
113 /* clang-format on */
114 
117 #define LWPA_LOG_TIMESTAMP_LEN (10 /*Date*/ + 1 /*T*/ + 12 /*Time*/ + 6 /*Offset*/ + 1 /*Nullterm*/)
118 
120 #define LWPA_SYSLOG_HEADER_MAX_LEN \
121  (5 /*PRIVAL*/ + 3 /*Version*/ + 1 /*SP*/ + (LWPA_LOG_TIMESTAMP_LEN - 1) /*Timestamp*/ + 1 /*SP*/ + \
122  (LWPA_LOG_HOSTNAME_MAX_LEN - 1) + 1 /*SP*/ + (LWPA_LOG_APP_NAME_MAX_LEN - 1) + 1 /*SP*/ + \
123  (LWPA_LOG_PROCID_MAX_LEN - 1) + 1 /*SP*/ + 1 /*MSGID*/ + 1 /*SP*/ + 1 /*STRUCTURED-DATA*/ + 1 /*SP*/)
124 
126 #define LWPA_SYSLOG_STR_MIN_LEN LWPA_SYSLOG_HEADER_MAX_LEN
128 #define LWPA_HUMAN_LOG_STR_MIN_LEN LWPA_LOG_TIMESTAMP_LEN
129 
132 #define LWPA_SYSLOG_STR_MAX_LEN (LWPA_SYSLOG_HEADER_MAX_LEN + LWPA_LOG_MSG_MAX_LEN)
133 
136 #define LWPA_HUMAN_LOG_STR_MAX_LEN ((LWPA_LOG_TIMESTAMP_LEN - 1) + 1 /*SP*/ + LWPA_LOG_MSG_MAX_LEN)
137 
139 typedef struct LwpaLogTimeParams
140 {
142  int year;
144  int month;
146  int day;
148  int hour;
150  int minute;
152  int second;
154  int msec;
158 
172 typedef void (*lwpa_log_callback)(void *context, const char *syslog_str, const char *human_str, const char *raw_str);
173 
182 typedef void (*lwpa_log_time_fn)(void *context, LwpaLogTimeParams *time_params);
183 
185 typedef enum
186 {
197 
199 typedef struct LwpaSyslogParams
200 {
202  int facility;
210 
212 typedef struct LwpaLogParams
213 {
221  int log_mask;
226  void *context;
227 } LwpaLogParams;
228 
229 #define lwpa_setlogmask(logparamsptr, newlogmask) ((logparamsptr)->log_mask = newlogmask)
230 
231 #define lwpa_canlog(logparamsptr, pri) ((LWPA_LOG_MASK(pri) & (logparamsptr)->log_mask) != 0)
232 
233 #ifdef __cplusplus
234 extern "C" {
235 #endif
236 
237 /* The various compiler ifdefs are to use each compiler's method of checking printf-style
238  * arguments. */
239 
240 #ifdef __ICCARM__
241 #pragma __printf_args
242 #endif
243 bool lwpa_create_syslog_str(char *buf, size_t buflen, const LwpaLogTimeParams *time,
244  const LwpaSyslogParams *syslog_params, int pri, const char *format, ...)
245 #ifdef __GNUC__
246  __attribute__((__format__(__printf__, 6, 7)))
247 #endif
248  ;
249 
250 #ifdef __ICCARM__
251 #pragma __printf_args
252 #endif
253 bool lwpa_create_human_log_str(char *buf, size_t buflen, const LwpaLogTimeParams *time, const char *format, ...)
254 #ifdef __GNUC__
255  __attribute__((__format__(__printf__, 4, 5)))
256 #endif
257  ;
258 
261 
262 #ifdef __ICCARM__
263 #pragma __printf_args
264 #endif
265 void lwpa_log(const LwpaLogParams *params, int pri, const char *format, ...)
266 #ifdef __GNUC__
267  __attribute__((__format__(__printf__, 3, 4)))
268 #endif
269  ;
270 
271 void lwpa_vlog(const LwpaLogParams *params, int pri, const char *format, va_list args);
272 
273 #ifdef __cplusplus
274 }
275 #endif
276 
279 #endif /* _LWPA_LOG_H_ */
void lwpa_sanitize_syslog_params(LwpaSyslogParams *params)
Ensure that the given syslog parameters are compliant with the syslog RFC (modifying them if necessar...
Definition: lwpa_log.c:63
bool lwpa_create_syslog_str(char *buf, size_t buflen, const LwpaLogTimeParams *time, const LwpaSyslogParams *syslog_params, int pri, const char *format,...)
Create a log message with syslog header in the given buffer.
Definition: lwpa_log.c:199
struct LwpaLogParams LwpaLogParams
A set of parameters used for the lwpa_*log() functions.
void lwpa_vlog(const LwpaLogParams *params, int pri, const char *format, va_list args)
Log a message from a library module with the list of format arguments already generated.
Definition: lwpa_log.c:285
struct LwpaLogTimeParams LwpaLogTimeParams
A set of parameters which represent the current local time with millisecond resolution.
void lwpa_log(const LwpaLogParams *params, int pri, const char *format,...)
Log a message from a library module.
Definition: lwpa_log.c:267
void(* lwpa_log_time_fn)(void *context, LwpaLogTimeParams *time_params)
Time callback function.
Definition: lwpa_log.h:181
bool lwpa_validate_log_params(LwpaLogParams *params)
Ensure that the given lwpa_log_params are valid.
Definition: lwpa_log.c:81
lwpa_log_action_t
Which types of log message(s) the lwpa_log() and lwpa_vlog() functions create.
Definition: lwpa_log.h:185
bool lwpa_create_human_log_str(char *buf, size_t buflen, const LwpaLogTimeParams *time, const char *format,...)
Create a log message with a human-readable header in the given buffer.
Definition: lwpa_log.c:248
struct LwpaSyslogParams LwpaSyslogParams
A set of parameters for the syslog header.
#define LWPA_LOG_HOSTNAME_MAX_LEN
Max length of the hostname param.
Definition: lwpa_log.h:105
void(* lwpa_log_callback)(void *context, const char *syslog_str, const char *human_str, const char *raw_str)
Log callback function.
Definition: lwpa_log.h:171
#define LWPA_LOG_APP_NAME_MAX_LEN
Max length of the app_name param.
Definition: lwpa_log.h:106
#define LWPA_LOG_PROCID_MAX_LEN
Max length of the procid param.
Definition: lwpa_log.h:107
@ kLwpaLogCreateBoth
lwpa_log() and lwpa_vlog() create both a syslog message and a human-readable log message and pass the...
Definition: lwpa_log.h:194
@ kLwpaLogCreateHumanReadableLog
lwpa_log() and lwpa_vlog() create a human-readable log message and pass it back in the human_str para...
Definition: lwpa_log.h:191
@ kLwpaLogCreateSyslog
lwpa_log() and lwpa_vlog() create a syslog message and pass it back in the syslog_str parameter of th...
Definition: lwpa_log.h:188
A set of parameters used for the lwpa_*log() functions.
Definition: lwpa_log.h:212
lwpa_log_callback log_fn
A callback function for the finished log string(s).
Definition: lwpa_log.h:216
lwpa_log_action_t action
What should be done when lwpa_*log() is called.
Definition: lwpa_log.h:214
LwpaSyslogParams syslog_params
The syslog header parameters.
Definition: lwpa_log.h:218
int log_mask
A mask value that determines which priority messages can be logged.
Definition: lwpa_log.h:220
void * context
Application context that will be passed back with the log callback function.
Definition: lwpa_log.h:225
lwpa_log_time_fn time_fn
A callback function for lwpa_*log() functions to obtain the time from the application.
Definition: lwpa_log.h:223
A set of parameters which represent the current local time with millisecond resolution.
Definition: lwpa_log.h:139
int utc_offset
The local offset from UTC in minutes.
Definition: lwpa_log.h:155
int hour
Hours since midnight.
Definition: lwpa_log.h:147
int day
Day of the month.
Definition: lwpa_log.h:145
int minute
Minutes past the current hour.
Definition: lwpa_log.h:149
int second
Seconds past the current minute.
Definition: lwpa_log.h:151
int year
Absolute year.
Definition: lwpa_log.h:141
int msec
Milliseconds past the current second.
Definition: lwpa_log.h:153
int month
Month of the year.
Definition: lwpa_log.h:143
A set of parameters for the syslog header.
Definition: lwpa_log.h:199
char procid[LWPA_LOG_PROCID_MAX_LEN]
Syslog PROCID; see RFC 5424 § 6.2.6.
Definition: lwpa_log.h:207
char hostname[LWPA_LOG_HOSTNAME_MAX_LEN]
Syslog HOSTNAME; see RFC 5424 § 6.2.4.
Definition: lwpa_log.h:203
int facility
Syslog Facility; see RFC 5424 § 6.2.1.
Definition: lwpa_log.h:201
char app_name[LWPA_LOG_APP_NAME_MAX_LEN]
Syslog APP-NAME; see RFC 5424 § 6.2.5.
Definition: lwpa_log.h:205