File: msgmgr.cpp

package info (click to toggle)
ibutils 1.2-OFED-1.4.2-1.3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 23,616 kB
  • sloc: cpp: 153,349; ansic: 77,237; tcl: 13,447; sh: 11,852; makefile: 494; yacc: 333; lex: 169; awk: 53
file content (348 lines) | stat: -rw-r--r-- 10,374 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
 * Copyright (c) 2004 Mellanox Technologies LTD. All rights reserved.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * $Id$
 */

#include "msgmgr.h"
#include <string>
#include <stdio.h>
#include <string.h>

using namespace std;

//
// MESSAGE TYPE CLASS
//
msgType::msgType(char s, string &fmt, string ctx, string mod)
{
  severity = s;
  format = fmt;
  context = ctx;
  module = mod;

  // calc the number of fields
  int pos = 0;
  numFields = 0;
  while ((pos = 1 + format.find('$', pos)) > 0) numFields++;

  // limit to 6 :
  if (numFields > 6)
  {
    cerr << "-E- msgManager too many fields (>6) in msgType:" << fmt << endl;
    numFields = 6;
  }
}

//
// MESSAGE MANAGER CLASS
//

string msgManager::msg2string(
  msgObj msg,
  int externalVerbosity, // if not 0 will be used as the verbosity
  int errFatals)  // if not 0 will cerr any  FATAL err
{
  // find the message format:
  int_mtype_map::const_iterator mTypeI = types.find(msg.typeId);
  if (mTypeI == types.end())
  {
    cerr << "-E- Fail to find message type by id:" << msg.typeId << endl;
    return "";
  }

  msgType t = (*mTypeI).second;

  int vl;
  if (externalVerbosity == 0)
    vl = getVerbLevel(t.module);
  else
    vl = externalVerbosity;

  // filter by verbose level:
  if ( (t.severity == 'F' && (vl & MsgShowFatal)) ||
       (t.severity == 'E' && (vl & MsgShowError)) ||
       (t.severity == 'W' && (vl & MsgShowWarning)) ||
       (t.severity == 'I' && (vl & MsgShowInfo)) ||
       (t.severity == 'M' && (vl & MsgShowMads)) ||
       (t.severity == 'R' && (vl & MsgShowFrames)) ||
       (t.severity == 'V' && (vl & MsgShowVerbose)))
  {

    char res[1024];
    char tmp[1024];

    res[0] = '\0';

    // add time if required
    if (vl & MsgShowTime)
      sprintf(res, "[%09ld:%09ld]", msg.when.tv_sec, (long)msg.when.tv_usec);

    // start with severity
    sprintf(tmp, "-%c-", t.severity);
    strcat(res, tmp);

    // include parenthesis if either context or source
    if ((vl & MsgShowContext) || (vl & MsgShowSource) || (vl & MsgShowModule) )
      strcat(res,"(");

    // append the module if required:
    if (vl & MsgShowModule)
    {
      strcat(res, t.module.c_str());
      strcat(res, " ");
    }

    // append the context if required:
    if (vl & MsgShowContext)
      strcat(res, t.context.c_str());

    // append file and line if required:
    if (vl & MsgShowSource)
    {
      sprintf(tmp, " %s,%d", msg.inFile.c_str(), msg.lineNum);
      strcat(res, tmp);
    }

    // close the parent system or space
    if ((vl & MsgShowContext) || (vl & MsgShowSource) || (vl & MsgShowModule))
      strcat(res,") ");
    else
      strcat(res," ");

    // go over the format:
    int pos = 0, nextPos = 0;
    int numFields = 0;
    while ((nextPos = t.format.find('$', pos)) >= 0)
    {
      strcat(res,t.format.substr(pos,nextPos - pos).c_str());
      switch (++numFields)
      {
      case 1: strcat(res, msg.f1.c_str()); break;
      case 2: strcat(res, msg.f2.c_str()); break;
      case 3: strcat(res, msg.f3.c_str()); break;
      case 4: strcat(res, msg.f4.c_str()); break;
      case 5: strcat(res, msg.f5.c_str()); break;
      case 6: strcat(res, msg.f6.c_str()); break;
      }
      pos = nextPos + 1;
    }

    strcat(res, t.format.substr(pos).c_str());
    strcat(res, "\n");

    // send FATAL errors directly to the stderr
    if (errFatals && (t.severity == 'F') && (vl & MsgShowFatal))
      cerr << res;

    return res;
  }
  return "";
}

// get number of outstanding messages of the given severity
int msgManager::outstandingMsgCount(int vl) {
  // go over all messages past the pendingMsgsI iterator and count them if
  // match the given verbose level
  int cnt = 0;

  unsigned int I = pendingMsgsI;
  msgType t;
  pthread_mutex_lock(&lock);
  while (I < log.size())
  {
    // get the message type obj:
    t = types[(log[I]).typeId];
    if ( (t.severity == 'F' && (vl & MsgShowFatal)) ||
         (t.severity == 'E' && (vl & MsgShowError)) ||
         (t.severity == 'W' && (vl & MsgShowWarning)) ||
         (t.severity == 'I' && (vl & MsgShowInfo)) ||
         (t.severity == 'M' && (vl & MsgShowMads)) ||
         (t.severity == 'R' && (vl & MsgShowFrames)) ||
         (t.severity == 'V' && (vl & MsgShowVerbose)) ) cnt++;
    I++;
  }
  pthread_mutex_unlock(&lock);
  return cnt;
}

// get outstanding messages of the given severity
string msgManager::outstandingMsgs(int vl) {
  // go over all messages past the pendingMsgsI iterator and collect
  // match the given verbose level
  string res = "";

  pthread_mutex_lock(&lock);
  unsigned int I = pendingMsgsI;

  msgType t;
  while (I < log.size())
  {
    // get the message type obj:
    t = types[(log[I]).typeId];
    if ( (t.severity == 'F' && (vl & MsgShowFatal)) ||
         (t.severity == 'E' && (vl & MsgShowError)) ||
         (t.severity == 'W' && (vl & MsgShowWarning)) ||
         (t.severity == 'I' && (vl & MsgShowInfo)) ||
         (t.severity == 'M' && (vl & MsgShowMads)) ||
         (t.severity == 'R' && (vl & MsgShowFrames)) ||
         (t.severity == 'V' && (vl & MsgShowVerbose)) )
      res += msg2string(log[I], vl);
    I++;
  }
  pthread_mutex_unlock(&lock);
  return res;
}

// return the next message string if included in the given types
string msgManager::getNextMessage() {
  string res("");

  pthread_mutex_lock(&lock);

  if (pendingMsgsI == log.size() - 1)
    res = msg2string(log[pendingMsgsI++]);

  pthread_mutex_unlock(&lock);
  return res;
}

void  msgManager::nullOutstandingMsgs() {
  pthread_mutex_lock(&lock);

  if (log.size() == 0)
    pendingMsgsI = 0;
  else
    pendingMsgsI = log.size();

  pthread_mutex_unlock(&lock);

}

// declare a new message type - return the generator id:
int msgManager::reg(char s, string fmt, string ctx, string mod) {
  msgType t(s,fmt,ctx,mod); // create a msg type
  pthread_mutex_lock(&lock);
  int id = types.size() + 1;
  types[id] = t; // store in map
  pthread_mutex_unlock(&lock);
  return id;
}

// get a new message and do something with it
int msgManager::send(int typeId, string fn , int ln, msgStr i1 ,msgStr i2 ,msgStr i3 ,
                 msgStr i4, msgStr i5 ,msgStr i6)
{

  // create a new message
  msgObj msg(typeId, i1.s,i2.s,i3.s,i4.s,i5.s,i6.s,fn,ln);

  // store in the log
  pthread_mutex_lock(&lock);

  // We store all messages in the log vector - but we can void that for now
  // log.push_back(msg);

  // if the message if Fatal send it to stderr if we are logging to file.
  int sendFatalsToCerr = 0;
  if ((*outStreamP != cout) && (*outStreamP != cerr))
    sendFatalsToCerr = 1;

  // handle the new message
  if (outStreamP) {
    // default verbosity, err on fatal
    (*outStreamP) << msg2string(msg, 0, sendFatalsToCerr);
    outStreamP->flush();
  }

  // if we unlock after we stream we serialize the display
  pthread_mutex_unlock(&lock);


  return 0;
}

msgStr::msgStr(const char cp[])           {if (cp) {s = string(cp);};}
msgStr::msgStr(const int i)               {char b[8];  sprintf(b, "%d", i); s = string(b);}
msgStr::msgStr(const long int i)          {char b[8];  sprintf(b, "%ld", i);s = string(b);}
msgStr::msgStr(const float i)             {char b[16]; sprintf(b, "%f", i); s = string(b);}
msgStr::msgStr(const double i)            {char b[16]; sprintf(b, "%f", i); s = string(b);}
msgStr::msgStr(const unsigned long int i) {char b[16]; sprintf(b, "%lu", i);s = string(b);}
msgStr::msgStr(const unsigned int i)      {char b[8];  sprintf(b, "%u", i); s = string(b);}
msgStr::msgStr(const unsigned short i)    {char b[8];  sprintf(b, "%u", i); s = string(b);}
msgStr::msgStr(const unsigned long long i){char b[20];  sprintf(b, "0x%016llx", i); s = string(b);}

msgManager &msgMgr(int vl, std::ostream *o)
{
  static msgManager *pMgr = NULL;
  if (pMgr == NULL)
  {
    pMgr = new msgManager(vl, o);
  }

  return (*pMgr);
};

// we provide two global definitions for entering and leaving functions:
int msgMgrEnterFunc = msgMgr().reg('R',"$ [", "top", "msg");
int msgMgrLeaveFunc = msgMgr().reg('R',"$ ]", "top", "msg");

#ifdef MSG_MGR_TEST

int main(int argc, string **argv)
{
  msgMgr( MsgShowFatal | MsgShowError | MsgShowWarning, &cerr);
  MSGREG(err1,
         'E',  // severity
         "Fail to find node:$ for inst:$ of type:$",
         "noModule"
         );
  MSGREG(verb1,'V', "This verbose with param:$ bla","");
  msgMgr().send(err1,__FILE__,__LINE__,"n18","i1","output");
  MSGSND(verb1,"pv1");
  MSGSND(err1,"node","inst","type");
  static int mod1Err = msgMgr().reg('V', "This is module param:$", "main", "myModule");
  MSGREG(mod2Err,'V', "This is another module param:$", "otherModule");

  msgMgr().setVerbLevel(MsgShowAll, "myModule");
  msgMgr().send(mod1Err,__FILE__,__LINE__,"PARAM1");
  MSGSND(mod2Err,"PARAM2");
  MSGREG(err3, 'E', "This is error no param", "mySecondModule");
  MSGSND(err3);

  cout << "Getting all messages..." <<endl;
  msgMgr().clrVerbLevel("myModule");
  msgMgr().send(mod1Err,__FILE__,__LINE__,"PARAM1");

  cout << msgMgr().outstandingMsgs(MsgShowAll);
}

#endif