File: logs.c

package info (click to toggle)
enna 0.4.1~r3557-2.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 24,164 kB
  • sloc: ansic: 24,208; makefile: 1,166; sh: 130
file content (134 lines) | stat: -rw-r--r-- 3,000 bytes parent folder | download
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
/*
 * GeeXboX Enna Media Center.
 * Copyright (C) 2005-2010 The Enna Project
 *
 * This file is part of Enna.
 *
 * Enna is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Enna 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Enna; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

#include "enna.h"

#define DEFAULT_MODULE_NAME "Enna"

#define NORMAL   "\033[0m"
#define COLOR(x) "\033[" #x ";1m"
#define BOLD     COLOR(1)
#define F_RED    COLOR(31)
#define F_GREEN  COLOR(32)
#define F_YELLOW COLOR(33)
#define F_BLUE   COLOR(34)
#define B_RED    COLOR(41)


static FILE *fp = NULL;
static int refcount = 0;

int
enna_log_init(const char *filename)
{

    if (refcount > 1)
        return 0;

    if (filename)
    {
        fp = fopen(filename, "w");
        if (!fp)
            return 0;
    }

    refcount++;
    return 1;
}

void
enna_log_print(int level, const char *module,
               char *file, int line, const char *format, ...)
{
    FILE *f;
    static const char *const c[] =
    {
        [ENNA_MSG_EVENT]    = F_BLUE,
        [ENNA_MSG_INFO]     = F_GREEN,
        [ENNA_MSG_WARNING]  = F_YELLOW,
        [ENNA_MSG_ERROR]    = F_RED,
        [ENNA_MSG_CRITICAL] = B_RED,
    };

    static const char *const l[] =
    {
        [ENNA_MSG_EVENT]    = "Event",
        [ENNA_MSG_INFO]     = "Info",
        [ENNA_MSG_WARNING]  = "Warn",
        [ENNA_MSG_ERROR]    = "Err",
        [ENNA_MSG_CRITICAL] = "Crit",
    };

    va_list va;
    int verbosity;
    const char *prefix = NULL;

    if (!format)
        return;

    if (enna) verbosity = enna->lvl;
    else verbosity = ENNA_MSG_INFO;

    /* do we really want loging ? */
    if (verbosity == ENNA_MSG_NONE)
        return;

    if (level < verbosity)
        return;

    va_start (va, format);

    if (!module)
        module = DEFAULT_MODULE_NAME;
    else
        prefix = DEFAULT_MODULE_NAME "/";

    if (!fp)
    {
        f = stderr;
        fprintf (f, "[" BOLD "%s%s" NORMAL "] [%s:%d] %s%s" NORMAL ": ",
            prefix ? prefix : "", module, file, line, c[level], l[level]);
    }
    else
    {
        f = fp;
        fprintf (f, "[%s%s] [%s:%d] %s: ",
            prefix ? prefix : "", module, file, line, l[level]);
    }

    vfprintf (f, format, va);
    fprintf (f, "\n");
    va_end (va);
}

void
enna_log_shutdown(void)
{
    if (fp)
    {
        fclose(fp);
    }
    refcount--;
}