summaryrefslogtreecommitdiff
path: root/toxcore/logger.c
blob: 2ef5f21aef8a9d03da42ac95b61cce86ef71d098 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*  logger.c
 *
 *  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"
#include "crypto_core.h" /* for random_int() */

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

#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
#   define getpid() ((unsigned) GetCurrentProcessId())
#   define SFILE(FILE__M) (strrchr(FILE__M, '\\') ? strrchr(FILE__M, '\\') + 1 : FILE__M)
#else
#   define SFILE(FILE__M) (strrchr(FILE__M, '/') ? strrchr(FILE__M, '/') + 1 : FILE__M)
#endif


typedef struct logger {
    FILE *log_file;
    LOG_LEVEL level;
    uint64_t start_time; /* Time when lib loaded */
    char *id;

    /* Allocate these once */
    char *tstr;
    char *posstr;
    char *msg;

    /* For thread synchronisation */
    pthread_mutex_t mutex[1];
} logger;

logger *global = NULL;

const char *LOG_LEVEL_STR [] = {
    [LOG_TRACE]   = "TRACE",
    [LOG_DEBUG]   = "DEBUG",
    [LOG_INFO]    = "INFO" ,
    [LOG_WARNING] = "WARN" ,
    [LOG_ERROR]   = "ERROR",
};

char *strtime(char *dest, size_t max_len)
{
    time_t timer;
    struct tm *tm_info;

    time(&timer);
    tm_info = localtime(&timer);

    strftime(dest, max_len, "%m:%d %H:%M:%S", tm_info);
    return dest;
}


/**
 * Public Functions
 */
logger *logger_new (const char *file_name, LOG_LEVEL level, const char *id)
{
#ifndef LOGGING /* Disabled */
    return NULL;
#endif

    logger *retu = calloc(1, sizeof(logger));

    if (!retu)
        return NULL;

    if ( pthread_mutex_init(retu->mutex, NULL) != 0 ) {
        free(retu);
        return NULL;
    }

    if (!(retu->log_file = fopen(file_name, "ab"))) {
        fprintf(stderr, "Error opening logger file: %s; info: %s\n", file_name, strerror(errno));
        free(retu);
        pthread_mutex_destroy(retu->mutex);
        return NULL;
    }

    if (!(retu->tstr = calloc(16, sizeof (char))) ||
            !(retu->posstr = calloc(300, sizeof (char))) ||
            !(retu->msg = calloc(4096, sizeof (char))) )
        goto ERROR;

    if (id) {
        if (!(retu->id = calloc(strlen(id) + 1, 1)))
            goto ERROR;

        strcpy(retu->id, id);
    } else {
        if (!(retu->id = malloc(8)))
            goto ERROR;

        snprintf(retu->id, 8, "%u", random_int());
    }

    retu->level = level;
    retu->start_time = current_time_monotonic();

    fprintf(retu->log_file, "Successfully created and running logger id: %s; time: %s\n",
            retu->id, strtime(retu->tstr, 16));

    return retu;

ERROR:
    fprintf(stderr, "Failed to create logger!\n");
    pthread_mutex_destroy(retu->mutex);
    fclose(retu->log_file);
    free(retu->tstr);
    free(retu->posstr);
    free(retu->msg);
    free(retu->id);
    free(retu);
    return NULL;
}

void logger_kill(logger *log)
{
#ifndef LOGGING /* Disabled */
    return;
#endif

    if (!log)
        return;

    pthread_mutex_lock(log->mutex);
    free(log->id);
    free(log->tstr);
    free(log->posstr);
    free(log->msg);

    if (fclose(log->log_file) != 0 )
        perror("Could not close log file");

    pthread_mutex_unlock(log->mutex);
    pthread_mutex_destroy(log->mutex);

    free(log);
}

void logger_kill_global(void)
{
    logger_kill(global);
    global = NULL;
}

void logger_set_global(logger *log)
{
#ifndef LOGGING /* Disabled */
    return;
#endif

    global = log;
}

logger *logger_get_global(void)
{
#ifndef LOGGING /* Disabled */
    return NULL;
#endif

    return global;
}

void logger_write (logger *log, LOG_LEVEL level, const char *file, int line, const char *format, ...)
{
#ifndef LOGGING /* Disabled */
    return;
#endif

    static const char *logger_format =
        "%s  "   /* Logger id string */
        "%-16s"  /* Time string of format: %m:%d %H:%M:%S */
        "%u  "   /* Thread id */
        "%-5s  " /* Logger lever string */
        "%-20s " /* File:line string */
        "- %s"   /* Output message */
        "\n";    /* Every new print new line */


    logger *this_log = log ? log : global;

    if (!this_log)
        return;

    /* Don't print levels lesser than set one */
    if (this_log->level > level)
        return;

    pthread_mutex_lock(this_log->mutex);

    /* Set position str */
    snprintf(this_log->posstr, 300, "%s:%d", SFILE(file), line);

    /* Set message */
    va_list args;
    va_start (args, format);
    vsnprintf(this_log->msg, 4096, format, args);
    va_end (args);

    fprintf(this_log->log_file, logger_format, this_log->id, strtime(this_log->tstr, 16), pthread_self(),
            LOG_LEVEL_STR[level], this_log->posstr, this_log->msg);
    fflush(this_log->log_file);

    pthread_mutex_unlock(this_log->mutex);
}