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