libpynq (release 5EWC0-2023 version 0.2.2 of 2023-09-24 22:22)
Loading...
Searching...
No Matches
log.c
Go to the documentation of this file.
1/*
2Copyright (c) 2023 Eindhoven University of Technology
3
4Permission is hereby granted, free of charge, to any person obtaining a copy
5of this software and associated documentation files (the "Software"), to deal
6in the Software without restriction, including without limitation the rights
7to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8copies of the Software, and to permit persons to whom the Software is
9furnished to do so, subject to the following conditions:
10
11The above copyright notice and this permission notice shall be included in all
12copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20SOFTWARE.
21*/
22#include <stdarg.h>
23#include <stdbool.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28#include "log.h"
29
31#define DOMAIN "LOGGER"
32
34static const char color_escape_calls[NUM_LOG_LEVELS][8] = {
36 "\033[1;32m",
38 "\033[1;33m",
40 "\033[1;31m"};
42static const char log_level_name[NUM_LOG_LEVELS][10] = {
43 "INFO: ", "WARNING: ", "ERROR: "};
45static const char color_escape_blue[] = "\033[1;34m";
46static const char color_escape_reset[] = "\033[0m";
47
48static bool pynq_log_init = false;
49static LogLevel critical_level = LOG_LEVEL_ERROR;
50static LogLevel min_log_level = LOG_LEVEL_WARNING;
51
52void pynq_log(const LogLevel level, char const *domain, char const *location,
53 unsigned int lineno, char const *fmt, ...) {
54 va_list arg_list;
55
56 // on first call, initialize based on input arguments
57 if (!pynq_log_init) {
58 // if DEBUG is set, we also print log level INFO
59 char const *env = getenv("DEBUG");
60 if (env != NULL) {
61 min_log_level = LOG_LEVEL_INFO;
62 }
63 // make warnings fatal
64 env = getenv("FATAL_WARNING");
65 if (env != NULL) {
66 critical_level = LOG_LEVEL_WARNING;
67 }
68 pynq_log_init = true;
69 }
70 // check if the log level is valid
71 if (level < LOG_LEVEL_INFO || level > LOG_LEVEL_ERROR) {
72 printf("pynq_log: invalid log level specified (%d)\r\n", level);
73 return;
74 }
75
76 if (level < min_log_level) {
77 return;
78 }
79 fputs(color_escape_calls[level], stderr);
80 fputs(log_level_name[level], stderr);
81
82 fputs(color_escape_blue, stderr);
83 if (domain != NULL) {
84 fprintf(stderr, "%s::", domain);
85 }
86 fprintf(stderr, "%s:%d ", location, lineno);
87 fputs(color_escape_reset, stderr);
88
89 va_start(arg_list, fmt);
90 vfprintf(stderr, fmt, arg_list);
91 va_end(arg_list);
92 if (fmt[strlen(fmt) - 1] != '\n') {
93 fputs("\n", stderr);
94 }
95
96 if (level >= critical_level) {
97 abort();
98 }
99}
void pynq_log(const LogLevel level, char const *domain, char const *location, unsigned int lineno, char const *fmt,...)
Definition log.c:52
LogLevel
Definition log.h:65
@ NUM_LOG_LEVELS
Definition log.h:73
@ LOG_LEVEL_ERROR
Definition log.h:71
@ LOG_LEVEL_WARNING
Definition log.h:69
@ LOG_LEVEL_INFO
Definition log.h:67