File: logging.cpp

package info (click to toggle)
icecc 1.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,520 kB
  • sloc: cpp: 14,058; sh: 3,006; ansic: 767; xml: 744; makefile: 231; asm: 2
file content (226 lines) | stat: -rw-r--r-- 5,908 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
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
/* -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 99; -*- */
/* vim: set ts=4 sw=4 et tw=99:  */
/*
    This file is part of Icecream.

    Copyright (c) 2004 Stephan Kulow <coolo@suse.de>

    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, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include <config.h>
#include <iostream>
#include "logging.h"
#include <fstream>
#include <signal.h>
#include <limits.h>
#include <fcntl.h>
#ifdef __linux__
#include <dlfcn.h>
#endif

using namespace std;

int debug_level = Error;
ostream *logfile_trace = nullptr;
ostream *logfile_info = nullptr;
ostream *logfile_warning = nullptr;
ostream *logfile_error = nullptr;
string logfile_prefix;
volatile sig_atomic_t reset_debug_needed = 0;

static ofstream logfile_null("/dev/null");
static ofstream logfile_file;
static string logfile_filename;

static void reset_debug_signal_handler(int);

// Implementation of an iostream helper that allows redirecting output to a given file descriptor.
// This seems to be the only portable way to do it.
namespace
{
class ofdbuf : public streambuf
{
public:
    explicit ofdbuf( int fd ) : fd( fd ) {}
    virtual int_type overflow( int_type c );
    virtual streamsize xsputn( const char* c, streamsize n );
private:
    int fd;
};

ofdbuf::int_type ofdbuf::overflow( int_type c )
{
    if( c != EOF ) {
        char cc = c;
        if( write( fd, &cc, 1 ) != 1 )
            return EOF;
    }
    return c;
}

streamsize ofdbuf::xsputn( const char* c, streamsize n )
{
    return write( fd, c, n );
}

ostream* ccache_stream( int fd )
{
    int status = fcntl( fd, F_GETFL );
    if( status < 0 || ( status & ( O_WRONLY | O_RDWR )) == 0 ) {
        // As logging is not set up yet, this will log to stderr.
        log_warning() << "UNCACHED_ERR_FD provides an invalid file descriptor, using stderr" << endl;
        return &cerr; // fd is not valid fd for writting
    }
    static ofdbuf buf( fd );
    static ostream stream( &buf );
    return &stream;
}
} // namespace

void setup_debug(int level, const string &filename, const string &prefix)
{
    debug_level = level;
    logfile_prefix = prefix;
    logfile_filename = filename;

    if (logfile_file.is_open()) {
        logfile_file.close();
    }

    ostream *output = nullptr;

    if (filename.length()) {
        logfile_file.clear();
        logfile_file.open(filename.c_str(), fstream::out | fstream::app);
#ifdef __linux__

        string fname = filename;
        if (fname[0] != '/') {
            char buf[PATH_MAX];

            if (getcwd(buf, sizeof(buf))) {
                fname.insert(0, "/");
                fname.insert(0, buf);
            }
        }

        setenv("SEGFAULT_OUTPUT_NAME", fname.c_str(), false);
#endif
        output = &logfile_file;
    } else if( const char* ccache_err_fd = getenv( "UNCACHED_ERR_FD" )) {
        output = ccache_stream( atoi( ccache_err_fd ));
    } else {
        output = &cerr;
    }

#ifdef __linux__
    (void) dlopen("libSegFault.so", RTLD_NOW | RTLD_LOCAL);
#endif

    if (debug_level >= Debug) {
        logfile_trace = output;
    } else {
        logfile_trace = &logfile_null;
    }

    if (debug_level >= Info) {
        logfile_info = output;
    } else {
        logfile_info = &logfile_null;
    }

    if (debug_level >= Warning) {
        logfile_warning = output;
    } else {
        logfile_warning = &logfile_null;
    }

    if (debug_level >= Error) {
        logfile_error = output;
    } else {
        logfile_error = &logfile_null;
    }

    signal(SIGHUP, reset_debug_signal_handler);
}

void reset_debug()
{
    setup_debug(debug_level, logfile_filename);
}

void reset_debug_signal_handler(int)
{
    reset_debug_needed = 1;
}

void reset_debug_if_needed()
{
    if( reset_debug_needed ) {
        reset_debug_needed = 0;
        reset_debug();
        if( const char* env = getenv( "ICECC_TEST_FLUSH_LOG_MARK" )) {
            ifstream markfile( env );
            string mark;
            getline( markfile, mark );
            if( !mark.empty()) {
                assert( logfile_trace != NULL );
                *logfile_trace << "flush log mark: " << mark << endl;
            }
        }
        if( const char* env = getenv( "ICECC_TEST_LOG_HEADER" )) {
            ifstream markfile( env );
            string header1, header2, header3;
            getline( markfile, header1 );
            getline( markfile, header2 );
            getline( markfile, header3 );
            if( !header1.empty()) {
                assert( logfile_trace != NULL );
                *logfile_trace << header1 << endl;
                *logfile_trace << header2 << endl;
                *logfile_trace << header3 << endl;
            }
        }
    }
}

void close_debug()
{
    if (logfile_null.is_open()) {
        logfile_null.close();
    }

    if (logfile_file.is_open()) {
        logfile_file.close();
    }

    logfile_trace = logfile_info = logfile_warning = logfile_error = nullptr;
}

/* Flushes all ostreams used for debug messages.  You need to call
   this before forking.  */
void flush_debug()
{
    if (logfile_null.is_open()) {
        logfile_null.flush();
    }

    if (logfile_file.is_open()) {
        logfile_file.flush();
    }
}

unsigned log_block::nesting;