aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxc/xtl_logger_stdio.c
blob: 2e73c862b98763caced1a72360f2dcf821953d25 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * xtl_logger_stdio.c
 *
 * log message consumer that writes to stdio
 *
 * Copyright (c) 2010 Citrix
 * Part of a generic logging interface used by various dom0 userland libraries.
 *
 * This library 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;
 * version 2.1 of the License.
 *
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "xentoollog.h"

#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

struct xentoollog_logger_stdiostream {
    xentoollog_logger vtable;
    FILE *f;
    xentoollog_level min_level;
    unsigned flags;
    int progress_erase_len, progress_last_percent;
    int tty;
};

static void progress_erase(xentoollog_logger_stdiostream *lg) {
    if (lg->progress_erase_len)
        fprintf(lg->f, "\r%*s\r", lg->progress_erase_len, "");
}

static void stdiostream_vmessage(xentoollog_logger *logger_in,
                                 xentoollog_level level,
                                 int errnoval,
                                 const char *context,
                                 const char *format,
                                 va_list al) {
    xentoollog_logger_stdiostream *lg = (void*)logger_in;

    if (level < lg->min_level)
        return;

    progress_erase(lg);

    if (lg->flags & XTL_STDIOSTREAM_SHOW_DATE) {
        struct tm lt_buf;
        time_t now = time(0);
        struct tm *lt= localtime_r(&now, &lt_buf);
        fprintf(lg->f, "%04d-%02d-%02d %02d:%02d:%02d %s ",
                lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday,
                lt->tm_hour, lt->tm_min, lt->tm_sec,
                tzname[daylight ? !!lt->tm_isdst : 0]);
    }
    if (lg->flags & XTL_STDIOSTREAM_SHOW_PID)
        fprintf(lg->f, "[%lu] ", (unsigned long)getpid());

    if (context)
        fprintf(lg->f, "%s: ", context);

    fprintf(lg->f, "%s: ", xtl_level_to_string(level));

    vfprintf(lg->f, format, al);

    if (errnoval >= 0)
        fprintf(lg->f, ": %s", strerror(errnoval));

    putc('\n', lg->f);
    fflush(lg->f);
}

static void stdiostream_message(struct xentoollog_logger *logger_in,
                                xentoollog_level level,
                                const char *context,
                                const char *format, ...)
{
    va_list al;
    va_start(al,format);
    stdiostream_vmessage(logger_in, level, -1, context, format, al);
    va_end(al);
}

static void stdiostream_progress(struct xentoollog_logger *logger_in,
                                 const char *context,
                                 const char *doing_what, int percent,
                                 unsigned long done, unsigned long total) {
    xentoollog_logger_stdiostream *lg = (void*)logger_in;
    int newpel, extra_erase;
    xentoollog_level this_level;

    if (lg->flags & XTL_STDIOSTREAM_HIDE_PROGRESS)
        return;

    if (percent < lg->progress_last_percent) {
        this_level = XTL_PROGRESS;
    } else if (percent == lg->progress_last_percent) {
        return;
    } else if (percent < lg->progress_last_percent + 5) {
        this_level = XTL_DETAIL;
    } else {
        this_level = XTL_PROGRESS;
    }

    if (this_level < lg->min_level)
        return;

    lg->progress_last_percent = percent;

    if (!lg->tty) {
        stdiostream_message(logger_in, this_level, context,
                            "%s: %lu/%lu  %3d%%",
                            doing_what, done, total, percent);
        return;
    }

    if (lg->progress_erase_len)
        putc('\r', lg->f);

    newpel = fprintf(lg->f, "%s%s" "%s: %lu/%lu  %3d%%%s",
                     context?context:"", context?": ":"",
                     doing_what, done, total, percent,
		     done == total ? "\n" : "");

    extra_erase = lg->progress_erase_len - newpel;
    if (extra_erase > 0)
        fprintf(lg->f, "%*s\r", extra_erase, "");

    lg->progress_erase_len = newpel;
}

static void stdiostream_destroy(struct xentoollog_logger *logger_in) {
    xentoollog_logger_stdiostream *lg = (void*)logger_in;
    progress_erase(lg);
    free(lg);
}

void xtl_stdiostream_set_minlevel(xentoollog_logger_stdiostream *lg,
                                  xentoollog_level min_level) {
    lg->min_level = min_level;
}

void xtl_stdiostream_adjust_flags(xentoollog_logger_stdiostream *lg,
                                  unsigned set_flags, unsigned clear_flags) {
    unsigned new_flags = (lg->flags & ~clear_flags) | set_flags;
    if (new_flags & XTL_STDIOSTREAM_HIDE_PROGRESS)
        progress_erase(lg);
    lg->flags = new_flags;
}

xentoollog_logger_stdiostream *xtl_createlogger_stdiostream
        (FILE *f, xentoollog_level min_level, unsigned flags) {
    xentoollog_logger_stdiostream newlogger;

    newlogger.f = f;
    newlogger.min_level = min_level;
    newlogger.flags = flags;
    newlogger.tty = isatty(fileno(newlogger.f)) > 0;

    if (newlogger.flags & XTL_STDIOSTREAM_SHOW_DATE) tzset();

    newlogger.progress_erase_len = 0;

    return XTL_NEW_LOGGER(stdiostream, newlogger);
}