source: cpp/common/loggers/loggers.cpp @ 547

Last change on this file since 547 was 522, checked in by Maciej Komosinski, 8 years ago

Code formatting

  • Property svn:eol-style set to native
File size: 4.3 KB
Line 
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 "loggers.h"
6#include <common/stl-util.h>
7#include <string.h>
8
9void logMessage(const char *obj, const char *method, int level, const char *msg)
10{
11        tlsGetRef(message_handler_manager_instance).send(obj, method, level, msg);
12}
13
14THREAD_LOCAL_DEF(LoggerManager, message_handler_manager_instance);
15
16void LoggerManager::send(int position, const char *obj, const char *method, int level, const char *msg)
17{
18        if (position >= (int)loggers.size()) position = loggers.size() - 1;
19        bool blocked = false;
20        for (int i = position; i >= 0; i--)
21        {
22                LoggerBase *logger = loggers[i];
23                if ((!(logger->options & LoggerBase::Paused)) &&
24                        ((!blocked) || (logger->options & LoggerBase::CannotBeBlocked)))
25                {
26                        logger->handle(obj, method, level, msg);
27                        if (!(logger->options & LoggerBase::DontBlock)) blocked = true;
28                }
29        }
30}
31
32int LoggerManager::add(LoggerBase *logger)
33{
34        logger->manager = this;
35        loggers.push_back(logger);
36        return loggers.size() - 1;
37}
38
39void LoggerManager::remove(int i)
40{
41        LoggerBase *h = loggers[i];
42        h->manager = NULL;
43        loggers.erase(loggers.begin() + i);
44}
45
46void LoggerManager::remove(LoggerBase *logger)
47{
48        int index = find(logger);
49        if (index >= 0)
50                remove(index);
51}
52
53void LoggerManager::removeAll()
54{
55        while (loggers.size() > 0)
56                remove(loggers.size() - 1);
57}
58
59//////////////////////////////////
60
61void LoggerBase::send(const char *obj, const char *method, int level, const char *msg)
62{
63        if (!isEnabled()) return;
64        int position = manager->find(this);
65        if (position >= 0) manager->send(position - 1, obj, method, level, msg);
66}
67
68void LoggerBase::logPrintf(const char *obj, const char *method, int level, const char *msg, ...)
69{
70        if (!isEnabled()) return;
71        string buf;
72        va_list argptr;
73        va_start(argptr, msg);
74        buf = ssprintf_va(msg, argptr);
75        va_end(argptr);
76        send(obj, method, level, buf.c_str());
77}
78
79
80void LoggerBase::enable()
81{
82        if (isEnabled()) return;
83        tlsGetRef(message_handler_manager_instance).add(this);
84}
85
86void LoggerBase::disable()
87{
88        if (!isEnabled()) return;
89        tlsGetRef(message_handler_manager_instance).remove(this);
90}
91
92void LoggerBase::pause()
93{
94        if (isPaused()) return;
95        options |= Paused;
96}
97
98void LoggerBase::resume()
99{
100        if (!isPaused()) return;
101        options &= ~Paused;
102}
103
104void LoggerBase::handle(const char *obj, const char *method, int level, const char *msg)
105{
106        int line = 0; //all lines except the first one get the "..." prefix
107        const char* nextsep;
108        do
109        {
110                nextsep = strchr(msg, '\n');
111                if (nextsep == NULL) //last chunk, until the end
112                        nextsep = strchr(msg, '\0');
113                if ((nextsep > msg) && (nextsep[-1] == '\r'))
114                        nextsep--;
115                if (line == 0)
116                {
117                        if (*nextsep == 0) //there was only one line! no need to modify it in any way.
118                                handleSingleLine(obj, method, level, msg);
119                        else //first line from multi-line
120                                handleSingleLine(obj, method, level, string(msg, nextsep - msg).c_str());
121                }
122                else //consecutive lines from multi-line
123                        handleSingleLine(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
124                line++;
125                if ((nextsep[0] == '\r') && (nextsep[1] == '\n'))
126                        msg = nextsep + 2;
127                else if (*nextsep)
128                        msg = nextsep + 1;
129        } while (*nextsep);
130}
131
132/////////////////////////////////
133
134void LoggerToMemory::handle(const char *obj, const char *method, int level, const char *msg)
135{
136        if (level > maxlevel) maxlevel = level;
137        if (level >= LOG_INFO) infocount++;
138        if (level >= LOG_WARN) warncount++;
139        if (level >= LOG_ERROR) errcount++;
140
141        if (level >= minleveltostore)
142        {
143                storedcount++;
144                if (options & (StoreFirstMessage | StoreAllMessages))
145                {
146                        if (!((options&StoreFirstMessage) && (msgs.length() > 0)))
147                        {
148                                if (msgs.length() > 0) msgs += '\n';
149                                msgs += ssprintf(LOG_FORMAT, LOG_LEVEL[level + 1], obj, method, msg);
150                        }
151                }
152        }
153}
154
155string LoggerToMemory::getCountSummary() const
156{
157        if (getInfoCount())
158        {
159                string msg;
160                if (getErrorCount())
161                        msg = ssprintf("%d error(s)", getErrorCount());
162                int w;
163                if (w = getWarningCount() - getErrorCount())
164                        msg += ssprintf("%s%d warning(s)", (getErrorCount() ? ", " : ""), w);
165                if (w = getInfoCount() - getWarningCount())
166                        msg += ssprintf("%s%d message(s)", (getWarningCount() ? ", " : ""), w);
167                return msg;
168        }
169        return string("");
170}
171
Note: See TracBrowser for help on using the repository browser.