summaryrefslogtreecommitdiff
path: root/src/Message/Message_Algorithm.cxx
blob: 5a4dba0f5871492f65abd163b655ab5801377d22 (plain)
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// File:      Message_Algorithm.cxx
// Created:   04.03.03 12:23:33
// Author:    Pavel TELKOV
// Copyright: Open CASCADE S.A. 2007
// The original implementation copyright (c) RINA S.p.A.

#include <Message_Algorithm.ixx>

#include <Message.hxx>
#include <Message_Msg.hxx>
#include <Message_MsgFile.hxx>
#include <Message_Messenger.hxx>
#include <Standard_AncestorIterator.hxx>
#include <TCollection_AsciiString.hxx>
#include <TColStd_SequenceOfInteger.hxx>
#include <TColStd_HSequenceOfInteger.hxx>
#include <TColStd_HSequenceOfHExtendedString.hxx>
#include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>

//=======================================================================
//function : SetMessenger
//purpose  :
//=======================================================================

Message_Algorithm::Message_Algorithm ()
{
  myMessenger = Message::DefaultMessenger();
}

//=======================================================================
//function : SetMessenger
//purpose  :
//=======================================================================

void Message_Algorithm::SetMessenger (const Handle(Message_Messenger)& theMsgr)
{
  if ( theMsgr.IsNull() )
    myMessenger = Message::DefaultMessenger();
  else
    myMessenger = theMsgr;
}

//=======================================================================
//function : SetStatus
//purpose  :
//=======================================================================

void Message_Algorithm::SetStatus(const Message_Status& theStat)
{
  myStatus.Set( theStat );
}

//=======================================================================
//function : SetStatus
//purpose  :
//=======================================================================

void Message_Algorithm::SetStatus (const Message_Status& theStat, 
				   const Standard_Integer theInt)
{
  // Set status flag
  SetStatus ( theStat );

  // Find index of bit corresponding to that flag
  Standard_Integer aFlagIndex = Message_ExecStatus::StatusIndex(theStat);
  if ( !aFlagIndex ) return;

  // Create map of integer parameters for a given flag, if not yet done
  if ( myReportIntegers.IsNull() )
    myReportIntegers = new TColStd_HArray1OfTransient (Message_ExecStatus::FirstStatus, 
                                                       Message_ExecStatus::LastStatus);
  Handle(Standard_Transient)& aData = 
    myReportIntegers->ChangeValue(aFlagIndex);
  if ( aData.IsNull() )
    aData = new TColStd_HPackedMapOfInteger;

  // add integer parameter for the status
  Handle(TColStd_HPackedMapOfInteger)::DownCast(aData)->ChangeMap().Add(theInt);
}

//=======================================================================
//function : SetStatus
//purpose  :
//=======================================================================

void Message_Algorithm::SetStatus (const Message_Status& theStat, 
				   const Handle(TCollection_HExtendedString) &theStr,
				   const Standard_Boolean noRepetitions)
{
  // Set status flag
  SetStatus ( theStat );
  if ( theStr.IsNull() )
    return;

  // Find index of bit corresponding to that flag
  Standard_Integer aFlagIndex = Message_ExecStatus::StatusIndex(theStat);
  if ( !aFlagIndex ) return;
    
  // Create sequence of string parameters for a given flag, if not yet done
  if ( myReportStrings.IsNull() )
    myReportStrings = new TColStd_HArray1OfTransient (Message_ExecStatus::FirstStatus,
						      Message_ExecStatus::LastStatus);
  Handle(Standard_Transient)& aData = 
    myReportStrings->ChangeValue(aFlagIndex);
  if ( aData.IsNull() )
    aData = new TColStd_HSequenceOfHExtendedString;

  // Add string parameter
  Handle(TColStd_HSequenceOfHExtendedString) aReportSeq = 
    Handle(TColStd_HSequenceOfHExtendedString)::DownCast(aData);
  if ( aReportSeq.IsNull() )
    return;
  if ( noRepetitions )
  {
    // if the provided string has been already registered, just do nothing
    for ( Standard_Integer i=1; i <= aReportSeq->Length(); i++ )
      if ( aReportSeq->Value(i)->String().IsEqual( theStr->String() ) ) 
	return;
  }

  aReportSeq->Append ( theStr );
}

//=======================================================================
//function : ClearStatus
//purpose  :
//=======================================================================

void Message_Algorithm::ClearStatus() 
{ 
  myStatus.Clear(); 
  myReportIntegers.Nullify();
  myReportStrings.Nullify();
}

//=======================================================================
//function : SendStatusMessages
//purpose  : 
//=======================================================================

void Message_Algorithm::SendStatusMessages (const Message_ExecStatus& theStatus,
					    const Message_Gravity theTraceLevel,
					    const Standard_Integer theMaxCount) const
{
  Handle(Message_Messenger) aMsgr = GetMessenger();
  if (aMsgr.IsNull()) return;

  TCollection_AsciiString aClassName ( DynamicType()->Name() );

  // Iterate on all set flags in the specified range
  for ( Standard_Integer i  = Message_ExecStatus::FirstStatus; 
                         i <= Message_ExecStatus::LastStatus; i++ )
  {
    Message_Status stat = Message_ExecStatus::StatusByIndex( i );
    if ( !theStatus.IsSet( stat ) || !myStatus.IsSet( stat ) )
      continue;

    // construct message suffix
    TCollection_AsciiString aSuffix;
    switch( Message_ExecStatus::TypeOfStatus( stat ) )
    {
    case Message_DONE:  aSuffix.AssignCat( ".Done" ); break;
    case Message_WARN:  aSuffix.AssignCat( ".Warn" ); break;
    case Message_ALARM: aSuffix.AssignCat( ".Alarm"); break;
    case Message_FAIL:  aSuffix.AssignCat( ".Fail" ); break;
    default:            continue;  
    }
    aSuffix.AssignCat( Message_ExecStatus::LocalStatusIndex( stat ) );

    // find message, iterating by base classes if necessary
    TCollection_AsciiString aMsgName = aClassName + aSuffix;
    Handle(Standard_Type) aType = DynamicType();
    while (Message_MsgFile::Msg(aMsgName).Length() == 0 && !aType.IsNull())
    {
      Standard_AncestorIterator it(aType);
      aType.Nullify();
      for (; it.More(); it.Next())
      {
        aType = it.Value();
        TCollection_AsciiString aClassName1 (aType->Name());
        TCollection_AsciiString aMsgName1 = aClassName1 + aSuffix;
        if (Message_MsgFile::Msg(aMsgName1).Length() != 0)
        {
          aMsgName = aMsgName1;
          break;
        }
      }
    }

    // create a message
    Message_Msg aMsg ( aMsgName );

    // if additional parameters are defined for a given status flag,
    // try to feed them into the message
    if ( ! myReportIntegers.IsNull() ) 
    {
      Handle(TColStd_HPackedMapOfInteger) aMapErrors =
	Handle(TColStd_HPackedMapOfInteger)::DownCast(myReportIntegers->Value(i));
      if (!aMapErrors.IsNull() ) 
        aMsg << PrepareReport ( aMapErrors, theMaxCount );
    }
    if ( ! myReportStrings.IsNull() && ! myReportStrings->Value(i).IsNull() ) 
    {
      Handle(TColStd_HSequenceOfHExtendedString) aReportSeq = 
        Handle(TColStd_HSequenceOfHExtendedString)::DownCast ( myReportStrings->Value(i) );
      if ( ! aReportSeq.IsNull() ) 
        aMsg << PrepareReport ( aReportSeq->Sequence(), theMaxCount );
    }

    // output the message
    aMsgr->Send(aMsg, theTraceLevel);
  }
}

//=======================================================================
//function : SendMessages
//purpose  : 
//=======================================================================

void Message_Algorithm::SendMessages (const Message_Gravity theTraceLevel,
				      const Standard_Integer theMaxCount) const
{
  Message_ExecStatus aStat;
  aStat.SetAllWarn();
  aStat.SetAllAlarm();
  aStat.SetAllFail();
  SendStatusMessages( aStat, theTraceLevel, theMaxCount );
}

//=======================================================================
//function : AddStatus
//purpose  : 
//=======================================================================

void Message_Algorithm::AddStatus
      (const Handle(Message_Algorithm)& theOtherAlgo)
{
  AddStatus( theOtherAlgo->GetStatus(), theOtherAlgo );
}

//=======================================================================
//function : AddStatus
//purpose  : 
//=======================================================================

void Message_Algorithm::AddStatus
      (const Message_ExecStatus& theAllowedStatus,
       const Handle(Message_Algorithm)& theOtherAlgo)
{
  // Iterate on all set flags in the specified range
  const Message_ExecStatus& aStatusOfAlgo = theOtherAlgo->GetStatus();
  for ( Standard_Integer i  = Message_ExecStatus::FirstStatus; 
                         i <= Message_ExecStatus::LastStatus; i++ )
  {
    Message_Status stat = Message_ExecStatus::StatusByIndex( i );
    if ( ! theAllowedStatus.IsSet( stat ) || ! aStatusOfAlgo.IsSet( stat ) )
      continue;

    SetStatus ( stat );

    // if additional parameters are defined for a given status flag,
    // move them to <this> algorithm
    // a) numbers
    Handle(TColStd_HPackedMapOfInteger) aNumsOther = 
      theOtherAlgo->GetMessageNumbers (stat); 
    if ( ! aNumsOther.IsNull() ) 
    {
      // Create sequence of integer parameters for a given flag, if not yet done
      if ( myReportIntegers.IsNull() )
	myReportIntegers =
	  new TColStd_HArray1OfTransient(Message_ExecStatus::FirstStatus, 
                                         Message_ExecStatus::LastStatus);
      Handle(Standard_Transient)& aData = 
	myReportIntegers->ChangeValue(i);
      if ( aData.IsNull() )
	aData = new TColStd_HPackedMapOfInteger;

      // add integer parameter for the status
      Handle(TColStd_HPackedMapOfInteger)::DownCast(aData)
	->ChangeMap().Unite(aNumsOther->Map());
    }
    // b) strings
    Handle(TColStd_HSequenceOfHExtendedString) aStrsOther = 
      theOtherAlgo->GetMessageStrings (stat); 
    if ( ! aStrsOther.IsNull() ) 
    {
      for (Standard_Integer n=1; n < aStrsOther->Length(); n++ )
	SetStatus (stat, aStrsOther->Value(n));
    }
  }
}

//=======================================================================
//function : GetMessageNumbers
//purpose  : 
//=======================================================================

Handle(TColStd_HPackedMapOfInteger) Message_Algorithm::GetMessageNumbers 
       (const Message_Status& theStatus) const
{
  if ( myReportIntegers.IsNull() )
    return 0;

  // Find index of bit corresponding to that flag
  Standard_Integer aFlagIndex = Message_ExecStatus::StatusIndex(theStatus);
  if ( ! aFlagIndex ) return 0;
    
  return Handle(TColStd_HPackedMapOfInteger)::DownCast(myReportIntegers->Value(aFlagIndex));
}

//=======================================================================
//function : GetMessageStrings
//purpose  : 
//=======================================================================

Handle(TColStd_HSequenceOfHExtendedString) Message_Algorithm::GetMessageStrings
       (const Message_Status& theStatus) const
{
  if ( myReportStrings.IsNull() )
    return 0;

  // Find index of bit corresponding to that flag
  Standard_Integer aFlagIndex = Message_ExecStatus::StatusIndex(theStatus);
  if ( ! aFlagIndex ) return 0;
    
  return Handle(TColStd_HSequenceOfHExtendedString)::DownCast(myReportStrings->Value(aFlagIndex));
}

//=======================================================================
//function : PrepareReport
//purpose  : static method
//=======================================================================

TCollection_ExtendedString Message_Algorithm::PrepareReport 
  (const Handle(TColStd_HPackedMapOfInteger)& theMapError,
   const Standard_Integer theMaxCount)
{
  TCollection_ExtendedString aNewReport;
  TColStd_MapIteratorOfPackedMapOfInteger anIt(theMapError->Map());
  Standard_Integer nb = 1;
  for (; anIt.More() && nb <= theMaxCount; anIt.Next(), nb++ )
  {
    if ( nb > 1 ) 
      aNewReport += " ";
    aNewReport += anIt.Key();
  }
 
  if ( anIt.More() )
  {
    aNewReport += " ... (total ";
    aNewReport += theMapError->Map().Extent();
    aNewReport += ")";
  }
  return aNewReport;
}

//=======================================================================
//function : PrepareReport
//purpose  : static method
//=======================================================================

TCollection_ExtendedString Message_Algorithm::PrepareReport 
  (const TColStd_SequenceOfHExtendedString& theReportSeq,
   const Standard_Integer theMaxCount)
{
  TCollection_ExtendedString aNewReport;
  Standard_Integer nb = 1;
  for ( ; nb <= theReportSeq.Length() && nb <= theMaxCount; nb++)
  {
    aNewReport += (Standard_CString)( nb > 1 ? ", \'" : "\'" );
    aNewReport += theReportSeq.Value(nb)->String();
    aNewReport += "\'";
  }

  if (theReportSeq.Length() > theMaxCount )
  {
    aNewReport += " ... (total ";
    aNewReport += theReportSeq.Length();
    aNewReport += ") ";
  }
  return aNewReport;
}