EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
msg_buffer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file msg_buffer.cc
1 
2 #include "msg_buffer.h"
3 #include <strnstr.h>
4 
5 // in the constructor we allocate the specified amount of bytes for
6 // the output array.
7 
8 msg_buffer::msg_buffer(const int msglen)
9 {
10  maximum_position = msglen;
11  oBuffer = new char[msglen];
12  for (int i=0; i < msglen; i++)
13  {
14  oBuffer[i] = '\0';
15  }
16 
17  pos =0;
18 
19  m = new msg_control(0,0,0);
20  m->activate();
21 
22 }
23 
24 // in the destructor, we deallocte the memory again
26 {
27  delete [] oBuffer;
28  if (m)
29  {
30  m->deactivate();
31  delete m;
32  }
33 }
34 
35 // this is the function which is called as a result of the << std::endl
36 // and flush() operations
38 {
39  pos = 0;
40  return 0;
41 }
42 
43 
44 
45 // and this routine
47 {
48 
49  if (pos >= maximum_position) // oops, our buffer is too small...
50  {
51  int oldmaximum_position=maximum_position;
52  maximum_position += MSG_EXTENSION_AMOUNT; // we extend it
53  char * n = new char[maximum_position];
54  for (int ix =0; ix < oldmaximum_position; ix++) n[ix] = oBuffer[ix];
55  for(int ix=oldmaximum_position;ix<maximum_position;ix++)n[ix]='\0';
56  delete [] oBuffer; // get rid of too small array
57  oBuffer = n; // and put new array in its place
58  }
59 
60  oBuffer[pos++] = ch;
61 
62  return 0;
63 }
64 
65 // and this routine
67 {
68 
69  char *c, *s, *n,*src;
70  int remainingLength = pos;
71  int srcmsglen = 0;
72 
73  if ( ! (c = strnstr(oBuffer,remainingLength,"<|",2) ) )
74  {
75  mp->type = MSG_TYPE_DEFAULT;
78  mp->reserved1 = 0;
79  mp->reserved2 = 0;
80  mp->reserved3 = 0;
81  // mp->sourcecomponent = new char[strlen("ONLINE")+1 ];
82  // strcpy(mp->sourcecomponent,"ONLINE");
83  strcpy(mp->sourcecomponent,"ONLINE");
84  *length = pos;
85  c = oBuffer;
86  n = new char[pos+1];
87  s = n;
88  for (int i=0; i<*length; i++) *s++ = *c++;
89  n[pos] = '\0';
90  return n;
91 
92  }
93 
94  remainingLength = pos - (int) ( c-oBuffer);
95  // extract message type
96  if ( ( s = strnstr(c,remainingLength,"|A" , 2) ) )
97  {
98  sscanf (s+2, "%d", &mp->type);
99  }
100  else mp->type = MSG_TYPE_DEFAULT;
101 
102  // extract message source
103  if ( ( s = strnstr(c,remainingLength,"|B" , 2) ) )
104  {
105  sscanf (s+2, "%d", &mp->source);
106  }
107  else mp->source = MSG_SOURCE_DEFAULT;
108 
109  // extract message severity
110  if ( ( s = strnstr(c,remainingLength,"|C" , 2) ) )
111  {
112  sscanf (s+2, "%d", &mp->severity);
113  }
114  else mp->severity = MSG_SEV_DEFAULT;
115 
116  // extract message reserved1
117  if ( ( s = strnstr(c,remainingLength,"|D" , 2) ) )
118  {
119  sscanf (s+2, "%d", &mp->reserved1);
120  }
121  else mp->reserved1 = 0;
122 
123  // extract message reserved2
124  if ( ( s = strnstr(c,remainingLength,"|E" , 2) ) )
125  {
126  sscanf (s+2, "%d", &mp->reserved2);
127  }
128  else mp->reserved2 = 0;
129 
130  // extract message reserved3
131  if ( ( s = strnstr(c,remainingLength,"|F" , 2) ) )
132  {
133  sscanf (s+2, "%d", &mp->reserved3);
134  }
135  else mp->reserved3 = 0;
136 
137  // extract message sourcecomponent
138  if ( ( s = strnstr(c,remainingLength,"|G" , 2) ) )
139  {
140  src = strnstr(c,remainingLength,"|>",2);
141  srcmsglen = (src - (s +2) );
142  strncpy(mp->sourcecomponent,s +2, srcmsglen);
143  mp->sourcecomponent[srcmsglen] = '\0';
144  // sscanf (s+2, "%s", &mp->sourcecomponent);
145  }
146  else
147  {
148  // mp->sourcecomponent = new char[strlen("ONLINE")+1 ];
149  strcpy(mp->sourcecomponent,"ONLINE");
150  }
151 
152  remainingLength = pos;
153  if ( ( c = strnstr(oBuffer,remainingLength,"|>" , 2) ) )
154  {
155  c+=2;
156  *length = ( pos - (int)(c - oBuffer) ) ;
157  }
158  else
159  {
160  c = oBuffer;
161  *length = pos ;
162  }
163  n = new char[pos+1];
164  s = n;
165  for (int i=0; i<*length; i++) *s++ = *c++;
166  n[pos] = '\0';
167  return n;
168 }
169 
171 {
172 
173 
174  struct timeb tp;
175  ftime(&tp);
176  char *timestr = ctime(&tp.time);
177  int length = strlen(timestr);
178 
179  // char timestr[128];
180  //sprintf(timestr, "%d", time(0));
181  //int length = strlen(timestr);
182 
183  char *cc = timestr;
184 
185 #if defined(LVL2_WINNT) || defined(STREAMBUF_NEW_IOSTREAM)
186 
187  for ( int ix =0; ix < length-1; ix++)
188  sb->sputc( *cc++ );
189 
190  sb->sputc( ':' );
191  sb->sputc( ' ' );
192 
193 #else
194  for ( int ix =0; ix < length-1; ix++)
195  sb->overflow( *cc++ );
196 
197  sb->overflow( ':' );
198  sb->overflow( ' ' );
199 #endif
200 }
201 
202 int printf (const char *format, ...)
203 {
204  va_list ap;
205  va_start (ap,format);
206  char x[1000];
207  vsprintf (x,format,ap);
208 
209  if (x[strlen(x)-1] == '\n') x[strlen(x)-1] = '\0';
210  COUT << x << ENDL;
211  return 0;
212 
213 }
214