summaryrefslogtreecommitdiff
path: root/toxcore/logger.h
blob: 856c48d0349a6f692ac25e6b84d466994a051e5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*  logger.h
 *
 *  Wrapping logger functions in nice macros
 *
 *  Copyright (C) 2013 Tox project All Rights Reserved.
 *
 *  This file is part of Tox.
 *
 *  Tox 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.
 *
 *  Tox 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 Tox.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


#ifndef __TOXLOGGER
#define __TOXLOGGER

#include <string.h>

#ifdef LOGGING

typedef enum _LoggerLevel {
    INFO,
    DEBUG,
    WARNING,
    ERROR
} LoggerLevel;

/*
 * Set 'level' as the lowest printable level
 */
int logger_init(const char *file_name, LoggerLevel level);
const char *logger_stringify_level(LoggerLevel level);
unsigned logger_get_pid();
void logger_write (LoggerLevel level, const char *format, ...);
char *logger_timestr (char *dest, size_t max_size);
char *logger_posstr (char *dest, size_t max_size, const char *file, int line);

#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
#define _SFILE (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#else
#define _SFILE (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#endif

#define LFORMAT "\n%-15s %-7u %-5s %-20s - %s"
#define WRITE_FORMAT(__LEVEL__, __WHAT__) \
    char __time__[15]; char posstr[200]; char the_str [4096]; \
    snprintf(the_str, 4096, LFORMAT, logger_timestr(__time__, 15), logger_get_pid(), \
    logger_stringify_level(__LEVEL__), logger_posstr(posstr, 200, _SFILE, __LINE__), __WHAT__)

/* Use these macros */

#define LOGGER_INIT(name, level) logger_init(name, level);
#define LOGGER_INFO(format, ...) do { WRITE_FORMAT(INFO, format); logger_write( INFO, the_str, ##__VA_ARGS__ ); } while (0)
#define LOGGER_DEBUG(format, ...) do { WRITE_FORMAT(DEBUG, format); logger_write( DEBUG, the_str, ##__VA_ARGS__ ); } while (0)
#define LOGGER_WARNING(format, ...) do { WRITE_FORMAT(WARNING, format); logger_write( WARNING, the_str, ##__VA_ARGS__ ); } while (0)
#define LOGGER_ERROR(format, ...) do { WRITE_FORMAT(ERROR, format); logger_write( ERROR, the_str, ##__VA_ARGS__ ); } while (0)

/* To do some checks or similar only when logging use this */
#define LOGGER_SCOPE(__SCOPE_DO__) do { __SCOPE_DO__ } while(0)

#else


#define LOGGER_INIT(name, level) do {} while(0)
#define LOGGER_INFO(format, ...) do {} while(0)
#define LOGGER_DEBUG(format, ...) do {} while(0)
#define LOGGER_WARNING(format, ...) do {} while(0)
#define LOGGER_ERROR(format, ...) do {} while(0)

#define LOGGER_SCOPE(__SCOPE_DO__) do {} while(0)

#endif /* LOGGING */




#endif /* __TOXLOGGER */