[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[122] | 4 | |
---|
[375] | 5 | #include "log.h" |
---|
[109] | 6 | #include <common/nonstd_stdio.h> |
---|
[180] | 7 | #include "stl-util.h" |
---|
[109] | 8 | #include "Convert.h" |
---|
| 9 | |
---|
[375] | 10 | const char* LOG_LEVEL[] = { "[DEBUG] ", "", "[WARN] ", "[ERROR] ", "[CRITICAL] " }; |
---|
[109] | 11 | |
---|
[375] | 12 | void logMessage(const char *obj, const char *method, int level, const char *msg) |
---|
[244] | 13 | { |
---|
| 14 | int line = 0; //all lines except the first one get the "..." prefix |
---|
| 15 | const char* nextsep; |
---|
| 16 | do |
---|
| 17 | { |
---|
[375] | 18 | nextsep = strchr(msg, '\n'); |
---|
[244] | 19 | if (nextsep == NULL) //last chunk, until the end |
---|
[375] | 20 | nextsep = strchr(msg, '\0'); |
---|
| 21 | if ((nextsep > msg) && (nextsep[-1] == '\r')) |
---|
[244] | 22 | nextsep--; |
---|
| 23 | if (line == 0) |
---|
| 24 | { |
---|
| 25 | if (*nextsep == 0) //there was only one line! no need to modify it in any way. |
---|
[375] | 26 | _logMessageSingleLine(obj, method, level, msg); |
---|
[244] | 27 | else //first line from multi-line |
---|
[375] | 28 | _logMessageSingleLine(obj, method, level, string(msg, nextsep - msg).c_str()); |
---|
[244] | 29 | } |
---|
| 30 | else //consecutive lines from multi-line |
---|
[375] | 31 | _logMessageSingleLine(obj, method, level, (LOG_MULTILINE_CONTINUATION + string(msg, nextsep - msg)).c_str()); //could also add line numbers like ...(3)... but let's keep the prefix short and simple |
---|
[244] | 32 | line++; |
---|
| 33 | if ((nextsep[0] == '\r') && (nextsep[1] == '\n')) |
---|
[375] | 34 | msg = nextsep + 2; |
---|
[244] | 35 | else if (*nextsep) |
---|
[375] | 36 | msg = nextsep + 1; |
---|
[244] | 37 | } while (*nextsep); |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | |
---|
[375] | 41 | void logPrintf_va(const char *obj, const char *method, int level, const char *msgf, va_list va) |
---|
[109] | 42 | { |
---|
[375] | 43 | string buf = ssprintf_va(msgf, va); |
---|
| 44 | logMessage(obj, method, level, buf.c_str()); |
---|
[109] | 45 | } |
---|
| 46 | |
---|
[375] | 47 | void logPrintf(const char *obj, const char *method, int level, const char *msgf, ...) |
---|
[109] | 48 | { |
---|
| 49 | va_list argptr; |
---|
[375] | 50 | va_start(argptr, msgf); |
---|
| 51 | logPrintf_va(obj, method, level, msgf, argptr); |
---|
[109] | 52 | va_end(argptr); |
---|
| 53 | } |
---|
| 54 | |
---|
[375] | 55 | void log_printf(const char *msgf, ...) |
---|
[109] | 56 | { |
---|
| 57 | va_list argptr; |
---|
[375] | 58 | va_start(argptr, msgf); |
---|
| 59 | logPrintf_va("Message", "printf", LOG_INFO, msgf, argptr); |
---|
[109] | 60 | va_end(argptr); |
---|
| 61 | } |
---|