Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
logging.h
Go to the documentation of this file.
1
34#ifndef _UTILS_LOGGING_H_
35#define _UTILS_LOGGING_H_
36
37#ifndef __cplusplus
38#ifndef __STDC_VERSION__
40#define __STDC_VERSION__ 199901L
41#endif
42#endif
43
44#include <stdio.h>
45#include <inttypes.h>
46#include "os/os.h"
47
49static const int ALWAYS = -1;
51static const int FATAL = 0;
53static const int LEVEL_ERROR = 1;
55static const int WARNING = 2;
57static const int INFO = 3;
59static const int VERBOSE = 4;
60
61#if defined(__linux__) || defined(__MACH__) || defined(GCC_ARMCM3) || defined(GCC_ARMCM0)
62#define LOCKED_LOGGING
63#endif
64
65#ifdef LOCKED_LOGGING
66extern os_mutex_t g_log_mutex;
67#define LOCK_LOG os_mutex_lock(&g_log_mutex)
68#define UNLOCK_LOG os_mutex_unlock(&g_log_mutex)
69#else
71#define LOCK_LOG
73#define UNLOCK_LOG
74#endif
75
76#ifdef __cplusplus
77#define GLOBAL_LOG_OUTPUT ::log_output
78#else
81#define GLOBAL_LOG_OUTPUT log_output
82#endif
83
84#ifdef __FreeRTOS__
85#define LOG_MAYBE_DIE(level) (level == FATAL)
86#else
89#define LOG_MAYBE_DIE(level) 0
90#endif
91
99#define LOG(level, message...) \
100 do \
101 { \
102 if (LOG_MAYBE_DIE(level)) \
103 { \
104 DIE("log fatal"); \
105 } \
106 else if (level == FATAL) \
107 { \
108 fprintf(stderr, message); \
109 fprintf(stderr, "\n"); \
110 abort(); \
111 } \
112 else if (LOGLEVEL >= level) \
113 { \
114 LOCK_LOG; \
115 int sret = snprintf(logbuffer, sizeof(logbuffer), message); \
116 if (sret > (int)sizeof(logbuffer)) \
117 sret = sizeof(logbuffer); \
118 GLOBAL_LOG_OUTPUT(logbuffer, sret); \
119 UNLOCK_LOG; \
120 } \
121 } while (0)
122
124#define LOG_ERROR(message...) LOG(LEVEL_ERROR, message)
125
126#if defined(__linux__) || defined(__MACH__)
127extern char logbuffer[4096];
128#elif defined(ESP_PLATFORM)
129extern char logbuffer[1024];
130#else
132extern char logbuffer[256];
133#endif
134
135#ifndef LOGLEVEL
136#ifdef __FreeRTOS__
137#define LOGLEVEL FATAL
138#else
140#define LOGLEVEL INFO
141#endif // not FreeRTOS
142#endif // ifndef LOGLEVEL
143
144#ifdef __cplusplus
145extern "C" {
146#endif
154void log_output(char *buf, int size);
161void print_errno_and_exit(const char *where);
162#ifdef __cplusplus
163}
164#endif
165
174#define ERRNOCHECK(where, x...) \
175 do \
176 { \
177 if ((x) < 0) \
178 { \
179 print_errno_and_exit(where); \
180 } \
181 } while (0)
182
183#endif // _UTILS_LOGGING_H_
static const int VERBOSE
Loglevel that is usually not printed, reporting debugging information.
Definition logging.h:59
static const int WARNING
Loglevel that is always printed, reporting a warning or a retryable error.
Definition logging.h:55
static const int LEVEL_ERROR
Loglevel that is always printed, reporting an error.
Definition logging.h:53
void print_errno_and_exit(const char *where)
Prints an error message about errno to std error and terminates the current program.
Definition errno_exit.c:47
char logbuffer[256]
Temporary buffer to sprintf() the log lines into.
Definition logging.cxx:42
static const int INFO
Loglevel that is printed by default, reporting some status information.
Definition logging.h:57
static const int ALWAYS
Loglevel that is always printed.
Definition logging.h:49
void log_output(char *buf, int size)
Prints a line of log to the log output destination.
Definition logging.cxx:73
static const int FATAL
Loglevel that kills the current process.
Definition logging.h:51