summaryrefslogtreecommitdiffstats
path: root/loader2/log.c
blob: 2872fe845d30df5e6d094d71fac19fa3db1edb17 (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
/*
 * log.c - logging functionality
 *
 * Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Red Hat, Inc.
 * All rights reserved.
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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, see <http://www.gnu.org/licenses/>.
 *
 * Author(s): Erik Troan <ewt@redhat.com>
 *            Matt Wilson <msw@redhat.com>
 *            Michael Fulbright <msf@redhat.com>
 *            Jeremy Katz <katzj@redhat.com>
 */

#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#include "log.h"

static FILE * tty_logfile = NULL;
static FILE * file_logfile = NULL;
static int minLevel = INFO;

static void printLogHeader(int level, FILE *outfile) {
    time_t current_time = time(NULL);
    struct tm *t = gmtime (&current_time);

    switch (level) {
        case DEBUGLVL:
            fprintf (outfile, "%02d:%02d:%02d DEBUG   : ", t->tm_hour,
                     t->tm_min, t->tm_sec);
            break;

        case INFO:
            fprintf (outfile, "%02d:%02d:%02d INFO    : ", t->tm_hour,
                     t->tm_min, t->tm_sec);
            break;

        case WARNING:
            fprintf (outfile, "%02d:%02d:%02d WARNING : ", t->tm_hour,
                     t->tm_min, t->tm_sec);
            break;

        case ERROR:
            fprintf (outfile, "%02d:%02d:%02d ERROR   : ", t->tm_hour,
                     t->tm_min, t->tm_sec);
            break;

        case CRITICAL:
            fprintf (outfile, "%02d:%02d:%02d CRITICAL: ", t->tm_hour,
                     t->tm_min, t->tm_sec);
            break;
    }
}

void logMessageV(int level, const char * s, va_list ap) {

    /* Only log to the screen things that are above the minimum level. */
    if (tty_logfile && level >= minLevel) {
        va_list apc;

        va_copy(apc, ap);

        printLogHeader(level, tty_logfile);
        vfprintf(tty_logfile, s, apc);
        fprintf(tty_logfile, "\n");
        fflush(tty_logfile);

        va_end(apc);
    }

    /* But log everything to the file. */
    if (file_logfile) {
        va_list apc;

        va_copy(apc, ap);

        printLogHeader(level, file_logfile);
        vfprintf(file_logfile, s, apc);
        fprintf(file_logfile, "\n");
        fflush(file_logfile);

        va_end(apc);
    }
}

void logMessage(int level, const char * s, ...) {
    va_list args;

    va_start(args, s);
    logMessageV(level, s, args);
    va_end(args);
}

int tty_logfd = -1;
int file_logfd = -1;

void openLog(int useLocal) {
    int flags;

    if (!useLocal) {
        tty_logfile = fopen("/dev/tty3", "w");
        file_logfile = fopen("/tmp/anaconda.log", "w");
    } else {
        tty_logfile = NULL;
        file_logfile = fopen("debug.log", "w");
    }

    if (tty_logfile) {
        tty_logfd = fileno(tty_logfile);
        flags = fcntl(tty_logfd, F_GETFD, 0) | FD_CLOEXEC;
        fcntl(tty_logfd, F_SETFD, flags);
    }

    if (file_logfile) {
        file_logfd = fileno(file_logfile);
        flags = fcntl(file_logfd, F_GETFD, 0) | FD_CLOEXEC;
        fcntl(file_logfd, F_SETFD, flags);
    }
}

void closeLog(void) {
    if (tty_logfile)
        fclose(tty_logfile);

    if (file_logfile)
        fclose(file_logfile);
}

/* set the level.  higher means you see more verbosity */
void setLogLevel(int level) {
    minLevel = level;
}

int getLogLevel(void) {
    return minLevel;
}

/* vim:set shiftwidth=4 softtabstop=4: */