3 * Copyright (c) 2013 Thomas Perl <m@thp.io>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #ifndef HYBRIS_LOGGING_H
20 #define HYBRIS_LOGGING_H
23 #include <sys/types.h>
31 enum hybris_log_level
{
32 /* Most verbose logging level */
35 /* Normal logging levels */
41 * "Fake" level at which no messages are logged.
42 * Can be used with hybris_set_log_level().
47 enum hybris_log_format
{
48 HYBRIS_LOG_FORMAT_NORMAL
,
49 HYBRIS_LOG_FORMAT_SYSTRACE
53 * Returns nonzero if messages at level "level" should be logged.
54 * Only used by the HYBRIS_LOG() macro, no need to call it manually.
57 hybris_should_log(enum hybris_log_level level
);
61 * Sets the minimum log level that is logged, for example a minimum
62 * log level of HYBRIS_LOG_DEBUG would print all log messages, a
63 * minimum log level of HYBRIS_LOG_WARN would only print warnings and
64 * errors. The default log level is HYBRIS_LOG_WARN.
67 hybris_set_log_level(enum hybris_log_level level
);
70 hybris_get_thread_id();
72 enum hybris_log_format
hybris_logging_format();
74 int hybris_should_trace(const char *module
, const char *tracepoint
);
76 extern pthread_mutex_t hybris_logging_mutex
;
82 extern FILE *hybris_logging_target
;
85 # define HYBRIS_LOG_(level, module, message, ...) do { \
86 if (hybris_should_log(level)) { \
87 pthread_mutex_lock(&hybris_logging_mutex); \
88 if (hybris_logging_format() == HYBRIS_LOG_FORMAT_NORMAL) \
90 fprintf(hybris_logging_target, "%s %s:%d (%s) %s: " message "\n", \
91 module, __FILE__, __LINE__, __PRETTY_FUNCTION__, \
92 #level + 11 /* + 11 = strip leading "HYBRIS_LOG_" */, \
94 fflush(hybris_logging_target); \
95 } else if (hybris_logging_format() == HYBRIS_LOG_FORMAT_SYSTRACE) { \
96 fprintf(hybris_logging_target, "B|%i|%s(%s) %s:%d (%s) " message "\n", \
97 getpid(), module, __PRETTY_FUNCTION__, __FILE__, __LINE__, \
98 #level + 11 /* + 11 = strip leading "HYBRIS_LOG_" */, \
100 fflush(hybris_logging_target); \
101 fprintf(hybris_logging_target, "E|%i|%s(%s) %s:%d (%s) " message "\n", \
102 getpid(), module, __PRETTY_FUNCTION__, __FILE__, __LINE__, \
103 #level + 11 /* + 11 = strip leading "HYBRIS_LOG_" */, \
105 fflush(hybris_logging_target); \
107 pthread_mutex_unlock(&hybris_logging_mutex); \
111 #define HYBRIS_TRACE_RECORD(module, what, tracepoint, message, ...) do { \
112 if (hybris_should_trace(module, tracepoint)) { \
113 pthread_mutex_lock(&hybris_logging_mutex); \
114 if (hybris_logging_format() == HYBRIS_LOG_FORMAT_NORMAL) \
116 fprintf(hybris_logging_target, "PID: %i Tracepoint-%c/%s::%s" message "\n", \
117 getpid(), what, tracepoint, module, \
119 fflush(hybris_logging_target); \
120 } else if (hybris_logging_format() == HYBRIS_LOG_FORMAT_SYSTRACE) { \
122 fprintf(hybris_logging_target, "B|%i|%s::%s" message "", \
123 getpid(), tracepoint, module, ##__VA_ARGS__); \
124 else if (what == 'E') \
125 fprintf(hybris_logging_target, "E"); \
127 fprintf(hybris_logging_target, "C|%i|%s::%s-%i|" message "", \
128 getpid(), tracepoint, module, getpid(), ##__VA_ARGS__); \
129 fflush(hybris_logging_target); \
131 pthread_mutex_unlock(&hybris_logging_mutex); \
134 # define HYBRIS_TRACE_BEGIN(module, tracepoint, message, ...) HYBRIS_TRACE_RECORD(module, 'B', tracepoint, message, ##__VA_ARGS__)
135 # define HYBRIS_TRACE_END(module, tracepoint, message, ...) HYBRIS_TRACE_RECORD(module, 'E', tracepoint, message, ##__VA_ARGS__)
136 # define HYBRIS_TRACE_COUNTER(module, tracepoint, message, ...) HYBRIS_TRACE_RECORD(module, 'C', tracepoint, message, ##__VA_ARGS__)
138 # define HYBRIS_LOG_(level, module, message, ...) while (0) {}
139 # define HYBRIS_TRACE_BEGIN(module, tracepoint, message, ...) while (0) {}
140 # define HYBRIS_TRACE_END(module, tracepoint, message, ...) while (0) {}
141 # define HYBRIS_TRACE_COUNTER(module, tracepoint, message, ...) while (0) {}
145 /* Generic logging functions taking the module name and a message */
146 #define HYBRIS_DEBUG_LOG(module, message, ...) HYBRIS_LOG_(HYBRIS_LOG_DEBUG, #module, message, ##__VA_ARGS__)
147 #define HYBRIS_WARN_LOG(module, message, ...) HYBRIS_LOG_(HYBRIS_LOG_WARN, #module, message, ##__VA_ARGS__)
148 #define HYBRIS_INFO_LOG(module, message, ...) HYBRIS_LOG_(HYBRIS_LOG_INFO, #module, message, ##__VA_ARGS__)
149 #define HYBRIS_ERROR_LOG(module, message, ...) HYBRIS_LOG_(HYBRIS_LOG_ERROR, #module, message, ##__VA_ARGS__)
152 /* Module-specific logging functions can be defined like this */
153 #define HYBRIS_DEBUG(message, ...) HYBRIS_DEBUG_LOG(HYBRIS, message, ##__VA_ARGS__)
154 #define HYBRIS_WARN(message, ...) HYBRIS_WARN_LOG(HYBRIS, message, ##__VA_ARGS__)
155 #define HYBRIS_INFO(message, ...) HYBRIS_INFO_LOG(HYBRIS, message, ##__VA_ARGS__)
156 #define HYBRIS_ERROR(message, ...) HYBRIS_ERROR_LOG(HYBRIS, message, ##__VA_ARGS__)
158 /* for compatibility reasons */
159 #define TRACE(message, ...) HYBRIS_DEBUG_LOG(EGL, message, ##__VA_ARGS__)
161 #endif /* HYBRIS_LOGGING_H */
162 // vim: noai:ts=4:sw=4:ss=4:expandtab