source: cpp/frams/errmgr/errmanager.cpp @ 336

Last change on this file since 336 was 336, checked in by Maciej Komosinski, 9 years ago

Error message formatting: - "[x] x::x - " -> "[x] x.x: "
Error managers support configurable multi-line "call stack" messages

  • Property svn:eol-style set to native
File size: 2.6 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 "errmanager.h"
6#include <common/stl-util.h>
7
8void _FramMessageSingleLine(const char *o, const char *m, const char *txt, int w)
9{
10        tlsGetRef(errmgr_instance).send(o, m, txt, w);
11}
12
13THREAD_LOCAL_DEF(ErrorManager, errmgr_instance);
14
15void ErrorManager::send(int level, const char *o, const char *m, const char *bl, int w)
16{
17        if (level >= handlers.size()) level = handlers.size() - 1;
18        bool blocked = 0;
19        for (int i = level; i >= 0; i--)
20        {
21                ErrorHandlerBase *r = handlers(i);
22                if ((!(r->options & ErrorHandlerBase::Paused)) &&
23                        ((!blocked) || (r->options & ErrorHandlerBase::CannotBeBlocked)))
24                {
25                        r->handle(o, m, bl, w);
26                        if (!(r->options & ErrorHandlerBase::DontBlock)) blocked = 1;
27                }
28        }
29}
30
31int ErrorManager::add(ErrorHandlerBase *h)
32{
33        h->mgr = this;
34        handlers += h;
35        return handlers.size() - 1;
36}
37
38void ErrorManager::remove(int i)
39{
40        ErrorHandlerBase *h = handlers(i);
41        h->mgr = 0;
42        handlers.remove(i);
43}
44
45void ErrorManager::remove(ErrorHandlerBase *h)
46{
47        int i;
48        if ((i = handlers.find(h)) < 0) return;
49        remove(i);
50}
51
52void ErrorManager::removeAll()
53{
54        while (handlers.size() > 0)
55                remove(handlers.size() - 1);
56}
57
58//////////////////////////////////
59
60void ErrorHandlerBase::send(const char *o, const char *m, const char *bl, int w)
61{
62        if (!isEnabled()) return;
63        int level = mgr->find(this);
64        if (level >= 0) mgr->send(level - 1, o, m, bl, w);
65}
66
67void ErrorHandlerBase::FMprintf(const char *o, const char *m, int w, const char *bl, ...)
68{
69        if (!isEnabled()) return;
70        string buf;
71        va_list argptr;
72        va_start(argptr, bl);
73        buf = ssprintf_va(bl, argptr);
74        va_end(argptr);
75        send(o, m, buf.c_str(), w);
76}
77
78
79void ErrorHandlerBase::enable()
80{
81        if (isEnabled()) return;
82        tlsGetRef(errmgr_instance).add(this);
83}
84
85void ErrorHandlerBase::disable()
86{
87        if (!isEnabled()) return;
88        tlsGetRef(errmgr_instance).remove(this);
89}
90
91void ErrorHandlerBase::pause()
92{
93        if (isPaused()) return;
94        options |= Paused;
95}
96
97void ErrorHandlerBase::resume()
98{
99        if (!isPaused()) return;
100        options &= ~Paused;
101}
102
103/////////////////////////////////
104
105void ErrorHandler::handle(const char *o, const char *m, const char *bl, int w)
106{
107        if (w > maxlevel) maxlevel = w;
108        if (w >= FMLV_INFO) infocount++;
109        if (w >= FMLV_WARN) warncount++;
110        if (w >= FMLV_ERROR) errcount++;
111
112        if (w >= storlevel)
113        {
114                storcount++;
115                if (options & (StoreFirstMessage | StoreAllMessages))
116                {
117                        if (!((options&StoreFirstMessage) && (msgs.len() > 0)))
118                        {
119                                if (msgs.len() > 0) msgs += '\n';
120                                msgs += o; msgs += "::"; msgs += m;
121                                msgs += " - "; msgs += bl;
122                        }
123                }
124        }
125}
Note: See TracBrowser for help on using the repository browser.