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

Last change on this file since 201 was 197, checked in by Maciej Komosinski, 11 years ago

GDK used by developers since 1999, distributed on the web since 2002

  • Property svn:eol-style set to native
File size: 2.5 KB
RevLine 
[121]1// This file is a part of the Framsticks GDK.
[197]2// Copyright (C) 1999-2014  Maciej Komosinski and Szymon Ulatowski.  See LICENSE.txt for details.
[109]3// Refer to http://www.framsticks.com/ for further information.
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{
17friend class ErrorHandlerBase;
18SListTempl<ErrorHandlerBase*> handlers;
19void send(int level,const char *o,const char *m,const char *bl,int w);
20  public:
21int find(ErrorHandlerBase *r) {return handlers.find(r);}
22int add(ErrorHandlerBase *r);
23void remove(int i);
24void remove(ErrorHandlerBase *r);
25void removeAll();
26void send(const char *o,const char *m,const char *bl,int w)
27        {send(handlers.size()-1,o,m,bl,w);}
28~ErrorManager() {removeAll();}
29};
30
31extern THREAD_LOCAL_DECL(ErrorManager,errmgr_instance);
32
33////////////////////////////////////////
34
35class ErrorHandlerBase
36{
37friend class ErrorManager;
38  protected:
39ErrorManager* mgr;
40int options;
41
42  public:
43
44enum HandlerOptions
45 { DontBlock=1, CannotBeBlocked=2, DontEnable=4 };
46
47void FMprintf(const char *o,const char *m,int w,const char *bl, ...);
48void send(const char *o,const char *m,const char *bl,int w);
49
50bool isEnabled() {return mgr?1:0;}
51void enable();
52void disable();
53
54ErrorHandlerBase(int opts=0):mgr(0),options(opts)
55        {if (!(options&DontEnable)) enable();}
56virtual ~ErrorHandlerBase()
57        {disable();}
58
59virtual void handle(const char *o,const char *m,const char *bl,int w) {}
60};
61
62///////////////////////////////////////////
63
64class ErrorHandler: public ErrorHandlerBase
65{
66  protected:
67int maxlevel,errcount,warncount,storlevel,storcount,infocount;
68SString msgs;
69
70  public:
71
72void reset() {maxlevel=FMLV_INFO-1; errcount=warncount=storcount=infocount=0; msgs=0;}
73
74enum Options2
75 { StoreFirstMessage=8, StoreAllMessages=16 };
76
77int getErrorCount()       {return errcount;}
78int getWarningCount()     {return warncount;}
79int getInfoCount()        {return infocount;}
80int getStoredCount()      {return storcount;}
81int getErrorLevel()       {return maxlevel;}
82const SString& getMessages() {return msgs;}
83
84ErrorHandler(int opts=0,int store=FMLV_ERROR):ErrorHandlerBase(opts),storlevel(store)
85        {reset();}
86
87void handle(const char *o,const char *m,const char *bl,int w);
88};
89
90class ErrorRedirector: public ErrorHandlerBase
91{
92        ErrorManager *other_mgr;
93public:
94        ErrorRedirector(ErrorManager *om)
95                :ErrorHandlerBase(),other_mgr(om) {}
96
97        void handle(const char *o,const char *m,const char *bl,int w)
98                {
99                other_mgr->send(o,m,bl,w);
100                }
101};
102
103#endif
104
Note: See TracBrowser for help on using the repository browser.