Vuo 2.4.4
Loading...
Searching...
No Matches
VuoLog.h
Go to the documentation of this file.
1
10// Don't use `#pragma once` here, since this header is included both from the source tree and from Vuo.framework.
11#ifndef VUO_LOG
12#define VUO_LOG
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
18#include <dlfcn.h>
19#include <libgen.h>
20#include <stdbool.h>
21#include <stdlib.h>
22#include <string.h>
23
32double VuoLogGetTime(void);
33double VuoLogGetElapsedTime(void);
34bool VuoIsDebugEnabled(void);
35
36void VuoLog_backtrace(void);
37#ifdef __cplusplus
38}
39#include <vector>
40std::vector<std::string> VuoLog_getBacktrace(void);
41extern "C" {
42#endif
43
45
46static char *VuoLog_moduleName = 0;
47
52static void __attribute__((constructor(90))) VuoLog_initModuleName(void)
53{
54 Dl_info info;
55 if (dladdr((void *)VuoLog_initModuleName, &info) && info.dli_fname)
56 {
57 VuoLog_moduleName = basename_r(info.dli_fname, (char *)malloc(strlen(info.dli_fname + 1)));
58
60 {
61 // Trim off the `lib` prefix, if any.
62 if (strncmp(VuoLog_moduleName, "lib", 3) == 0)
64
65 // Trim off the extension, if any.
66 char *dot = strrchr(VuoLog_moduleName, '.');
67 if (dot)
68 *dot = 0;
69 }
70 }
71}
72
83#define VuoLog_status(format, ...) VuoLog_statusF(VuoLog_moduleName, __FILE__, __LINE__, __func__, format, ##__VA_ARGS__)
84
88#ifdef DOXYGEN
89void VuoLog_statusF(const char *moduleName, const char *file, const unsigned int linenumber, const char *function, const char *format, ...);
90#else
91void VuoLog_statusF(const char *moduleName, const char *file, const unsigned int linenumber, const char *function, const char *format, ...) __attribute__((format(printf, 5, 6)));
92#endif
93
99#ifdef DOXYGEN
100void VuoLog(const char *moduleName, const char *file, const unsigned int linenumber, const char *function, const char *format, ...);
101#else
102void VuoLog(const char *moduleName, const char *file, const unsigned int linenumber, const char *function, const char *format, ...) __attribute__((format(printf, 5, 6)));
103#endif
104
117#define VL() VuoLog(VuoLog_moduleName, __FILE__, __LINE__, __func__, "")
118
133#define VLog(format, ...) VuoLog(VuoLog_moduleName, __FILE__, __LINE__, __func__, format, ##__VA_ARGS__)
134
140#define VUserLog(format, ...) VuoLog(VuoLog_moduleName, __FILE__, __LINE__, __func__, format, ##__VA_ARGS__)
141
147#define VDebugLog(format, ...) do { if (VuoIsDebugEnabled()) VuoLog(VuoLog_moduleName, __FILE__, __LINE__, __func__, format, ##__VA_ARGS__); } while(0)
148
161#define VLogHeap(heapPointer) do { \
162 char *description = VuoHeap_getDescription(heapPointer); \
163 VLog("%s = %p (registered at %s)", #heapPointer, heapPointer, description); \
164 free(description); \
165 } while(0)
166
167
168void VuoLog_recordTime(const char *name, double time);
169
170#ifdef VUO_PROFILE
171#define VUOLOG_PROFILE_BEGIN(object) double VuoLog_time_ ## object = VuoLogGetTime();
172#define VUOLOG_PROFILE_END(object) VuoLog_recordTime(#object, VuoLogGetTime() - VuoLog_time_ ## object);
173#else
185#define VUOLOG_PROFILE_BEGIN(object)
186
190#define VUOLOG_PROFILE_END(object)
191#endif
192
205#ifndef DOXYGEN
206// Executes the block passed to it (an adapter in order to use `__attribute__((cleanup(…)))` with C Blocks).
207static inline void VuoDeferCleanup(void (^*b)(void)) { if (*b) (*b)(); }
208
209// Combines two strings into an identifier.
210#define VuoDeferMerge(a,b) a##b
211
212// Create the name of the defer scope variable.
213#define VuoDeferVarName(a) VuoDeferMerge(VuoDeferScopeVar, a)
214#endif
215
230#define VuoDefer __attribute__((cleanup(VuoDeferCleanup),unused)) void (^VuoDeferVarName(__COUNTER__))(void) =
231
236#ifdef __cplusplus
237}
238#endif
239
240#endif