summaryrefslogtreecommitdiff
path: root/toxcore/logger.c
blob: e700fe7122c39bc33b125bbb3785e07209a03ec6 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*  logger.c
 * 
 *  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/>.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */


#include "logger.h"

#ifdef LOGGING

#include "network.h" /* for time */

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
#include <inttypes.h>
#include <time.h>

#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
#define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
#endif

static struct logger_config {
    FILE* log_file;
    LoggerLevel level;
    uint64_t start_time; /* Time when lib loaded */
} 
logger = { 
    NULL, 
    DEBUG,
    0
};

void __attribute__((destructor)) terminate_logger()
{
    if ( !logger.log_file ) return;
    
    time_t tim = time(NULL);
    
    logger_write(ERROR, "============== Closing logger ==============\n"
                        "Time: %s", asctime(localtime(&tim)));
    
    fclose(logger.log_file);
}

unsigned logger_get_pid()
{
    return
    #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
    GetCurrentProcessId();
    #else
    getpid();
    #endif
}

const char* logger_stringify_level(LoggerLevel level)
{
    static const char* strings [] = 
    {
        "INFO",
        "DEBUG",
        "WARNING",
        "ERROR"
    };
    
    return strings[level];
}


int logger_init(const char* file_name, LoggerLevel level)
{
    char* final_l = calloc(sizeof(char), strlen(file_name) + 32);
    sprintf(final_l, "%s"/*.%u"*/, file_name, logger_get_pid());
    
    if ( logger.log_file ) { 
        fprintf(stderr, "Error opening logger name: %s with level %d: already opened!\n", final_l, level);
        free (final_l);
        return -1;
    }
    
    logger.log_file = fopen(final_l, "wb");
    
    if ( logger.log_file == NULL ) {
        char error[1000];
        if ( strerror_r(errno, error, 1000) == 0 ) 
            fprintf(stderr, "Error opening logger file: %s; info: %s\n", final_l, error);
        else
            fprintf(stderr, "Error opening logger file: %s\n", final_l);
        
        free (final_l);
        return -1;
    }
    
    
    logger.level = level;
    logger.start_time = current_time();
    
    
    time_t tim = time(NULL);
    logger_write(ERROR, "============== Starting logger ==============\n"
                        "Time: %s", asctime(localtime(&tim)));
    
    
    
    free (final_l);
    return 0;
}


void logger_write (LoggerLevel level, const char* format, ...)
{    
    if (logger.log_file == NULL) {
        /*fprintf(stderr, "Logger file is NULL!\n");*/
        return;
    }
    
    if (logger.level > level) return; /* Don't print some levels xuh */
        
    va_list _arg;
    va_start (_arg, format);
    vfprintf (logger.log_file, format, _arg);
    va_end (_arg);
    
    fflush(logger.log_file);
}

char* logger_timestr(char* dest)
{
    uint64_t diff = (current_time() - logger.start_time) / 1000; /* ms */
    sprintf(dest, "%"PRIu64"", diff);
    
    return dest;
}


#endif /* LOGGING */