1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/*
* yosys -- Yosys Open SYnthesis Suite
*
* Copyright (C) 2012 Clifford Wolf <clifford@clifford.at>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
#include "kernel/log.h"
#include "backends/ilang/ilang_backend.h"
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <vector>
#include <list>
std::vector<FILE*> log_files;
FILE *log_errfile = NULL;
bool log_time = false;
bool log_cmd_error_throw = false;
int log_verbose_level;
std::vector<int> header_count;
std::list<std::string> string_buf;
static struct timeval initial_tv = { 0, 0 };
static bool next_print_log = false;
std::string stringf(const char *fmt, ...)
{
std::string string;
char *str = NULL;
va_list ap;
va_start(ap, fmt);
if (vasprintf(&str, fmt, ap) < 0)
str = NULL;
va_end(ap);
if (str != NULL) {
string = str;
free(str);
}
return string;
}
void logv(const char *format, va_list ap)
{
if (log_time) {
while (format[0] == '\n' && format[1] != 0) {
format++;
log("\n");
}
if (next_print_log || initial_tv.tv_sec == 0) {
next_print_log = false;
struct timeval tv;
gettimeofday(&tv, NULL);
if (initial_tv.tv_sec == 0)
initial_tv = tv;
if (tv.tv_usec < initial_tv.tv_usec) {
tv.tv_sec--;
tv.tv_usec += 1000000;
}
tv.tv_sec -= initial_tv.tv_sec;
tv.tv_usec -= initial_tv.tv_usec;
log("[%05d.%06d] ", int(tv.tv_sec), int(tv.tv_usec));
}
if (format[0] && format[strlen(format)-1] == '\n')
next_print_log = true;
}
for (auto f : log_files) {
va_list aq;
va_copy(aq, ap);
vfprintf(f, format, aq);
va_end(aq);
}
}
void logv_header(const char *format, va_list ap)
{
log("\n");
if (header_count.size() > 0)
header_count.back()++;
for (int c : header_count)
log("%d.", c);
log(" ");
logv(format, ap);
log_flush();
if (int(header_count.size()) <= log_verbose_level && log_errfile != NULL) {
for (int c : header_count)
fprintf(log_errfile, "%d.", c);
fprintf(log_errfile, " ");
vfprintf(log_errfile, format, ap);
fflush(log_errfile);
}
}
void logv_error(const char *format, va_list ap)
{
log("ERROR: ");
logv(format, ap);
if (log_errfile != NULL) {
fprintf(log_errfile, "ERROR: ");
vfprintf(log_errfile, format, ap);
}
log_flush();
exit(1);
}
void log(const char *format, ...)
{
va_list ap;
va_start(ap, format);
logv(format, ap);
va_end(ap);
}
void log_header(const char *format, ...)
{
va_list ap;
va_start(ap, format);
logv_header(format, ap);
va_end(ap);
}
void log_error(const char *format, ...)
{
va_list ap;
va_start(ap, format);
logv_error(format, ap);
}
void log_cmd_error(const char *format, ...)
{
va_list ap;
va_start(ap, format);
if (log_cmd_error_throw) {
log("ERROR: ");
logv(format, ap);
log_flush();
throw 0;
}
logv_error(format, ap);
}
void log_push()
{
header_count.push_back(0);
}
void log_pop()
{
header_count.pop_back();
string_buf.clear();
log_flush();
}
void log_reset_stack()
{
while (header_count.size() > 1)
header_count.pop_back();
string_buf.clear();
log_flush();
}
void log_flush()
{
for (auto f : log_files)
fflush(f);
}
const char *log_signal(const RTLIL::SigSpec &sig, bool autoint)
{
char *ptr;
size_t size;
FILE *f = open_memstream(&ptr, &size);
ILANG_BACKEND::dump_sigspec(f, sig, autoint);
fputc(0, f);
fclose(f);
string_buf.push_back(ptr);
free(ptr);
return string_buf.back().c_str();
}
|