summaryrefslogtreecommitdiffstats
path: root/common/eurephia_log.c
blob: 0955aa077baa504ef1fde76de9d5fdb1146efa48 (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
/*  eurephia_log.c  --  eurephia logging
 *
 *  GPLv2 only - 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.
 *
 */

/**
 * @file   eurephia_log.c
 * @author David Sommerseth <dazo@users.sourceforge.net>
 * @date   2008-08-06
 *
 * @brief  Function for unified logging
 *
 */


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

#include <eurephia_context.h>
#include "eurephia_log.h"

/**
 * Mapping table for mapping log types (defined in eurephia_log.h)
 * to string values
 */
const char *erp_logtypes[] = {
        "\0",
        "-- INFO --      \0",	/**< LOG_INFO     */
        "-- DEBUG --     \0",	/**< LOG_DEBUG    */
        "** WARNING **   \0",	/**< LOG_WARNING  */
        "** ERROR **     \0",	/**< LOG_ERROR    */
        "** CRITICAL **  \0",	/**< LOG_CRITICAL */
        "** - FATAL - ** \0",	/**< LOG_FATAL    */
        "** * PANIC * ** \0"	/**< LOG_PANIC    */
};

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

/**
 * Simple log function which writes log data to the log file available in the eurephiaCTX
 *
 * @param ctx    eurephiaCTX
 * @param logdst Log destination, can be LOG_INFO, LOG_DEBUG, LOG_WARNING, LOG_ERROR, 
 *        LOG_CRITICAL, LOG_FATAL or LOG_PANIC
 * @param loglvl Log level of the message.  If the eurephiaCTX has a lower log level setup
 *        than what this parameter is set to, the message will not be logged.
 * @param fmt    Contents of the log message (stdarg)
 */
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 != NULL) && (ctx->log != NULL) && (ctx->loglevel >= loglvl) ) {
                va_list ap;
                char tstmp_str[200];
                time_t tstmp;
                struct tm *loctstmp;

                // Get time stamp
                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 time stamp 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);
        }
}