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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
//
// EventLogger.c - MrsWatson
// Created by Nik Reiman on 1/2/12.
// Copyright (c) 2012 Teragon Audio. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "app/BuildInfo.h"
#include "audio/AudioSettings.h"
#include "logging/EventLogger.h"
#include "logging/LogPrinter.h"
#include "time/AudioClock.h"
#include "MrsWatson.h"
#if WINDOWS
#include <Windows.h>
#include <io.h>
#elif UNIX
#include <unistd.h>
#endif
EventLogger eventLoggerInstance = NULL;
void initEventLogger(void)
{
#if WINDOWS
ULONGLONG currentTime;
#else
struct timeval currentTime;
#endif
eventLoggerInstance = (EventLogger)malloc(sizeof(EventLoggerMembers));
eventLoggerInstance->logLevel = LOG_INFO;
eventLoggerInstance->logFile = NULL;
eventLoggerInstance->useColor = false;
eventLoggerInstance->zebraStripeSize = (unsigned long)DEFAULT_SAMPLE_RATE;
eventLoggerInstance->systemErrorMessage = NULL;
#if WINDOWS
currentTime = GetTickCount();
eventLoggerInstance->startTimeInSec = (unsigned long)(currentTime / 1000);
eventLoggerInstance->startTimeInMs = (unsigned long)currentTime;
#else
gettimeofday(¤tTime, NULL);
eventLoggerInstance->startTimeInSec = currentTime.tv_sec;
eventLoggerInstance->startTimeInMs = currentTime.tv_usec / 1000;
#endif
if (isatty(1)) {
eventLoggerInstance->useColor = true;
}
}
static EventLogger _getEventLoggerInstance(void)
{
return eventLoggerInstance;
}
char *stringForLastError(int errorNumber)
{
EventLogger eventLogger = _getEventLoggerInstance();
if (eventLogger->systemErrorMessage == NULL) {
eventLogger->systemErrorMessage = newCharString();
}
#if UNIX
return strerror(errorNumber);
#elif WINDOWS
FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, 0, errorNumber, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
eventLogger->systemErrorMessage->data, eventLogger->systemErrorMessage->capacity - 1, NULL);
return eventLogger->systemErrorMessage->data;
#endif
}
boolByte isLogLevelAtLeast(LogLevel logLevel)
{
EventLogger eventLogger = _getEventLoggerInstance();
return (boolByte)(logLevel >= eventLogger->logLevel);
}
void setLogLevel(LogLevel logLevel)
{
EventLogger eventLogger = _getEventLoggerInstance();
eventLogger->logLevel = logLevel;
}
void setLogLevelFromString(const CharString logLevelString)
{
if (charStringIsEqualToCString(logLevelString, "debug", true)) {
setLogLevel(LOG_DEBUG);
} else if (charStringIsEqualToCString(logLevelString, "info", true)) {
setLogLevel(LOG_INFO);
} else if (charStringIsEqualToCString(logLevelString, "warn", true)) {
setLogLevel(LOG_WARN);
} else if (charStringIsEqualToCString(logLevelString, "error", true)) {
setLogLevel(LOG_ERROR);
} else {
logCritical("Invalid log level '%s', see '--help full' for valid arguments", logLevelString->data);
}
}
void setLogFile(const CharString logFileName)
{
EventLogger eventLogger = _getEventLoggerInstance();
eventLogger->logFile = fopen(logFileName->data, "a");
if (eventLogger->logFile == NULL) {
logCritical("Could not open file '%s' for logging", logFileName->data);
} else {
eventLogger->useColor = false;
}
}
void setLoggingColorEnabled(boolByte useColor)
{
EventLogger eventLogger = _getEventLoggerInstance();
eventLogger->useColor = useColor;
}
void setLoggingColorEnabledWithString(const CharString colorSchemeName)
{
if (charStringIsEmpty(colorSchemeName)) {
setLoggingColorEnabled(false);
} else if (charStringIsEqualToCString(colorSchemeName, "none", false)) {
setLoggingColorEnabled(false);
} else if (charStringIsEqualToCString(colorSchemeName, "auto", false)) {
setLoggingColorEnabled((boolByte)isatty(1));
} else if (charStringIsEqualToCString(colorSchemeName, "force", false)) {
setLoggingColorEnabled(true);
} else {
// Use critical log level to avoid colors
logCritical("Unknown color scheme '%s'", colorSchemeName->data);
}
}
void setLoggingZebraSize(const unsigned long zebraStripeSize)
{
EventLogger eventLogger = _getEventLoggerInstance();
eventLogger->zebraStripeSize = zebraStripeSize;
}
static char _logLevelStatusChar(const LogLevel logLevel)
{
switch (logLevel) {
case LOG_DEBUG:
return 'D';
case LOG_INFO:
return '-';
case LOG_WARN:
return 'W';
case LOG_ERROR:
return 'E';
default:
return '!';
}
}
static LogColor _logLevelStatusColor(const LogLevel logLevel)
{
switch (logLevel) {
case LOG_DEBUG:
return COLOR_FG_DKGRAY;
case LOG_INFO:
return COLOR_RESET;
case LOG_WARN:
return COLOR_FG_YELLOW;
case LOG_ERROR:
return COLOR_FG_RED;
default:
return COLOR_RESET;
}
}
static LogColor _logTimeColor(void)
{
return COLOR_FG_CYAN;
}
static LogColor _logTimeZebraStripeColor(const long elapsedTime, const unsigned long zebraSizeInMs)
{
boolByte zebraState = (boolByte)((elapsedTime / zebraSizeInMs) % 2);
return zebraState ? COLOR_FG_OLIVE : COLOR_FG_GREEN;
}
static void _printMessage(const LogLevel logLevel, const long elapsedTimeInMs, const long numFramesProcessed, const char *message, const EventLogger eventLogger)
{
char *logString = (char *)malloc(sizeof(char) * kCharStringLengthLong);
if (eventLogger->useColor) {
snprintf(logString, kCharStringLengthLong, "%c ", _logLevelStatusChar(logLevel));
printToLog(_logLevelStatusColor(logLevel), eventLogger->logFile, logString);
snprintf(logString, kCharStringLengthLong, "%08ld ", numFramesProcessed);
printToLog(_logTimeZebraStripeColor(numFramesProcessed, eventLogger->zebraStripeSize),
eventLogger->logFile, logString);
snprintf(logString, kCharStringLengthLong, "%06ld ", elapsedTimeInMs);
printToLog(_logTimeColor(), eventLogger->logFile, logString);
printToLog(_logLevelStatusColor(logLevel), eventLogger->logFile, message);
} else {
snprintf(logString, kCharStringLengthLong, "%c %08ld %06ld %s", _logLevelStatusChar(logLevel), numFramesProcessed, elapsedTimeInMs, message);
printToLog(COLOR_NONE, eventLogger->logFile, logString);
}
flushLog(eventLogger->logFile);
free(logString);
}
static void _logMessage(const LogLevel logLevel, const char *message, va_list arguments)
{
long elapsedTimeInMs;
EventLogger eventLogger = _getEventLoggerInstance();
#if WINDOWS
ULONGLONG currentTime;
#else
struct timeval currentTime;
#endif
if (eventLogger != NULL && logLevel >= eventLogger->logLevel) {
CharString formattedMessage = newCharStringWithCapacity(kCharStringLengthDefault * 2);
vsnprintf(formattedMessage->data, formattedMessage->capacity, message, arguments);
#if WINDOWS
currentTime = GetTickCount();
elapsedTimeInMs = (unsigned long)(currentTime - eventLogger->startTimeInMs);
#else
gettimeofday(¤tTime, NULL);
elapsedTimeInMs = ((currentTime.tv_sec - (eventLogger->startTimeInSec + 1)) * 1000) +
(currentTime.tv_usec / 1000) + (1000 - eventLogger->startTimeInMs);
#endif
_printMessage(logLevel, elapsedTimeInMs, getAudioClock()->currentFrame, formattedMessage->data, eventLogger);
freeCharString(formattedMessage);
}
}
void logDebug(const char *message, ...)
{
va_list arguments;
va_start(arguments, message);
_logMessage(LOG_DEBUG, message, arguments);
va_end(arguments);
}
void logInfo(const char *message, ...)
{
va_list arguments;
va_start(arguments, message);
_logMessage(LOG_INFO, message, arguments);
va_end(arguments);
}
void logWarn(const char *message, ...)
{
va_list arguments;
va_start(arguments, message);
_logMessage(LOG_WARN, message, arguments);
va_end(arguments);
}
void logError(const char *message, ...)
{
va_list arguments;
va_start(arguments, message);
_logMessage(LOG_ERROR, message, arguments);
va_end(arguments);
}
void logCritical(const char *message, ...)
{
va_list arguments;
CharString formattedMessage = newCharString();
CharString wrappedMessage;
va_start(arguments, message);
// Instead of going through the common logging method, we always dump critical
// messages to stderr
vsnprintf(formattedMessage->data, formattedMessage->capacity, message, arguments);
wrappedMessage = charStringWrap(formattedMessage, 0);
fprintf(stderr, "ERROR: %s\n", wrappedMessage->data);
if (eventLoggerInstance != NULL && eventLoggerInstance->logFile != NULL) {
fprintf(eventLoggerInstance->logFile, "ERROR: %s\n", wrappedMessage->data);
}
freeCharString(formattedMessage);
freeCharString(wrappedMessage);
va_end(arguments);
}
void logInternalError(const char *message, ...)
{
va_list arguments;
CharString formattedMessage = newCharString();
va_start(arguments, message);
// Instead of going through the common logging method, we always dump critical messages to stderr
vsnprintf(formattedMessage->data, formattedMessage->capacity, message, arguments);
fprintf(stderr, "INTERNAL ERROR: %s\n", formattedMessage->data);
if (eventLoggerInstance != NULL && eventLoggerInstance->logFile != NULL) {
fprintf(eventLoggerInstance->logFile, "INTERNAL ERROR: %s\n", formattedMessage->data);
}
freeCharString(formattedMessage);
va_end(arguments);
fprintf(stderr, " This should not have happened. Please take a minute to report a bug.\n");
fprintf(stderr, " Support website: %s\n", SUPPORT_WEBSITE);
fprintf(stderr, " Support email: %s\n", SUPPORT_EMAIL);
}
void logUnsupportedFeature(const char *featureName)
{
fprintf(stderr, "UNSUPPORTED FEATURE: %s\n", featureName);
fprintf(stderr, " This feature is not yet supported. Please help us out and submit a patch! :)\n");
fprintf(stderr, " Project website: %s\n", PROJECT_WEBSITE);
fprintf(stderr, " Support email: %s\n", SUPPORT_EMAIL);
}
void logDeprecated(const char *functionName, const char *plugin)
{
logWarn("Call to deprecated function '%s' made by plugin '%s'", functionName, plugin);
}
void logFileError(const char *filename, const char *message)
{
logCritical("Could not parse file '%s'", filename);
logCritical("Got error message message: %s", message);
logPossibleBug("This file is either corrupt or was parsed incorrectly");
}
void logPossibleBug(const char *cause)
{
CharString extraText = newCharStringWithCString("If you believe this to be a \
bug in MrsWatson, please re-run the program with the --error-report option to \
generate a diagnostic report to send to support.");
CharString wrappedCause;
CharString wrappedExtraText;
wrappedCause = charStringWrap(newCharStringWithCString(cause), 0);
wrappedExtraText = charStringWrap(extraText, 0);
fprintf(stderr, "%s\n", wrappedCause->data);
fprintf(stderr, "%s\n", wrappedExtraText->data);
freeCharString(wrappedCause);
freeCharString(wrappedExtraText);
}
void flushErrorLog(void)
{
if (eventLoggerInstance != NULL && eventLoggerInstance->logFile != NULL) {
fflush(eventLoggerInstance->logFile);
}
}
void freeEventLogger(void)
{
if (eventLoggerInstance->logFile != NULL) {
fclose(eventLoggerInstance->logFile);
}
freeCharString(eventLoggerInstance->systemErrorMessage);
free(eventLoggerInstance);
eventLoggerInstance = NULL;
}
|