logger.h 4 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2016 Savoir-faire Linux Inc.
Guillaume Roguez's avatar
Guillaume Roguez committed
3
 *
4
 *  Author: Julien Bonjean <julien.bonjean@savoirfairelinux.com>
Guillaume Roguez's avatar
Guillaume Roguez committed
5
 *  Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
6
7
8
9
10
11
12
13
14
15
16
17
18
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
20
21
 */

Guillaume Roguez's avatar
Guillaume Roguez committed
22
#pragma once
Julien Bonjean's avatar
Julien Bonjean committed
23

Vittorio Giovara's avatar
Vittorio Giovara committed
24
25
26
27
#ifdef __cplusplus
extern "C" {
#endif

28
#include <stdarg.h>
Edric Milaret's avatar
Edric Milaret committed
29
30
#define __STDC_FORMAT_MACROS 1
#include <inttypes.h>
31

Guillaume Roguez's avatar
Guillaume Roguez committed
32
33
#define LOGFILE "dring"

Vittorio Giovara's avatar
Vittorio Giovara committed
34
35
36
/**
 * Print something, coloring it depending on the level
 */
atraczyk's avatar
atraczyk committed
37
38
39
40
41
42
43

#ifdef RING_UWP
  void wlogger(const int level, const char* file, const char* format, ...);
#else
  void logger(const int level, const char* format, ...)
#endif
#if defined(_WIN32) && !defined(RING_UWP)
44
45
    __attribute__((format(gnu_printf, 2, 3)))
#elif defined(__GNUC__)
46
47
48
    __attribute__((format(printf, 2, 3)))
#endif
    ;
Guillaume Roguez's avatar
Guillaume Roguez committed
49
void vlogger(const int level, const char* format, va_list);
Vittorio Giovara's avatar
Vittorio Giovara committed
50
51
52
53
54
55
56
57
58
59

/**
 * Allow writing on the console
 */
void setConsoleLog(int c);

/**
 * When debug mode is not set, logging will not print anything
 */
void setDebugMode(int d);
60

Vittorio Giovara's avatar
Vittorio Giovara committed
61
62
63
64
/**
 * Return the current mode
 */
int getDebugMode(void);
65

66
/**
Vittorio Giovara's avatar
Vittorio Giovara committed
67
68
69
 * Thread-safe function to print the stringified contents of errno
 */
void strErr();
Julien Bonjean's avatar
Julien Bonjean committed
70

Guillaume Roguez's avatar
Guillaume Roguez committed
71
72
#define STR(EXP) #EXP
#define XSTR(X) STR(X)
73

74
// Line return char in a string
atraczyk's avatar
atraczyk committed
75
76
77
#ifdef RING_UWP
#define ENDL " "
#else
78
#define ENDL "\n"
atraczyk's avatar
atraczyk committed
79
#endif
80

Guillaume Roguez's avatar
Guillaume Roguez committed
81
// Do not remove the "| " in following without modifying vlogger() code
atraczyk's avatar
atraczyk committed
82
#ifndef RING_UWP
Guillaume Roguez's avatar
Guillaume Roguez committed
83
#define LOG_FORMAT(M, ...) FILE_NAME ":" XSTR(__LINE__) "| " M, ##__VA_ARGS__
atraczyk's avatar
atraczyk committed
84
85
86
87
#else
#define FILE_NAME_ONLY(X) (strrchr(X, '\\') ? strrchr(X, '\\') + 1 : X)
#define LOG_FORMAT(M, ...) ":" XSTR(__LINE__) "| " M, ##__VA_ARGS__
#endif
Tristan Matthews's avatar
Tristan Matthews committed
88

89
#ifdef __ANDROID__
90

91
#include <android/log.h>
92

93
#ifndef APP_NAME
Adrien Béraud's avatar
Adrien Béraud committed
94
#define APP_NAME "libdring"
Vittorio Giovara's avatar
Vittorio Giovara committed
95
#endif /* APP_NAME */
96

97
98
99
#undef LOG_FORMAT
#define LOG_FORMAT(M, ...) "%s:%d | " M, FILE_NAME, __LINE__, ##__VA_ARGS__

100
// Avoid printing whole path on android
Guillaume Roguez's avatar
Guillaume Roguez committed
101
#define FILE_NAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
102

103
104
105
106
107
108
// because everyone likes reimplementing the wheel
#define LOG_ERR     ANDROID_LOG_ERROR
#define LOG_WARNING ANDROID_LOG_WARN
#define LOG_INFO    ANDROID_LOG_INFO
#define LOG_DEBUG   ANDROID_LOG_DEBUG

Guillaume Roguez's avatar
Guillaume Roguez committed
109
#define LOGGER(M, LEVEL, ...) __android_log_print(LEVEL, APP_NAME, LOG_FORMAT(M, ##__VA_ARGS__))
110

111
#elif defined(_WIN32)
Edric Milaret's avatar
Edric Milaret committed
112
113

#include "winsyslog.h"
114

Edric Milaret's avatar
Edric Milaret committed
115
116
117
118
119
120
121
#define LOG_ERR     EVENTLOG_ERROR_TYPE
#define LOG_WARNING EVENTLOG_WARNING_TYPE
#define LOG_INFO    EVENTLOG_INFORMATION_TYPE
#define LOG_DEBUG   EVENTLOG_SUCCESS

#define FILE_NAME __FILE__

atraczyk's avatar
atraczyk committed
122
#ifndef RING_UWP
Edric Milaret's avatar
Edric Milaret committed
123
#define LOGGER(M, LEVEL, ...) logger(LEVEL, LOG_FORMAT(M, ##__VA_ARGS__))
atraczyk's avatar
atraczyk committed
124
125
126
#else
#define LOGGER(M, LEVEL, ...) wlogger(LEVEL, FILE_NAME, LOG_FORMAT(M, ##__VA_ARGS__))
#endif
127
128

#else
129
130
131
132
133
134
135

#include <syslog.h>

#define FILE_NAME __FILE__

#define LOGGER(M, LEVEL, ...) logger(LEVEL, LOG_FORMAT(M, ##__VA_ARGS__))

136
#endif /* __ANDROID__ _WIN32 */
Julien Bonjean's avatar
Julien Bonjean committed
137

138
139
140
141
142
143
144
145
146
#define RING_ERR(M, ...)   LOGGER(M ENDL, LOG_ERR, ##__VA_ARGS__)
#define RING_WARN(M, ...)  LOGGER(M ENDL, LOG_WARNING, ##__VA_ARGS__)
#define RING_INFO(M, ...)  LOGGER(M ENDL, LOG_INFO, ##__VA_ARGS__)
#define RING_DBG(M, ...)   LOGGER(M ENDL, LOG_DEBUG, ##__VA_ARGS__)

#define RING_XERR(M, ...)   LOGGER(M, LOG_ERR, ##__VA_ARGS__)
#define RING_XWARN(M, ...)  LOGGER(M, LOG_WARNING, ##__VA_ARGS__)
#define RING_XINFO(M, ...)  LOGGER(M, LOG_INFO, ##__VA_ARGS__)
#define RING_XDBG(M, ...)   LOGGER(M, LOG_DEBUG, ##__VA_ARGS__)
147

Vittorio Giovara's avatar
Vittorio Giovara committed
148
149
150
#ifdef __cplusplus
}
#endif