logger.h 4.3 KB
Newer Older
1
/*
Adrien Béraud's avatar
Adrien Béraud committed
2
 *  Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *  Author: Julien Bonjean <julien.bonjean@savoirfairelinux.com>
 *
 *  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
17
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
18 19 20 21 22 23 24 25 26 27 28
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
29 30
 */

Vittorio Giovara's avatar
Vittorio Giovara committed
31 32
#ifndef H_LOGGER
#define H_LOGGER
33

Vittorio Giovara's avatar
Vittorio Giovara committed
34 35 36 37
#ifdef __cplusplus
extern "C" {
#endif

38 39
#include <stdarg.h>

Vittorio Giovara's avatar
Vittorio Giovara committed
40 41 42 43
/**
 * Print something, coloring it depending on the level
 */
void logger(const int level, const char *format, ...);
44
void vlogger(const int level, const char *format, va_list);
Vittorio Giovara's avatar
Vittorio Giovara committed
45 46 47 48 49 50 51 52 53 54

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

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

Vittorio Giovara's avatar
Vittorio Giovara committed
56 57 58 59
/**
 * Return the current mode
 */
int getDebugMode(void);
60

61
/**
Vittorio Giovara's avatar
Vittorio Giovara committed
62 63 64
 * Thread-safe function to print the stringified contents of errno
 */
void strErr();
65

66 67 68 69 70
#ifdef __linux__

#include <unistd.h>
#include <sys/syscall.h>

Vittorio Giovara's avatar
Vittorio Giovara committed
71
#define LOG_FORMAT(M, ...) "%s:%d:0x%x: " M, FILE_NAME, __LINE__, \
72
                            syscall(__NR_gettid) & 0xffff, \
Vittorio Giovara's avatar
Vittorio Giovara committed
73
                           ##__VA_ARGS__
74 75
#else

Edric Milaret's avatar
Edric Milaret committed
76 77
#define LOG_FORMAT(M, ...) "%s:%d: " M, FILE_NAME , __LINE__, \
                            ##__VA_ARGS__
78
#endif
79

80
#ifdef __ANDROID__
81

82
#include <android/log.h>
83

84
#ifndef APP_NAME
Adrien Béraud's avatar
Adrien Béraud committed
85
#define APP_NAME "libdring"
Vittorio Giovara's avatar
Vittorio Giovara committed
86
#endif /* APP_NAME */
87 88

// Avoid printing whole path on android
Vittorio Giovara's avatar
Vittorio Giovara committed
89 90
#define FILE_NAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 \
                                          : __FILE__)
91

92 93 94 95 96 97 98 99
// 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

#define LOGGER(M, LEVEL, ...) __android_log_print(LEVEL, APP_NAME, \
                                                  LOG_FORMAT(M, ##__VA_ARGS__))
100

Edric Milaret's avatar
Edric Milaret committed
101 102 103
#elif _WIN32

#include "winsyslog.h"
104

Edric Milaret's avatar
Edric Milaret committed
105 106 107 108 109 110 111 112
#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__

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

#else
115 116 117 118 119 120 121

#include <syslog.h>

#define FILE_NAME __FILE__

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

122
#endif /* __ANDROID__ _WIN32 */
123

Adrien Béraud's avatar
Adrien Béraud committed
124 125 126 127
#define RING_ERR(M, ...)   LOGGER(M, LOG_ERR, ##__VA_ARGS__)
#define RING_WARN(M, ...)    LOGGER(M, LOG_WARNING, ##__VA_ARGS__)
#define RING_INFO(M, ...)    LOGGER(M, LOG_INFO, ##__VA_ARGS__)
#define RING_DBG(M, ...)   LOGGER(M, LOG_DEBUG, ##__VA_ARGS__)
128

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
#define BLACK "\033[22;30m"
#define GREEN "\033[22;32m"
#define BROWN "\033[22;33m"
#define BLUE "\033[22;34m"
#define MAGENTA "\033[22;35m"
#define CYAN "\033[22;36m"
#define GREY "\033[22;37m"
#define DARK_GREY "\033[01;30m"
#define LIGHT_RED "\033[01;31m"
#define LIGHT_SCREEN "\033[01;32m"
#define LIGHT_BLUE "\033[01;34m"
#define LIGHT_MAGENTA "\033[01;35m"
#define LIGHT_CYAN "\033[01;36m"
#define WHITE "\033[01;37m"
#define END_COLOR "\033[0m"

Edric Milaret's avatar
Edric Milaret committed
145 146 147 148 149 150 151 152
#ifndef _WIN32
#define RED "\033[22;31m"
#define YELLOW "\033[01;33m"
#else
#define RED FOREGROUND_RED
#define YELLOW FOREGROUND_RED + FOREGROUND_GREEN
#endif

Vittorio Giovara's avatar
Vittorio Giovara committed
153 154 155
#ifdef __cplusplus
}
#endif
156

Vittorio Giovara's avatar
Vittorio Giovara committed
157
#endif // H_LOGGER