146eb6e699309b6e8540867a989e0ff2d2bddb57
[deb_libhybris.git] / hybris / common / logging.h
1
2 /*
3 * Copyright (c) 2013 Thomas Perl <m@thp.io>
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19 #ifndef HYBRIS_LOGGING_H
20 #define HYBRIS_LOGGING_H
21
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <pthread.h>
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 enum hybris_log_level {
32 /* Most verbose logging level */
33 HYBRIS_LOG_DEBUG = 0,
34
35 /* Normal logging levels */
36 HYBRIS_LOG_INFO,
37 HYBRIS_LOG_WARN,
38 HYBRIS_LOG_ERROR,
39
40 /**
41 * "Fake" level at which no messages are logged.
42 * Can be used with hybris_set_log_level().
43 **/
44 HYBRIS_LOG_DISABLED,
45 };
46
47 enum hybris_log_format {
48 HYBRIS_LOG_FORMAT_NORMAL,
49 HYBRIS_LOG_FORMAT_SYSTRACE
50 };
51
52 /**
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.
55 **/
56 int
57 hybris_should_log(enum hybris_log_level level);
58
59
60 /**
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.
65 **/
66 void
67 hybris_set_log_level(enum hybris_log_level level);
68
69 void *
70 hybris_get_thread_id();
71
72 enum hybris_log_format hybris_logging_format();
73
74 int hybris_should_trace(const char *module, const char *tracepoint);
75
76 extern pthread_mutex_t hybris_logging_mutex;
77
78 #ifdef __cplusplus
79 }
80 #endif
81
82 extern FILE *hybris_logging_target;
83
84 #if defined(DEBUG)
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) \
89 { \
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_" */, \
93 ##__VA_ARGS__); \
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_" */, \
99 ##__VA_ARGS__); \
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_" */, \
104 ##__VA_ARGS__); \
105 fflush(hybris_logging_target); \
106 } \
107 pthread_mutex_unlock(&hybris_logging_mutex); \
108 } \
109 } while(0)
110
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) \
115 { \
116 fprintf(hybris_logging_target, "PID: %i Tracepoint-%c/%s::%s" message "\n", \
117 getpid(), what, tracepoint, module, \
118 ##__VA_ARGS__); \
119 fflush(hybris_logging_target); \
120 } else if (hybris_logging_format() == HYBRIS_LOG_FORMAT_SYSTRACE) { \
121 if (what == 'B') \
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"); \
126 else \
127 fprintf(hybris_logging_target, "C|%i|%s::%s-%i|" message "", \
128 getpid(), tracepoint, module, getpid(), ##__VA_ARGS__); \
129 fflush(hybris_logging_target); \
130 } \
131 pthread_mutex_unlock(&hybris_logging_mutex); \
132 } \
133 } while(0)
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__)
137 #else
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) {}
142 #endif
143
144
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__)
150
151
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__)
157
158 /* for compatibility reasons */
159 #define TRACE(message, ...) HYBRIS_DEBUG_LOG(EGL, message, ##__VA_ARGS__)
160
161 #endif /* HYBRIS_LOGGING_H */
162 // vim: noai:ts=4:sw=4:ss=4:expandtab