1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2021 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
4 | |
---|
5 | #include "loggertostdout.h" |
---|
6 | #include <common/console.h> |
---|
7 | #include <algorithm> |
---|
8 | #include <assert.h> |
---|
9 | #ifdef SHP |
---|
10 | #include <FBaseSys.h> //AppLog |
---|
11 | #else |
---|
12 | #include <stdio.h> |
---|
13 | #endif |
---|
14 | |
---|
15 | static const char* default_log_level_ansicolor[] = |
---|
16 | { "[\033[2mDEBUG\033[0m] \033[2m", "", "[\033[33mWARN\033[0m] \033[1m", "[\033[1;31mERROR\033[0m] \033[1m", "[\033[1;31mCRITICAL\033[0m] \033[1m" }; |
---|
17 | |
---|
18 | const char** LoggerToStdout::default_log_level[] = |
---|
19 | { LOG_LEVEL_ARRAY, default_log_level_ansicolor }; |
---|
20 | |
---|
21 | const char* LoggerToStdout::default_log_format[] = //note trailing %s (so it's easy to append "\n" later) |
---|
22 | { LOG_FORMAT "%s", "%s%s.%s: %s\033[0m%s" }; |
---|
23 | |
---|
24 | LoggerToStdout::LoggerToStdout(int opts, VirtFILE *_file) |
---|
25 | :LoggerBase(opts), file(_file) |
---|
26 | { |
---|
27 | log_format = default_log_format[console_color_mode]; |
---|
28 | log_level = default_log_level[console_color_mode]; |
---|
29 | } |
---|
30 | |
---|
31 | void LoggerToStdout::handleSingleLine(const char *obj, const char *method, int level, const char *msg) |
---|
32 | { |
---|
33 | #ifdef SHP |
---|
34 | AppLog(LOG_FORMAT "\n", logLevelName(level), obj, method, msg); |
---|
35 | #else |
---|
36 | if (file) |
---|
37 | file->printf(LOG_FORMAT "\n", logLevelName(level), obj, method, msg); |
---|
38 | else |
---|
39 | { |
---|
40 | assert((level>=LOG_MIN) && (level<=LOG_MAX)); |
---|
41 | level = std::min(LOG_MAX, std::max(LOG_MIN, level)); |
---|
42 | printf(log_format, log_level[level + 1], obj, method, msg, "\n"); |
---|
43 | } |
---|
44 | #endif |
---|
45 | } |
---|