logger.h 3.58 KB
Newer Older
1
/*
Adrien Béraud's avatar
Adrien Béraud committed
2
 *  Copyright (C) 2004-2015 Savoir-Faire Linux Inc.
3
 *  Author: Julien Bonjean <julien.bonjean@savoirfairelinux.com>
Guillaume Roguez's avatar
Guillaume Roguez committed
4
 *  Author: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 *  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
18
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
19 20 21 22 23 24 25 26 27 28 29
 *
 *  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.
30 31
 */

Guillaume Roguez's avatar
Guillaume Roguez committed
32
#pragma once
33

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

38 39
#include <stdarg.h>

Guillaume Roguez's avatar
Guillaume Roguez committed
40 41
#define LOGFILE "dring"

Vittorio Giovara's avatar
Vittorio Giovara committed
42 43 44
/**
 * Print something, coloring it depending on the level
 */
45 46 47 48 49
void logger(const int level, const char* format, ...)
#ifdef __GNUC__
    __attribute__((format(printf, 2, 3)))
#endif
    ;
Guillaume Roguez's avatar
Guillaume Roguez committed
50
void vlogger(const int level, const char* format, va_list);
Vittorio Giovara's avatar
Vittorio Giovara committed
51 52 53 54 55 56 57 58 59 60

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

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

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

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

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

Guillaume Roguez's avatar
Guillaume Roguez committed
75 76
// Do not remove the "| " in following without modifying vlogger() code
#define LOG_FORMAT(M, ...) FILE_NAME ":" XSTR(__LINE__) "| " M, ##__VA_ARGS__
77

78
#ifdef __ANDROID__
79

80
#include <android/log.h>
81

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

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

89 90 91 92 93 94
// 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
95
#define LOGGER(M, LEVEL, ...) __android_log_print(LEVEL, APP_NAME, LOG_FORMAT(M, ##__VA_ARGS__))
96

97
#elif defined(_WIN32)
Edric Milaret's avatar
Edric Milaret committed
98 99

#include "winsyslog.h"
100

Edric Milaret's avatar
Edric Milaret committed
101 102 103 104 105 106 107 108
#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__))
109 110

#else
111 112 113 114 115 116 117

#include <syslog.h>

#define FILE_NAME __FILE__

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

118
#endif /* __ANDROID__ _WIN32 */
119

Adrien Béraud's avatar
Adrien Béraud committed
120
#define RING_ERR(M, ...)   LOGGER(M, LOG_ERR, ##__VA_ARGS__)
Guillaume Roguez's avatar
Guillaume Roguez committed
121 122
#define RING_WARN(M, ...)  LOGGER(M, LOG_WARNING, ##__VA_ARGS__)
#define RING_INFO(M, ...)  LOGGER(M, LOG_INFO, ##__VA_ARGS__)
Adrien Béraud's avatar
Adrien Béraud committed
123
#define RING_DBG(M, ...)   LOGGER(M, LOG_DEBUG, ##__VA_ARGS__)
124

Vittorio Giovara's avatar
Vittorio Giovara committed
125 126 127
#ifdef __cplusplus
}
#endif