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. |
---|
4 | |
---|
5 | #include "log.h" |
---|
6 | #include <common/nonstd_stdio.h> |
---|
7 | #include "stl-util.h" |
---|
8 | #include "Convert.h" |
---|
9 | |
---|
10 | const char* LOG_LEVEL[] = { "[DEBUG] ", "", "[WARN] ", "[ERROR] ", "[CRITICAL] " }; |
---|
11 | |
---|
12 | void logMessage(const char *obj, const char *method, int level, const char *msg) |
---|
13 | { |
---|
14 | int line = 0; //all lines except the first one get the "..." prefix |
---|
15 | const char* nextsep; |
---|
16 | do |
---|
17 | { |
---|
18 | nextsep = strchr(msg, '\n'); |
---|
19 | if (nextsep == NULL) //last chunk, until the end |
---|
20 | nextsep = strchr(msg, '\0'); |
---|
21 | if ((nextsep > msg) && (nextsep[-1] == '\r')) |
---|
22 | nextsep--; |
---|
23 | if (line == 0) |
---|
24 | { |
---|
25 | if (*nextsep == 0) //there was only one line! no need to modify it in any way. |
---|
26 | _logMessageSingleLine(obj, method, level, msg); |
---|
27 | else //first line from multi-line |
---|
28 | _logMessageSingleLine(obj, method, level, string(msg, nextsep - msg).c_str()); |
---|
29 | } |
---|
30 | else //consecutive lines from multi-line |
---|
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 |
---|
32 | line++; |
---|
33 | if ((nextsep[0] == '\r') && (nextsep[1] == '\n')) |
---|
34 | msg = nextsep + 2; |
---|
35 | else if (*nextsep) |
---|
36 | msg = nextsep + 1; |
---|
37 | } while (*nextsep); |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | void logPrintf_va(const char *obj, const char *method, int level, const char *msgf, va_list va) |
---|
42 | { |
---|
43 | string buf = ssprintf_va(msgf, va); |
---|
44 | logMessage(obj, method, level, buf.c_str()); |
---|
45 | } |
---|
46 | |
---|
47 | void logPrintf(const char *obj, const char *method, int level, const char *msgf, ...) |
---|
48 | { |
---|
49 | va_list argptr; |
---|
50 | va_start(argptr, msgf); |
---|
51 | logPrintf_va(obj, method, level, msgf, argptr); |
---|
52 | va_end(argptr); |
---|
53 | } |
---|
54 | |
---|
55 | void log_printf(const char *msgf, ...) |
---|
56 | { |
---|
57 | va_list argptr; |
---|
58 | va_start(argptr, msgf); |
---|
59 | logPrintf_va("Message", "printf", LOG_INFO, msgf, argptr); |
---|
60 | va_end(argptr); |
---|
61 | } |
---|