source: cpp/frams/errmgr/errmanager.h @ 366

Last change on this file since 366 was 336, checked in by Maciej Komosinski, 10 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.7 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#ifndef _ERRMANAGER_H_
6#define _ERRMANAGER_H_
7
8#include <frams/util/list.h>
9#include <frams/util/sstring.h>
10#include <common/framsg.h>
11#include <common/threads.h>
12
13class ErrorHandlerBase;
14
15class ErrorManager
16{
17        friend class ErrorHandlerBase;
18        SListTempl<ErrorHandlerBase*> handlers;
19        void send(int level, const char *o, const char *m, const char *bl, int w);
20public:
21        int find(ErrorHandlerBase *r) { return handlers.find(r); }
22        int add(ErrorHandlerBase *r);
23        void remove(int i);
24        void remove(ErrorHandlerBase *r);
25        void removeAll();
26        void send(const char *o, const char *m, const char *bl, int w)
27        {
28                send(handlers.size() - 1, o, m, bl, w);
29        }
30        ~ErrorManager() { removeAll(); }
31};
32
33extern THREAD_LOCAL_DECL(ErrorManager, errmgr_instance);
34
35////////////////////////////////////////
36
37class ErrorHandlerBase
38{
39        friend class ErrorManager;
40protected:
41        ErrorManager* mgr;
42        int options;
43
44public:
45
46        enum HandlerOptions
47        {
48                DontBlock = 1, CannotBeBlocked = 2, DontEnable = 4, Paused = 8
49        };
50
51        void FMprintf(const char *o, const char *m, int w, const char *bl, ...);
52        void send(const char *o, const char *m, const char *bl, int w);
53
54        bool isEnabled() { return mgr ? 1 : 0; }
55        void enable();
56        void disable();
57        bool isPaused() { return (options & Paused) != 0; }
58        void pause();
59        void resume();
60
61        ErrorHandlerBase(int opts = 0) :mgr(0), options(opts)
62        {
63                if (!(options&DontEnable)) enable();
64        }
65        virtual ~ErrorHandlerBase()
66        {
67                disable();
68        }
69
70        virtual void handle(const char *o, const char *m, const char *bl, int w) {}
71};
72
73///////////////////////////////////////////
74
75class ErrorHandler : public ErrorHandlerBase
76{
77protected:
78        int maxlevel, errcount, warncount, storlevel, storcount, infocount;
79        SString msgs;
80
81public:
82
83        void reset() { maxlevel = FMLV_INFO - 1; errcount = warncount = storcount = infocount = 0; msgs = 0; }
84
85        enum Options2
86        {
87                StoreFirstMessage = 16, StoreAllMessages = 32
88        };
89
90        int getErrorCount()       { return errcount; }
91        int getWarningCount()     { return warncount; }
92        int getInfoCount()        { return infocount; }
93        int getStoredCount()      { return storcount; }
94        int getErrorLevel()       { return maxlevel; }
95        const SString& getMessages() { return msgs; }
96
97        ErrorHandler(int opts = 0, int store = FMLV_ERROR) :ErrorHandlerBase(opts), storlevel(store)
98        {
99                reset();
100        }
101
102        void handle(const char *o, const char *m, const char *bl, int w);
103};
104
105class ErrorRedirector : public ErrorHandlerBase
106{
107        ErrorManager *other_mgr;
108public:
109        ErrorRedirector(ErrorManager *om)
110                :ErrorHandlerBase(), other_mgr(om) {}
111
112        void handle(const char *o, const char *m, const char *bl, int w)
113        {
114                other_mgr->send(o, m, bl, w);
115        }
116};
117
118#endif
Note: See TracBrowser for help on using the repository browser.