Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:46

0001 
0002 #include "msg_buffer.h"
0003 #include <strnstr.h>
0004 
0005 // in the constructor we allocate the specified amount of bytes for
0006 // the output array.
0007 
0008 msg_buffer::msg_buffer(const int msglen)
0009 {
0010   maximum_position = msglen;
0011   oBuffer = new char[msglen];
0012   for (int i=0; i < msglen; i++)
0013     {
0014       oBuffer[i] = '\0';
0015     }
0016 
0017   pos =0; 
0018 
0019   m = new msg_control(0,0,0);
0020   m->activate();
0021 
0022 }
0023 
0024 // in the destructor, we deallocte the memory again
0025 msg_buffer::~msg_buffer()
0026 {
0027   delete [] oBuffer;
0028   if (m) 
0029     {
0030       m->deactivate();
0031       delete m;
0032     }
0033 }
0034 
0035 // this is the function which is called as a result of the << std::endl 
0036 // and flush() operations
0037 int msg_buffer::sync ()
0038 { 
0039   pos = 0;
0040   return 0;
0041 }
0042 
0043 
0044 
0045 // and this routine 
0046 int msg_buffer::overflow (int ch)
0047 { 
0048 
0049   if (pos >= maximum_position) // oops, our buffer is too small...
0050     {
0051       int oldmaximum_position=maximum_position;
0052       maximum_position += MSG_EXTENSION_AMOUNT;  // we extend it
0053       char * n = new char[maximum_position];
0054       for (int ix =0; ix < oldmaximum_position; ix++) n[ix] = oBuffer[ix];
0055       for(int ix=oldmaximum_position;ix<maximum_position;ix++)n[ix]='\0';
0056       delete [] oBuffer;      // get rid of too small array
0057       oBuffer = n;            // and put new array in its place
0058     }
0059 
0060   oBuffer[pos++] = ch;
0061 
0062   return 0;
0063 }
0064 
0065 // and this routine 
0066 char* msg_buffer::format(int * length,  msgProfile *mp)
0067 {
0068 
0069   char *c, *s, *n,*src;
0070   int remainingLength = pos;
0071   int srcmsglen = 0;
0072 
0073   if ( ! (c = strnstr(oBuffer,remainingLength,"<|",2) ) )
0074     {
0075       mp->type      = MSG_TYPE_DEFAULT;
0076       mp->source    = MSG_SOURCE_DEFAULT;
0077       mp->severity  = MSG_SEV_DEFAULT;
0078       mp->reserved1 = 0;
0079       mp->reserved2 = 0;
0080       mp->reserved3 = 0;
0081       //     mp->sourcecomponent = new char[strlen("ONLINE")+1 ];
0082       //      strcpy(mp->sourcecomponent,"ONLINE");
0083       strcpy(mp->sourcecomponent,"ONLINE");
0084       *length = pos;
0085       c = oBuffer;
0086       n = new char[pos+1]; 
0087       s = n;
0088       for (int i=0; i<*length; i++) *s++ = *c++;
0089       n[pos] = '\0';
0090       return n;
0091 
0092     }
0093   
0094   remainingLength = pos - (int) ( c-oBuffer);
0095   // extract message type
0096   if ( ( s =  strnstr(c,remainingLength,"|A" , 2) ) )
0097     {
0098       sscanf (s+2, "%d", &mp->type);
0099     }
0100   else mp->type = MSG_TYPE_DEFAULT;
0101 
0102   // extract message source
0103   if ( ( s =  strnstr(c,remainingLength,"|B" , 2) ) )
0104     {
0105       sscanf (s+2, "%d", &mp->source);
0106     }
0107   else mp->source = MSG_SOURCE_DEFAULT;
0108 
0109   // extract message severity
0110   if ( ( s =  strnstr(c,remainingLength,"|C" , 2) ) )
0111     {
0112       sscanf (s+2, "%d", &mp->severity);
0113     }
0114   else mp->severity = MSG_SEV_DEFAULT;
0115 
0116   // extract message reserved1
0117   if ( ( s =  strnstr(c,remainingLength,"|D" , 2) ) )
0118     {
0119       sscanf (s+2, "%d", &mp->reserved1);
0120     }
0121   else mp->reserved1 = 0;
0122 
0123   // extract message reserved2
0124   if ( ( s =  strnstr(c,remainingLength,"|E" , 2) ) )
0125     {
0126       sscanf (s+2, "%d", &mp->reserved2);
0127     }
0128   else mp->reserved2 = 0;
0129 
0130   // extract message reserved3
0131   if ( ( s =  strnstr(c,remainingLength,"|F" , 2) ) )
0132     {
0133       sscanf (s+2, "%d", &mp->reserved3);
0134     }
0135   else mp->reserved3 = 0;
0136 
0137   // extract message sourcecomponent
0138   if ( ( s =  strnstr(c,remainingLength,"|G" , 2) ) )
0139     {
0140       src = strnstr(c,remainingLength,"|>",2);
0141       srcmsglen = (src - (s +2) );
0142       strncpy(mp->sourcecomponent,s +2, srcmsglen);
0143       mp->sourcecomponent[srcmsglen] = '\0';
0144       //      sscanf (s+2, "%s", &mp->sourcecomponent);
0145     }
0146   else 
0147     {
0148       // mp->sourcecomponent = new char[strlen("ONLINE")+1 ];
0149       strcpy(mp->sourcecomponent,"ONLINE");
0150     }
0151 
0152   remainingLength = pos;
0153   if ( ( c =  strnstr(oBuffer,remainingLength,"|>" , 2) ) )  
0154     {
0155       c+=2;
0156       *length = ( pos - (int)(c - oBuffer) ) ;
0157     }
0158   else
0159     {
0160       c = oBuffer;
0161       *length =  pos  ;
0162     }
0163   n = new char[pos+1]; 
0164   s = n;
0165   for (int i=0; i<*length; i++) *s++ = *c++;
0166   n[pos] = '\0';
0167   return n;
0168 }
0169 
0170 void streambuf_add_date (STREAMBUF * sb)
0171 {
0172 
0173   
0174   struct timespec tp;
0175   clock_gettime( CLOCK_REALTIME, &tp);
0176   char *timestr = ctime(&tp.tv_sec);
0177   int length = strlen(timestr);
0178   
0179   //  char timestr[128];
0180   //sprintf(timestr, "%d", time(0));
0181   //int length = strlen(timestr);
0182 
0183   char *cc = timestr;
0184 
0185 #if defined(LVL2_WINNT) || defined(STREAMBUF_NEW_IOSTREAM)
0186 
0187   for ( int ix =0; ix < length-1; ix++) 
0188     sb->sputc( *cc++ );
0189   
0190   sb->sputc( ':' );
0191   sb->sputc( ' ' );
0192 
0193 #else
0194   for ( int ix =0; ix < length-1; ix++) 
0195     sb->overflow( *cc++ );
0196   
0197   sb->overflow( ':' );
0198   sb->overflow( ' ' );
0199 #endif
0200 }
0201 
0202 int printf (const char *format, ...)
0203 {
0204   va_list ap;
0205   va_start (ap,format);
0206   char x[1000];
0207   vsprintf (x,format,ap);
0208 
0209   if (x[strlen(x)-1] == '\n') x[strlen(x)-1] = '\0';
0210   COUT << x << ENDL;
0211   return 0;
0212 
0213 }
0214