libpynq  (release 5EID0-2023 version 0.3.0 of 2024-04-25 09:42 )
log.c
Go to the documentation of this file.
1 /*
2 Copyright (c) 2023 Eindhoven University of Technology
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
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 
34 static const char color_escape_calls[NUM_LOG_LEVELS][8] = {
36  "\033[1;32m",
38  "\033[1;33m",
40  "\033[1;31m"};
42 static const char log_level_name[NUM_LOG_LEVELS][10] = {
43  "INFO: ", "WARNING: ", "ERROR: "};
45 static const char color_escape_blue[] = "\033[1;34m";
46 static const char color_escape_reset[] = "\033[0m";
47 
48 static bool pynq_log_init = false;
49 static LogLevel critical_level = LOG_LEVEL_ERROR;
50 static LogLevel min_log_level = LOG_LEVEL_WARNING;
51 
52 void 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 }
NUM_LOG_LEVELS
@ NUM_LOG_LEVELS
Definition: log.h:73
pynq_log
void pynq_log(const LogLevel level, char const *domain, char const *location, unsigned int lineno, char const *fmt,...)
Definition: log.c:11
LogLevel
LogLevel
Definition: log.h:65
LOG_LEVEL_ERROR
@ LOG_LEVEL_ERROR
Definition: log.h:71
LOG_LEVEL_INFO
@ LOG_LEVEL_INFO
Definition: log.h:67
log.h
LOG_LEVEL_WARNING
@ LOG_LEVEL_WARNING
Definition: log.h:69