MCS  0.3.3-alpha7
Event.cc
1 // ----------------------------------------------------------------------^
2 // Copyright (C) 2004, 2005, 2006, 2007, 2008 Giorgio Calderone
3 // (mailto: <gcalderone@ifc.inaf.it>)
4 //
5 // This file is part of MCS.
6 //
7 // MCS is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
11 //
12 // MCS is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with MCS; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 //
21 // ----------------------------------------------------------------------$
22 
23 
24 #include "mcs.hh"
25 using namespace mcs;
26 
27 
28 void mcs::Event::init(string atFile, unsigned int atLine,
29  RetValue type, int code)
30 {
31  if (! flInitialized) {
32  memset(vmsg, 0, sizeof(vmsg));
33 
34  if (custom_init_vmsg)
35  (*custom_init_vmsg)(vmsg);
36 
37  init_vmsg();
38 
39  flInitialized = true;
40  }
41 
42  latFile = atFile;
43  latLine = atLine;
44  lcode = code ;
45  lsubcode = 0 ;
46  ltype = type ;
47 
48  buf[0] = '\0';
49 }
50 
51 
52 
53 mcs::Event::Event(string atFile, unsigned int atLine, RetValue type, int code,
54  string s1, string s2, string s3)
55 {
56  init(atFile, atLine, type, code);
57  sprintf(buf, vmsg[code], s1.c_str(), s2.c_str(), s3.c_str());
58  lmsg = buf;
59 }
60 
61 mcs::Event::Event(string atFile, unsigned int atLine, RetValue type, int code,
62  string s1, int i1)
63 {
64  init(atFile, atLine, type, code);
65  sprintf(buf, vmsg[code], s1.c_str(), i1);
66  lmsg = buf;
67  lsubcode = i1;
68 }
69 
70 mcs::Event::Event(string atFile, unsigned int atLine, RetValue type, int code,
71  int i1, string s1)
72 {
73  init(atFile, atLine, type, code);
74  sprintf(buf, vmsg[code], i1, s1.c_str());
75  lmsg = buf;
76 }
77 
78 mcs::Event::Event(string atFile, unsigned int atLine, RetValue type, int code,
79  int i1, int i2)
80 {
81  init(atFile, atLine, type, code);
82  sprintf(buf, vmsg[code], i1, i2);
83  lmsg = buf;
84  lsubcode = i2;
85 }
86 
87 
88 
90 {}
91 
93 {
94  return lcode;
95 }
96 
97 
98 
100 {
101  return ltype;
102 }
103 
105 {
106  return lmsg;
107 }
108 
109 
111 {
112  string lerror;
113  char buf[30];
114 
115  sprintf(buf, "%d", latLine);
116  lerror = latFile + ":" + string(buf);
117 
118  sprintf(buf, " (%d) ", lcode);
119 
120  return lerror;
121 }
122 
123 
125 {
126  return latFile;
127 }
128 
129 
130 unsigned int mcs::Event::line()
131 {
132  return latLine;
133 }
134 
135 
137 {
138  char buf[5];
139  sprintf(buf, "#%03d", code());
140  return (buf + msg());
141 }
142 
143 
144 
146 {
147  return lsubcode;
148 }
149 
151 {
152  lsubcode = subcode;
153  return *this;
154 }
155 
156 string mcs::Event::static_vmsg(unsigned int i)
157 {
158  string ret = "";
159 
160  if ((i>=0) && (i < MCS_VMSG_SIZE))
161  if (vmsg[i])
162  ret = string(vmsg[i]);
163 
164  return ret;
165 }
166 
167 
168 const char* mcs::Event::vmsg[MCS_VMSG_SIZE];
169 bool mcs::Event::flInitialized = false;
170 void (*mcs::Event::custom_init_vmsg) (const char* vmsg[MCS_VMSG_SIZE]) = NULL;
171 
172 
173 
174 
175 
176 
177 #ifndef DOXYGEN_SKIP
178 #if ENABLE_DEBUG
179 pthread_t mcs::Debug::thrs[MCS_DEBUG_NTHR];
180 bool mcs::Debug::thrmap[MCS_DEBUG_NTHR];
181 int mcs::Debug::dummy = mcs::Debug::init();
182 
183 int mcs::Debug::init()
184 {
185  int i;
186  for (i=0; i<MCS_DEBUG_NTHR; i++) {
187  thrs[i] = 0;
188  thrmap[i] = false;
189  }
190  return 0;
191 }
192 
193 mcs::Debug::Debug()
194 {
195  active = false;
196  pThis = 0;
197 }
198 
199 mcs::Debug::~Debug()
200 {}
201 
202 
203 void mcs::Debug::setDebug(string lclassName, void* lpthis,
204  bool lactive)
205 {
206  className = lclassName;
207  pThis = lpthis;
208  active = lactive;
209 }
210 
211 void mcs::Debug::setDebug()
212 { active = true; }
213 
214 
215 bool mcs::Debug::getDebugFlag()
216 { return active; }
217 
218 
219 #define DEBUG_MAXLENGTH 80
220 string mcs::Debug::debug(const char* file, int line, const char* function)
221 {
222  int i;
223  bool found = false;
224  for (i=0; i<MCS_DEBUG_NTHR; i++) {
225  if (thrmap[i] && thrs[i] == pthread_self()) {
226  found = true;
227  break;
228  }
229  }
230 
231  if (! found) {
232  for (i=0; i<MCS_DEBUG_NTHR; i++)
233  if (! thrmap[i]) {
234  thrmap[i] = true;
235  thrs[i] = pthread_self();
236  break;
237  }
238  }
239 
240 
241  char buf[DEBUG_MAXLENGTH];
242  sprintf(buf, "*** THR %02d, %p, %s:%04d %s:%s ", i, pThis, file, line,
243  className.c_str(), function);
244  buf[DEBUG_MAXLENGTH - 1] = 0;
245  return string(buf);
246 }
247 
248 
249 #endif //ENABLE_DEBUG
250 #endif
~Event()
Destructor.
Definition: Event.cc:89
string where()
Returns the place where the error occurred formatted as FILE:LINE (CODE).
Definition: Event.cc:110
Hold informations about an event.
Definition: mcs.hh:814
#define MCS_VMSG_SIZE
Size of the Event.vmsg static array.
Definition: mcs.hh:730
string codemsg()
Return the code and the message, formatted as MCS_PRE CODE MESSAGE.
Definition: Event.cc:136
string msg()
Returns the message.
Definition: Event.cc:104
RetValue
Return value for MCS routines.
Definition: mcs.hh:471
RetValue type()
Return the event type.
Definition: Event.cc:99
string file()
Source file name where the event occurred.
Definition: Event.cc:124
int subcode()
Return event subcode.
Definition: Event.cc:145
Main include file for all MCS based applications.
int code()
Returns the event code.
Definition: Event.cc:92
static string static_vmsg(unsigned int i)
Return the msg in the i-th position of the vmsg array.
Definition: Event.cc:156
static void(* custom_init_vmsg)(const char *vmsg[1000])
Pointer to a callback function to initialize custom messages in the vmsg static array.
Definition: mcs.hh:853
unsigned int line()
Source line number where the event occurred.
Definition: Event.cc:130
static bool flInitialized
Flag to tell if the init() method has been already called.
Definition: mcs.hh:847
static const char * vmsg[1000]
Static array of descriptive messages.
Definition: mcs.hh:841
Event(string atFile, unsigned int atLine, RetValue type, int code, string s1="", string s2="", string s3="")
Constructor with three optional strings.
Definition: Event.cc:53
Namespace for MCS library.

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018