| 1 |
/* |
| 2 |
* xtl_core.c |
| 3 |
* |
| 4 |
* core code including functions for generating log messages |
| 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 <assert.h> |
| 26 |
#include <errno.h> |
| 27 |
#include <limits.h> |
| 28 |
|
| 29 |
static const char *level_strings[XTL_NUM_LEVELS]= { |
| 30 |
"[BUG:XTL_NONE]", |
| 31 |
"debug", "verbose", "detail", /* normally off by default */ |
| 32 |
"progress", "info", "notice", /* not a problem */ |
| 33 |
"warning", "error", "critical" /* problems and errors */ |
| 34 |
}; |
| 35 |
|
| 36 |
const char *xtl_level_to_string(xentoollog_level level) { |
| 37 |
assert(level >= 0 && level < XTL_NUM_LEVELS); |
| 38 |
return level_strings[level]; |
| 39 |
} |
| 40 |
|
| 41 |
void xtl_logv(struct xentoollog_logger *logger, |
| 42 |
xentoollog_level level, |
| 43 |
int errnoval /* or -1 */, |
| 44 |
const char *context /* eg "xc", "xenstore", "xl" */, |
| 45 |
const char *format /* does not contain \n */, |
| 46 |
va_list al) { |
| 47 |
int errno_save = errno; |
| 48 |
assert(level > XTL_NONE && level < XTL_NUM_LEVELS); |
| 49 |
logger->vmessage(logger,level,errnoval,context,format,al); |
| 50 |
errno = errno_save; |
| 51 |
} |
| 52 |
|
| 53 |
void xtl_log(struct xentoollog_logger *logger, |
| 54 |
xentoollog_level level, |
| 55 |
int errnoval /* or -1 */, |
| 56 |
const char *context /* eg "xc", "xenstore", "xl" */, |
| 57 |
const char *format /* does not contain \n */, |
| 58 |
...) { |
| 59 |
va_list al; |
| 60 |
va_start(al,format); |
| 61 |
xtl_logv(logger,level,errnoval,context,format,al); |
| 62 |
va_end(al); |
| 63 |
} |
| 64 |
|
| 65 |
void xtl_progress(struct xentoollog_logger *logger, |
| 66 |
const char *context, const char *doing_what, |
| 67 |
unsigned long done, unsigned long total) { |
| 68 |
int percent = 0; |
| 69 |
|
| 70 |
if (!logger->progress) return; |
| 71 |
|
| 72 |
if ( total ) |
| 73 |
percent = (total < LONG_MAX/100) |
| 74 |
? (done * 100) / total |
| 75 |
: done / ((total + 99) / 100); |
| 76 |
|
| 77 |
logger->progress(logger, context, doing_what, percent, done, total); |
| 78 |
} |
| 79 |
|
| 80 |
void xtl_logger_destroy(struct xentoollog_logger *logger) { |
| 81 |
if (!logger) return; |
| 82 |
logger->destroy(logger); |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* Local variables: |
| 87 |
* mode: C |
| 88 |
* c-file-style: "BSD" |
| 89 |
* c-basic-offset: 4 |
| 90 |
* tab-width: 4 |
| 91 |
* indent-tabs-mode: nil |
| 92 |
* End: |
| 93 |
*/ |