summaryrefslogtreecommitdiffstats
path: root/eurephia_log.c
blob: ba65b09e68efc5e1bb6bdb29087c085f362fe3c7 (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
/*  eurephia_log.c  --  eurephia logging
 *
 *  GPLv2 - Copyright (C) 2008  David Sommerseth <dazo@users.sourceforge.net>
 *
 *  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; version 2
 *  of the License.
 *
 *  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
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */

#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <pthread.h>

#include "eurephia_struct.h"
#include "eurephia_log.h"

const char *erp_logtypes[] = {
        "\0",
        "-- INFO --      \0",
        "-- DEBUG --     \0",
        "** WARNING **   \0", 
        "** ERROR **     \0",
        "** CRITICAL **  \0",
        "** - FATAL - ** \0",
        "** * PANIC * ** \0" 
};

// POSIX Mutex to avoild simultaneously logging activity from
// several threads at the same time
pthread_mutex_t log_mutex = PTHREAD_MUTEX_INITIALIZER;

// Simple log function ... Write log data to the context log file
void eurephia_log(eurephiaCTX *ctx, int logdst, int loglvl, const char *fmt, ... ) {

        // Only log if we have an open log file and which has high enough log level
        if( (ctx->log != NULL) && (ctx->loglevel >= loglvl) ) {
                va_list ap;
                char tstmp_str[200];
                time_t tstmp;
                struct tm *loctstmp;

                // Get timestamp
                memset(&tstmp_str, 0, 200);
                tstmp = time(NULL);
                loctstmp = localtime(&tstmp);
                if( loctstmp != NULL ) {
                        if( strftime(tstmp_str, 198, "%Y-%m-%d %H:%M:%S %Z", loctstmp) == 0 ) {
                                snprintf(tstmp_str, 198, "(error getting timestamp string)");
                        }
                } else {
                        snprintf(tstmp_str, 198, "(error getting timestamp)");
                }

                va_start(ap, fmt);
                pthread_mutex_lock(&log_mutex); // Block other threads from writing when we write
                fprintf(ctx->log, "[%s] %s [%i] ", tstmp_str, erp_logtypes[logdst], loglvl);
                vfprintf(ctx->log, fmt, ap);
                fprintf(ctx->log, "\n");
                fflush(ctx->log);
                pthread_mutex_unlock(&log_mutex); // Unblock other threads
                va_end(ap);
        }
}