|
| 1 | +/* BSD 2-Clause License |
| 2 | + * |
| 3 | + * Copyright (c) 2020, Andrea Giacomo Baldan All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * * Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | + * POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | + |
| 28 | +#include <time.h> |
| 29 | +#include <stdio.h> |
| 30 | +#include <stdarg.h> |
| 31 | +#include <string.h> |
| 32 | +#include <assert.h> |
| 33 | +#include "logging.h" |
| 34 | + |
| 35 | +#define MAX_LOG_SIZE 120 |
| 36 | + |
| 37 | +static FILE *fh = NULL; |
| 38 | +static int logging_level = DEBUG; |
| 39 | + |
| 40 | +void sol_log_init(const char *file, int level) { |
| 41 | + if (!file) return; |
| 42 | + fh = fopen(file, "a+"); |
| 43 | + logging_level = level; |
| 44 | + if (!fh) |
| 45 | + printf("%lu * WARNING: Unable to open file %s\n", |
| 46 | + (unsigned long) time(NULL), file); |
| 47 | +} |
| 48 | + |
| 49 | +void sol_log_close(void) { |
| 50 | + if (fh) { |
| 51 | + fflush(fh); |
| 52 | + fclose(fh); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +void sol_log(int level, const char *fmt, ...) { |
| 57 | + |
| 58 | + if (level < logging_level) |
| 59 | + return; |
| 60 | + |
| 61 | + assert(fmt); |
| 62 | + |
| 63 | + va_list ap; |
| 64 | + char msg[MAX_LOG_SIZE + 4]; |
| 65 | + |
| 66 | + va_start(ap, fmt); |
| 67 | + vsnprintf(msg, sizeof(msg), fmt, ap); |
| 68 | + va_end(ap); |
| 69 | + |
| 70 | + /* Truncate message too long and copy 3 bytes to make space for 3 dots */ |
| 71 | + memcpy(msg + MAX_LOG_SIZE, "...", 3); |
| 72 | + msg[MAX_LOG_SIZE + 3] = '\0'; |
| 73 | + |
| 74 | + // Open two handler, one for standard output and a second for the |
| 75 | + // persistent log file |
| 76 | + FILE *fp = stdout; |
| 77 | + |
| 78 | + if (!fp) |
| 79 | + return; |
| 80 | + |
| 81 | + fprintf(fp, "%lu %s\n", (unsigned long) time(NULL), msg); |
| 82 | + if (fh) |
| 83 | + fprintf(fh, "%lu %s\n", (unsigned long) time(NULL), msg); |
| 84 | +} |
0 commit comments