1 /*
2  * xtl_logger_stdio.c
3  *
4  * log message consumer that writes to stdio
5  *
6  * Copyright (c) 2010 Citrix
7  * Part of a generic logging interface used by various dom0 userland libraries.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation;
12  * version 2.1 of the License.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "xentoollog.h"
24 
25 #include <time.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <stdbool.h>
31 
32 struct xentoollog_logger_stdiostream {
33     xentoollog_logger vtable;
34     FILE *f;
35     xentoollog_level min_level;
36     unsigned flags;
37     int progress_erase_len, progress_last_percent;
38     bool progress_use_cr;
39 };
40 
progress_erase(xentoollog_logger_stdiostream * lg)41 static void progress_erase(xentoollog_logger_stdiostream *lg) {
42     if (lg->progress_erase_len)
43         fprintf(lg->f, "\r%*s\r", lg->progress_erase_len, "");
44 }
45 
stdiostream_vmessage(xentoollog_logger * logger_in,xentoollog_level level,int errnoval,const char * context,const char * format,va_list al)46 static void stdiostream_vmessage(xentoollog_logger *logger_in,
47                                  xentoollog_level level,
48                                  int errnoval,
49                                  const char *context,
50                                  const char *format,
51                                  va_list al) {
52     xentoollog_logger_stdiostream *lg = (void*)logger_in;
53 
54     if (level < lg->min_level)
55         return;
56 
57     progress_erase(lg);
58 
59     if (lg->flags & XTL_STDIOSTREAM_SHOW_DATE) {
60         struct tm lt_buf;
61         time_t now = time(0);
62         struct tm *lt= localtime_r(&now, &lt_buf);
63         if (lt != NULL)
64             fprintf(lg->f, "%04d-%02d-%02d %02d:%02d:%02d %s ",
65                     lt->tm_year+1900, lt->tm_mon+1, lt->tm_mday,
66                     lt->tm_hour, lt->tm_min, lt->tm_sec,
67                     tzname[!!lt->tm_isdst]);
68         else
69             fprintf(lg->f, "[localtime_r failed: %d] ", errno);
70     }
71     if (lg->flags & XTL_STDIOSTREAM_SHOW_PID)
72         fprintf(lg->f, "[%lu] ", (unsigned long)getpid());
73 
74     if (context)
75         fprintf(lg->f, "%s: ", context);
76 
77     fprintf(lg->f, "%s: ", xtl_level_to_string(level));
78 
79     vfprintf(lg->f, format, al);
80 
81     if (errnoval >= 0)
82         fprintf(lg->f, ": %s", strerror(errnoval));
83 
84     putc('\n', lg->f);
85     fflush(lg->f);
86 }
87 
stdiostream_message(struct xentoollog_logger * logger_in,xentoollog_level level,const char * context,const char * format,...)88 static void stdiostream_message(struct xentoollog_logger *logger_in,
89                                 xentoollog_level level,
90                                 const char *context,
91                                 const char *format, ...)
92 {
93     va_list al;
94     va_start(al,format);
95     stdiostream_vmessage(logger_in, level, -1, context, format, al);
96     va_end(al);
97 }
98 
stdiostream_progress(struct xentoollog_logger * logger_in,const char * context,const char * doing_what,int percent,unsigned long done,unsigned long total)99 static void stdiostream_progress(struct xentoollog_logger *logger_in,
100                                  const char *context,
101                                  const char *doing_what, int percent,
102                                  unsigned long done, unsigned long total) {
103     xentoollog_logger_stdiostream *lg = (void*)logger_in;
104     int newpel, extra_erase;
105     xentoollog_level this_level;
106 
107     if (lg->flags & XTL_STDIOSTREAM_HIDE_PROGRESS)
108         return;
109 
110     if (percent < lg->progress_last_percent) {
111         this_level = XTL_PROGRESS;
112     } else if (percent == lg->progress_last_percent) {
113         return;
114     } else if (percent < lg->progress_last_percent + 5) {
115         this_level = XTL_DETAIL;
116     } else {
117         this_level = XTL_PROGRESS;
118     }
119 
120     if (this_level < lg->min_level)
121         return;
122 
123     lg->progress_last_percent = percent;
124 
125     if (!lg->progress_use_cr) {
126         stdiostream_message(logger_in, this_level, context,
127                             "%s: %lu/%lu  %3d%%",
128                             doing_what, done, total, percent);
129         return;
130     }
131 
132     if (lg->progress_erase_len)
133         putc('\r', lg->f);
134 
135     newpel = fprintf(lg->f, "%s%s" "%s: %lu/%lu  %3d%%%s",
136                      context?context:"", context?": ":"",
137                      doing_what, done, total, percent,
138                      done == total ? "\n" : "");
139 
140     extra_erase = lg->progress_erase_len - newpel;
141     if (extra_erase > 0)
142         fprintf(lg->f, "%*s\r", extra_erase, "");
143 
144     lg->progress_erase_len = newpel;
145 }
146 
stdiostream_destroy(struct xentoollog_logger * logger_in)147 static void stdiostream_destroy(struct xentoollog_logger *logger_in) {
148     xentoollog_logger_stdiostream *lg = (void*)logger_in;
149     progress_erase(lg);
150     free(lg);
151 }
152 
xtl_stdiostream_set_minlevel(xentoollog_logger_stdiostream * lg,xentoollog_level min_level)153 void xtl_stdiostream_set_minlevel(xentoollog_logger_stdiostream *lg,
154                                   xentoollog_level min_level) {
155     lg->min_level = min_level;
156 }
157 
xtl_stdiostream_adjust_flags(xentoollog_logger_stdiostream * lg,unsigned set_flags,unsigned clear_flags)158 void xtl_stdiostream_adjust_flags(xentoollog_logger_stdiostream *lg,
159                                   unsigned set_flags, unsigned clear_flags) {
160     unsigned new_flags = (lg->flags & ~clear_flags) | set_flags;
161     if (new_flags & XTL_STDIOSTREAM_HIDE_PROGRESS)
162         progress_erase(lg);
163     lg->flags = new_flags;
164 }
165 
xtl_createlogger_stdiostream(FILE * f,xentoollog_level min_level,unsigned flags)166 xentoollog_logger_stdiostream *xtl_createlogger_stdiostream
167         (FILE *f, xentoollog_level min_level, unsigned flags) {
168     xentoollog_logger_stdiostream newlogger;
169 
170     newlogger.f = f;
171     newlogger.min_level = min_level;
172     newlogger.flags = flags;
173 
174     switch (flags & (XTL_STDIOSTREAM_PROGRESS_USE_CR |
175                      XTL_STDIOSTREAM_PROGRESS_NO_CR)) {
176     case XTL_STDIOSTREAM_PROGRESS_USE_CR: newlogger.progress_use_cr = 1; break;
177     case XTL_STDIOSTREAM_PROGRESS_NO_CR:  newlogger.progress_use_cr = 0; break;
178     case 0:
179         newlogger.progress_use_cr = isatty(fileno(newlogger.f)) > 0;
180         break;
181     default:
182         errno = EINVAL;
183         return 0;
184     }
185 
186     if (newlogger.flags & XTL_STDIOSTREAM_SHOW_DATE) tzset();
187 
188     newlogger.progress_erase_len = 0;
189     newlogger.progress_last_percent = 0;
190 
191     return XTL_NEW_LOGGER(stdiostream, newlogger);
192 }
193 
194 /*
195  * Local variables:
196  * mode: C
197  * c-file-style: "BSD"
198  * c-basic-offset: 4
199  * tab-width: 4
200  * indent-tabs-mode: nil
201  * End:
202  */
203