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

Last change on this file since 435 was 378, checked in by Maciej Komosinski, 10 years ago

Improved argument variable names; changed implementation to use STL classes (vector, string)

  • Property svn:eol-style set to native
File size: 2.9 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
8void _logMessageSingleLine(const char *obj, const char *method, int level, const char *msg)
9{
10        tlsGetRef(message_handler_manager_instance).send(obj, method, level, msg);
11}
12
13THREAD_LOCAL_DEF(LoggerManager, message_handler_manager_instance);
14
15void LoggerManager::send(int position, const char *obj, const char *method, int level, const char *msg)
16{
17        if (position >= (int)loggers.size()) position = loggers.size() - 1;
18        bool blocked = false;
19        for (int i = position; i >= 0; i--)
20        {
21                LoggerBase *logger = loggers[i];
22                if ((!(logger->options & LoggerBase::Paused)) &&
23                        ((!blocked) || (logger->options & LoggerBase::CannotBeBlocked)))
24                {
25                        logger->handle(obj, method, level, msg);
26                        if (!(logger->options & LoggerBase::DontBlock)) blocked = true;
27                }
28        }
29}
30
31int LoggerManager::add(LoggerBase *logger)
32{
33        logger->manager = this;
34        loggers.push_back(logger);
35        return loggers.size() - 1;
36}
37
38void LoggerManager::remove(int i)
39{
40        LoggerBase *h = loggers[i];
41        h->manager = NULL;
42        loggers.erase(loggers.begin() + i);
43}
44
45void LoggerManager::remove(LoggerBase *logger)
46{
47        int index = find(logger);
48        if (index >= 0)
49                remove(index);
50}
51
52void LoggerManager::removeAll()
53{
54        while (loggers.size() > 0)
55                remove(loggers.size() - 1);
56}
57
58//////////////////////////////////
59
60void LoggerBase::send(const char *obj, const char *method, int level, const char *msg)
61{
62        if (!isEnabled()) return;
63        int position = manager->find(this);
64        if (position >= 0) manager->send(position - 1, obj, method, level, msg);
65}
66
67void LoggerBase::logPrintf(const char *obj, const char *method, int level, const char *msg, ...)
68{
69        if (!isEnabled()) return;
70        string buf;
71        va_list argptr;
72        va_start(argptr, msg);
73        buf = ssprintf_va(msg, argptr);
74        va_end(argptr);
75        send(obj, method, level, buf.c_str());
76}
77
78
79void LoggerBase::enable()
80{
81        if (isEnabled()) return;
82        tlsGetRef(message_handler_manager_instance).add(this);
83}
84
85void LoggerBase::disable()
86{
87        if (!isEnabled()) return;
88        tlsGetRef(message_handler_manager_instance).remove(this);
89}
90
91void LoggerBase::pause()
92{
93        if (isPaused()) return;
94        options |= Paused;
95}
96
97void LoggerBase::resume()
98{
99        if (!isPaused()) return;
100        options &= ~Paused;
101}
102
103/////////////////////////////////
104
105void LoggerToMemory::handle(const char *obj, const char *method, int level, const char *msg)
106{
107        if (level > maxlevel) maxlevel = level;
108        if (level >= LOG_INFO) infocount++;
109        if (level >= LOG_WARN) warncount++;
110        if (level >= LOG_ERROR) errcount++;
111
112        if (level >= minleveltostore)
113        {
114                storedcount++;
115                if (options & (StoreFirstMessage | StoreAllMessages))
116                {
117                        if (!((options&StoreFirstMessage) && (msgs.length() > 0)))
118                        {
119                                if (msgs.length() > 0) msgs += '\n';
120                                msgs += ssprintf(LOG_FORMAT, LOG_LEVEL[level + 1], obj, method, msg);
121                        }
122                }
123        }
124}
Note: See TracBrowser for help on using the repository browser.