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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
#include <fstream><--- Skipping configuration 'DBL_MAX' since the value of 'DBL_MAX' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'DBL_MIN' since the value of 'DBL_MIN' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.<--- Skipping configuration 'M_PI' since the value of 'M_PI' is unknown. Use -D if you want to check it. You can use -U to skip it explicitly.
#include <iomanip>

#include "CubitMessage.hpp"
#include "CubitDefines.h"
#include "CubitString.hpp"
#include "CubitUtil.hpp"
#include <cassert>
#include <cstring>
#include <vector>
#include <iostream>

#include "SettingHandler.hpp"

#ifdef _WIN32
#define vsnprintf _vsnprintf
//#define strdup _strdup
#endif

int CubitMessage::errorCount = 0;
int CubitMessage::warningCount = 0;
//
// Message type usage:
// PRINT_ERROR:    Message to tell user why the task did not complete.
// PRINT_WARNING:  Message to tell user why completed task may not be what
//                 was requested.
// PRINT_INFO:     Message to tell user about status and progress.
// PRINT_DEBUG:    Message to developer tied to a global debug flag.
// DIAGNOSTIC:     Message to developer.


struct DefaultCubitMessageHandler : public CubitMessageHandler
{
  void print_message(const char* message)
  {
    std::cout << message;
    std::cout.flush();
  }
  void print_error(const char* message)
  {
#ifdef XTERM
    char esc = 0x1B;
    // Turn on reverse video on VT102 (xterm also)
    // (0=normal, 1-bold, 4-underscore, 5-blink, 7-inverse)
    std::cout << esc << '[' << '7' << 'm';
#endif

    std::cout << message;
    std::cout.flush();

#ifdef XTERM
    std::cout << esc << '[' << '0' << 'm';
    std::cout.flush();
#endif

  }
};

static DefaultCubitMessageHandler mDefaultHandler;


CubitMessage* CubitMessage::instance_ = NULL;
CubitMessageHandler* CubitMessage::mHandler = &mDefaultHandler;
CubitMessageErrorHandler* CubitMessage::mErrorHandler = NULL;
int CubitMessage::infoFlag = CUBIT_TRUE;
int CubitMessage::diagnosticFlag = CUBIT_FALSE;
int CubitMessage::warningFlag = CUBIT_TRUE;
int CubitMessage::errorFlag = CUBIT_TRUE;
std::ofstream* CubitMessage::loggingStream = NULL;
CubitString* CubitMessage::loggingFile = NULL;
std::ofstream* CubitMessage::loggingErrorStream = NULL;
CubitString* CubitMessage::loggingErrorFile = NULL;
int CubitMessage::expectedStartErrorCount = -1;
int CubitMessage::expectedEndErrorCount = -1;
bool CubitMessage::expectedLessErrorCountAccepted = false;

CubitMessage* CubitMessage::instance()
{
  if (!instance_)
  {
    instance_ = new CubitMessage;
    if (!instance_)
    {
	  std::cerr << " *** Unable to instantiate message object ***" << std::endl;
      exit(1);
    }
  }
  return instance_;
}

void CubitMessage::free_instance()
{<--- The function 'free_instance' is never used.
  if (instance_)
    delete instance_;
  instance_ = NULL;
}

CubitMessage::CubitMessage()
{
  infoFlag      = CUBIT_TRUE;
  warningFlag   = CUBIT_TRUE;
  errorFlag     = CUBIT_TRUE;
  diagnosticFlag= CUBIT_FALSE;
  loggingStream = NULL;
  loggingFile   = NULL;
  loggingErrorStream = NULL;
  loggingErrorFile   = NULL;
  currentDebugFlag = CUBIT_DEBUG_1;

    // Initialize the debugFlag array
  static MessageFlag staticDebugFlag[] =
  {
    MessageFlag(  0, "UNUSED"),
    MessageFlag(  1, "Previously used; available for reuse."),
    MessageFlag(  2, "Whisker weaving information"),
    MessageFlag(  3, "Timing information for 3D Meshing routines."),
    MessageFlag(  4, "Graphics Debugging (DrawingTool)"),
    MessageFlag(  5, "FastQ debugging"),
    MessageFlag(  6, "Submapping graphics debugging"),
    MessageFlag(  7, "Knife progress whisker weaving information"),
    MessageFlag(  8, "Mapping Face debug / Linear Programing debug "),
    MessageFlag(  9, "Paver Debugging"),
    MessageFlag( 10, "WW: removed hex seam flag"),
    MessageFlag( 11, "Nodeset Associativity debugging"),
    MessageFlag( 12, "Fastq activity"),
    MessageFlag( 13, "Mesh entities"),
    MessageFlag( 14, "Previously used; available for reuse."),
    MessageFlag( 15, "Previously used; available for reuse."),
    MessageFlag( 16, "Previously used; available for reuse."),
    MessageFlag( 17, "Use Count debugging"),
    MessageFlag( 18, "Webcut debugging"),
    MessageFlag( 19, "Feature Merge / Unmerge debugging"),
    MessageFlag( 20, "Parallel meshing activity"),
    MessageFlag( 21, "Boundary Layer Tool Debugging"),
    MessageFlag( 22, "ExodusMesh sizing function debugging"),
    MessageFlag( 23, "Draw after joining chords in WW"),
    MessageFlag( 24, "SelfCrossingLoop (and derivatives) debug info"),
    MessageFlag( 25, "Extra invalidity checking in WW"),
    MessageFlag( 26, "Surface Smoothing"),
    MessageFlag( 27, "Primal Construction debugging, see also flag 70"),
    MessageFlag( 28, "Plastering debugging"),
    MessageFlag( 29, "Volume SubMapping"),
    MessageFlag( 30, "Previously used; available for reuse."),
    MessageFlag( 31, "CleanUp debugging"),
    MessageFlag( 32, "Previously used; available for reuse."),
    MessageFlag( 33, "Whisker Weaving inside chord list face drawing"),
    MessageFlag( 34, "If on Whisker Weaving doesn't merge sheets"),
    MessageFlag( 35, "If on WW query displays sheets before joining chords"),
    MessageFlag( 36, "Enable/Disable idr_keyword_debugger function"),
    MessageFlag( 37, "Previously used; available for reuse."),
    MessageFlag( 38, "WW hex formation messages"),
    MessageFlag( 39, "Doublet Pillower graphics output"),
    MessageFlag( 40, "Previously used; available for reuse."),
    MessageFlag( 41, "Previously used; available for reuse."),
    MessageFlag( 42, "Auto vertex type and sweep verification"),
    MessageFlag( 43, "Programmer Errors for SubMapping"),
    MessageFlag( 44, "Submapping Graphics Debugging"),
    MessageFlag( 45, "Pillow Sheet debugging"),
    MessageFlag( 46, "Paver breakout detection (expensive)"),
    MessageFlag( 47, "Extra LP debugging (see flag 8 also)"),
    MessageFlag( 48, "Previously used; available for reuse."),
    MessageFlag( 49, "Draws Face by Face Creation in Paving"),
    MessageFlag( 50, "Debugging for AutoSchemeSelect"),
    MessageFlag( 51, "Previously used; available for reuse."),
    MessageFlag( 52, "User Interface: If flag is enabled, filenames being\n"
                "\t\t\t\tused for input will be echoed and each input\n"
                "\t\t\t\tline will be echoed prior to being parsed."),
    MessageFlag( 53, "Surface Morpher debugging"),
    MessageFlag( 54, "Parser debugging"),
    MessageFlag( 55, "Previously used; available for reuse."),
    MessageFlag( 56, "Previously used; available for reuse."),
    MessageFlag( 57, "Relative Interval/Length setting"),
    MessageFlag( 58, "StcVertex debugging of Whisker Weaving"),
    MessageFlag( 59, "Previously used; available for reuse."),
    MessageFlag( 60, "StcVertex debugging of Looping"),
    MessageFlag( 61, "List number of points used in curve faceting"),
    MessageFlag( 62, "Print verbose information on group operations"),
    MessageFlag( 63, "Label Whisker Weaving diagrams tersely"),
    MessageFlag( 64, "No label on Whisker Weaving diagrams"),
    MessageFlag( 65, "Volume Morpher debugging"),
    MessageFlag( 66, "Print debug information on importing Pro/E geometry"),
    MessageFlag( 67, "List number of triangles used in surface faceting"),
    MessageFlag( 68, "Previously used; available for reuse."),
    MessageFlag( 69, "Previously used; available for reuse."),
    MessageFlag( 70, "STC Pillowing, see also flag 27"),
    MessageFlag( 71, "Previously used; available for reuse."),
    MessageFlag( 72, "DoubletPillower text messages"),
    MessageFlag( 73, "Auto Surface debugging (use new auto surf select)"),
    MessageFlag( 74, "Feature-based decomposition info"),
    MessageFlag( 75, "Many-to-many sweep imprint debugging"),
    MessageFlag( 76, "Virtual point and partition curve"),
    MessageFlag( 77, "Volume interval matching"),
    MessageFlag( 78, "Tipton Smoother jacobian modification enabler"),
    MessageFlag( 79, "Previously used; available for reuse."),
    MessageFlag( 80, "Previously used; available for reuse."),
    MessageFlag( 81, "Curve Morpher Debugging"),
    MessageFlag( 82, "Previously used; available for reuse."),
    MessageFlag( 83, "Previously used; available for reuse."),
    MessageFlag( 84, "Surface auto decomposition"),
    MessageFlag( 85, "U-SubMapping debugging"),
    MessageFlag( 86, "Virtual curve and partition surface"),
    MessageFlag( 87, "Composite curve and composite surface"),
    MessageFlag( 88, "Volume partitioning"),
    MessageFlag( 89, "Previously used; available for reuse."),
    MessageFlag( 90, "Geometry attributes"),
    MessageFlag( 91, "Smoothing Debug Output"),
    MessageFlag( 92, "Print name changed warnings"),
    MessageFlag( 93, "Hex Fix Up"),
    MessageFlag( 94, "Entity name attribute"),
    MessageFlag( 95, "Group imprint errors"),
    MessageFlag( 96, "GraftTool debugging"),
    MessageFlag( 97, "Previously used; available for reuse."),
    MessageFlag( 98, "Color code imported THEX meshes"),
    MessageFlag( 99, "Geometry creation"),
    MessageFlag(100, "Skew Control debugging"),
    MessageFlag(101, "Previously used; available for reuse."),
    MessageFlag(102, "CAEntityId debugging"),
    MessageFlag(103, "Print compact interval assignment constraints"),
    MessageFlag(104, "Report interval matching progress"),
    MessageFlag(105, "Previously used; available for reuse."),
    MessageFlag(106, "Mesh Cleaver debugging"),
    MessageFlag(107, "Midpoint_subdivision debugging"),
    MessageFlag(108, "Simulog tetmesher debugging"),
    MessageFlag(109, "Transition schemes debugging"),
    MessageFlag(110, "Mesh Defined Geometry"),
    MessageFlag(111, "TriAdvance mesher debugging"),
    MessageFlag(112, "Auto Detail Suppression"),
    MessageFlag(113, "Previously used; available for reuse."),
    MessageFlag(114, "Blend Finder Debugging"),
    MessageFlag(115, "Exporting Feature Debugging Files"),
    MessageFlag(116, "Sizing function tool data information"),
    MessageFlag(117, "Extra Information on Autoscheme Decision Making"),
    MessageFlag(118, "Blend finding optimization file"),
    MessageFlag(119, "Laminate Tool debugging"),
    MessageFlag(120, "Print unassociated node locations on import mesh"),
    MessageFlag(121, "Print verbose infeasible match interval messages"),
    MessageFlag(122, "Mesh-Based Geometry Debug Information"),
    MessageFlag(123, "Collect memory statistics from Tetmesher"),
    MessageFlag(124, "Print verbose Tetmesher debugging information"),
    MessageFlag(125, "Mesh refinement debugging"),
    MessageFlag(126, "Previously used; available for reuse."),
    MessageFlag(127, "SculptingTool debug flag"),
    MessageFlag(128, "Previously used; available for reuse."),
    MessageFlag(129, "Virtual Imprint Debugging"),
    MessageFlag(130, "Hexsheet Insertion Debugging"),
    MessageFlag(131, "Mesh Cutting Debugging"),
    MessageFlag(132, "Global Collection Smoothing"),
    MessageFlag(133, "Print verbose import mesh progress"),
    MessageFlag(134, "Previously used; available for reuse."),
    MessageFlag(135, "Keep WhiskerWeave data after meshing"),
    MessageFlag(136, "Previously used; available for reuse."),
    MessageFlag(137, "GJoin"),
    MessageFlag(138, "Parallel CGM timing"),
    MessageFlag(139, "RTree Debugging"),
    MessageFlag(140, "Previously used; available for reuse."),
    MessageFlag(141, "Settings save/restore"),
    MessageFlag(142, "Decompose Sweep Debugging"),
    MessageFlag(143, "Decomp Sweep Imprint Debugging"),
    MessageFlag(144, "Medial Axis/Chordal Axis Debugging"),
    MessageFlag(145, "Virtual Geometry Facet Operations"),
    MessageFlag(146, "Sector Tool Meshing Scheme"),
    MessageFlag(147, "Previously used; available for reuse."),
    MessageFlag(148, "Meshing Benchmarks"),
    MessageFlag(149, "MeshCutting Graphical debugging"),
    MessageFlag(150, "MBG to Acis conversion debugging"),
    MessageFlag(151, "Previously used; available for reuse."),
    MessageFlag(152, "Boundary Conditions Debugging"),
    MessageFlag(153, "Print Body information in Geometry operations"),
    MessageFlag(154, "Split Surface Debugging"),
    MessageFlag(155, "Meshing Benchmarks Summary"),
    MessageFlag(156, "CAMAL Paver CleanUp debuging"),
    MessageFlag(157, "Skeleton Sizing Function timing and counts"),
    MessageFlag(158, "Previously used; available for reuse."),
    MessageFlag(159, "Previously used; available for reuse."),
    MessageFlag(160, "Previously used; available for reuse."),
    MessageFlag(161, "Previously used; available for reuse."),
    MessageFlag(162, "Previously used; available for reuse."),
    MessageFlag(163, "Previously used; available for reuse."),
    MessageFlag(164, "Previously used; available for reuse."),
    MessageFlag(165, "Previously used; available for reuse."),
    MessageFlag(166, "Unconstrained Paving debugging"),
    MessageFlag(167, "Skeleton Sizing Function Debugging (messages)"),
    MessageFlag(168, "Tweak Target Multiple Debugging "),
    MessageFlag(169, "Enable Knupp affine transformation instead of Roca"),
    MessageFlag(170, "Previously used; available for reuse."),
    MessageFlag(171, "Previously used; available for reuse."),
    MessageFlag(172, "Previously used; available for reuse."),
    MessageFlag(173, "Previously used; available for reuse."),
    MessageFlag(174, "Previously used; available for reuse."),
    MessageFlag(175, "Previously used; available for reuse."),
    MessageFlag(176, "Enable UCP database checking"),
    MessageFlag(177, "Enable Unconstrained Plastering Debug Drawing"),
    MessageFlag(178, "Enable Harris instead of Parrish hex refinement"),
    MessageFlag(179, "Enable Camal Sweeper for UCP Front Advancements.\n"
                "\t\t\t\tIgnored if debug 189 is on"),
    MessageFlag(180, "DecompAide (decomposition helper) debugging"),
    MessageFlag(181, "MBG.  Draw curve paths."),
    MessageFlag(182, "UCP Detailed Debug Printing."),
    MessageFlag(183, "Previously used; available for reuse."),
    MessageFlag(184, "Enable old sheet refinement command."),
    MessageFlag(185, "Enable straddle elements on hardlines."),
    MessageFlag(186, "Disable parametric coordinates in TriAdvMesher"),
    MessageFlag(187, "Previously used; available for reuse."),
    MessageFlag(188, "Tolerant Triangle Meshing"),
    MessageFlag(189, "Previously used; available for reuse."),
    MessageFlag(190, "Count CAMAL calls to move_to and normal_at"),
    MessageFlag(191, "Auto clean messages"),
    MessageFlag(192, "Previously used; available for reuse."),
    MessageFlag(193, "Tetmesh with stand-alone INRIA execuable thru files"),
    MessageFlag(194, "Hex Mesh Matching Debug drawing"),
    MessageFlag(195, "Previously used; available for reuse."),
    MessageFlag(196, "Previously used; available for reuse."),
    MessageFlag(197, "CAMAL Paver debugging (no Cubit smoothing, etc.)"),
    MessageFlag(198, "Auto Midsurface debugging"),
    MessageFlag(199, "Angle smoothing debugging"),
    MessageFlag(200, "Paver quality data output"),
    MessageFlag(201, "Paver cleanup edge metrics"),
    MessageFlag(202, "Disable Paver cleanup 3-3 replace"),
    MessageFlag(203, "Disable Paver cleanup 3-offset-3/5 replace"),
    MessageFlag(204, "Enable Paver cleanup 3-valent quad cluster"),
    MessageFlag(205, "Enable Paver cleanup partial chord collapse"),
    MessageFlag(206, "Hex Mesh Matching, match chords one at a time."),
    MessageFlag(207, "Defeature and Geometry tolerant meshing"),
    MessageFlag(208, "Previously used; available for reuse."),
    MessageFlag(209, "Change sense of partial/full tet remesh (v = !v)"),
    MessageFlag(210, "Use tetgen tetmesher via files"),
    MessageFlag(211, "Use tetgen tetmesher via direct interface"),
    MessageFlag(212, "Create debugging groups when doing geometry/meshing association for parallel refinement"),
    MessageFlag(213, "Previously used; available for reuse."),
    MessageFlag(214, "Boundary Layers"),
    MessageFlag(215, "Command Parser"),
    MessageFlag(216, "Command Parser Detailed"),
    MessageFlag(217, "Graphics debugging for unite dissimilar mesh command"),
    MessageFlag(218, "Previously used; available for reuse."),
    MessageFlag(219, "Materials Interface"),
    MessageFlag(220, "Previously used; available for reuse."),
    MessageFlag(221, "Turn ON Boundary Layer Correction in New Sweeper; ignored if target evaluator is NULL."),
    MessageFlag(222, "Turn ON Scale Mesh option to maximize percentage of model which gets scaled."),
    MessageFlag(223, "Turn ON Scale Mesh Debug Drawing."),
    MessageFlag(224, "Turn ON Scale Mesh Extended Debug Drawing."),
    MessageFlag(225, "unassigned")
    
      // IMPORTANT!!!
      // If you add a new debug flag, make sure that you change
      // the result of CubitMessage::number_of_debug_flags().
      // In order to use this type of static initialization,
      // we can't use the sizeof operator, so change it manually.
  };
  debugFlag = staticDebugFlag;

  // Check initialization of debugFlag array.
  for (int i=number_of_debug_flags(); i > 0; i--)
  {
    debugFlag[i].setting = CUBIT_FALSE;
    assert(i == debugFlag[i].flagNumber);
    assert(debugFlag[i].description != NULL);
  }
}

CubitMessage::~CubitMessage()
{
  // Close all streams associated with debug flags.
  // If the same stream is being used for debug and logging, it
  // will get closed below.
  for (int i=number_of_debug_flags(); i > 0; i--)
    remove_debug_stream(i);

  // At this time, the only open streams possible are loggingStream and loggingErrorStream.
  if (loggingStream != NULL)
  {
    loggingStream->close();
    delete loggingStream;
    delete loggingFile;
  }
  if (loggingErrorStream != NULL)
  {
    loggingErrorStream->close();
    delete loggingErrorStream;
    delete loggingErrorFile;
  }

  // Set static instance_ to zero to indicated that we are dead.
  instance_ = 0;
}

void CubitMessage::delete_instance()
{
  delete instance_;
  instance_ = NULL;
}

int CubitMessage::number_of_debug_flags()
{
  return NUM_DEBUG_FLAGS;
//  return sizeof(debugFlag)/sizeof(debugFlag[0])-1;
}

void CubitMessage::internal_error ( const int message_type,
                                    std::ofstream *output_stream,
                                    const CubitString& msgbuf)
{
  int print_it = CUBIT_FALSE;

  CubitString prefix;

  switch (message_type)
  {
    case CUBIT_ERROR:
      if (errorFlag)
      {
        print_it = CUBIT_TRUE;
        prefix = "ERROR: ";
      }
      break;
    case CUBIT_ERROR_EXPECTED:
      if (errorFlag)
      {
        print_it = CUBIT_TRUE;
        prefix = "ERROR_EXPECTED: ";
      }
      break;
    case CUBIT_WARNING:
      if (warningFlag)
      {
        print_it = CUBIT_TRUE;
        prefix = "WARNING: ";
      }
      break;
    case CUBIT_INFO:
      if (infoFlag)
        print_it = CUBIT_TRUE;
      break;
    case CUBIT_DIAGNOSTIC:
      if (diagnosticFlag)
      {
        print_it = CUBIT_TRUE;
        prefix = "DIAGNOSTIC: ";
      }
      break;
    default:
      if (message_type >= CUBIT_DEBUG_1 && message_type <= number_of_debug_flags()+10)
      {
        if (debugFlag[message_type-10].setting) print_it = CUBIT_TRUE;
        break;
      }
  }

  if (print_it)
  {
      // loggingStream is used to journal error, warning, and info messages.
      // debug messages can also be journalled there by setting the
      // output stream for the debug flag to the same file.
    if (loggingStream != NULL && (message_type == CUBIT_ERROR ||
                                  message_type == CUBIT_WARNING ||
                                  message_type == CUBIT_INFO))
    {
      *loggingStream << prefix.c_str() << msgbuf.c_str();
      loggingStream->flush();
    }
      //loggingErrorStream is used to (if the user has requested it)
      // log only ERROR: messages
    if (loggingErrorStream != NULL && message_type == CUBIT_ERROR)
    {
      *loggingErrorStream << prefix.c_str() << msgbuf.c_str();
      loggingErrorStream->flush();
    }

    if (output_stream == NULL)
    {
      if(message_type == CUBIT_ERROR)
      {
        CubitString ctx;
        if(CubitMessage::mErrorHandler)
        {
          ctx = CubitMessage::mErrorHandler->error_context().c_str();
        }
        CubitString msg = prefix + ctx + msgbuf;
        CubitMessage::mHandler->print_error(msg.c_str());
      }
      else
      {
        CubitString msg = prefix + msgbuf;
        CubitMessage::mHandler->print_message(msg.c_str());
      }
    }
    else
    {
      *output_stream << prefix.c_str() << msgbuf.c_str();
      output_stream->flush();
    }
  }
}

int CubitMessage::print_error ( const CubitString& str )
{
  int error_type = CUBIT_ERROR;
  if(expectedStartErrorCount != -1 && expectedEndErrorCount > expectedStartErrorCount)
  {
    error_type = CUBIT_ERROR_EXPECTED;
  }
  else
  {
    add_to_error_count();
  }

  if(expectedStartErrorCount != -1)
  {
    expectedStartErrorCount++;
  }

  static char* cubit_ctest = getenv("CUBIT_CTEST");
  if(cubit_ctest && error_type == CUBIT_ERROR)
  {
    internal_error(CUBIT_INFO, NULL, "<DartMeasurement name=\"Error\" type=\"text/plain\">\n");
    internal_error(error_type, NULL, str);
    internal_error(CUBIT_INFO, NULL, "</DartMeasurement>\n");
  }

  internal_error(error_type, NULL, str);

  return CUBIT_FAILURE;
}

int CubitMessage::print_warning ( const CubitString& str )
{
  internal_error(CUBIT_WARNING, NULL, str);
  add_to_warning_count();
  return CUBIT_FAILURE;
}

int CubitMessage::print_info ( const CubitString& str )
{
  internal_error(CUBIT_INFO, NULL, str);
  return CUBIT_FAILURE;
}

int CubitMessage::is_debug_flag_set( int flag )
{
   if( DEBUG_FLAG( flag ))
   {
      currentDebugFlag = flag;
      return CUBIT_TRUE;
   }
   return CUBIT_FALSE;
}

int CubitMessage::print_debug( const CubitString& str )
{
  internal_error(currentDebugFlag+10,
                 debugFlag[currentDebugFlag].outputStream,
                 str);
  return CUBIT_FAILURE;
}

void CubitMessage::print_diagnostic ( const CubitString& str )
{<--- The function 'print_diagnostic' is never used.
  internal_error(CUBIT_DIAGNOSTIC, NULL, str);
}

int CubitMessage::reset_error_count(int value)
{<--- The function 'reset_error_count' is never used.
  int current_value = errorCount;
  if (errorCount != value) {
    errorCount = value;
    PRINT_WARNING("Error count manually changed from %d to %d\n\n",
		  current_value, value);
  }
  return current_value;
}

int CubitMessage::error_count()
{
  return errorCount;
}

void CubitMessage::add_to_error_count()
{
  errorCount++;
}

int CubitMessage::reset_warning_count(int value)
{<--- The function 'reset_warning_count' is never used.
  int current_value = warningCount;
  if (warningCount != value) {
    warningCount = value;
    PRINT_INFO("Warning count manually changed from %d to %d\n\n",
		  current_value, value);
  }
  return current_value;
}

int CubitMessage::warning_count()
{<--- The function 'warning_count' is never used.
  return warningCount;
}

void CubitMessage::add_to_warning_count()
{
  warningCount++;
}

void CubitMessage::output_debug_information(int from, int to, int step)
{
  if (to == -1)
    to = number_of_debug_flags();

  PRINT_INFO("Debug Flag Settings "
	     "(flag number, setting, output to, description):\n");
   for (int i=from; i <= to; i+=step) {
      debugFlag[i].output();
   }
  PRINT_INFO("\n");
}

void CubitMessage::output_debug_information(CubitString &match)
{
  int count = 0;
  for (int i=1; i <= number_of_debug_flags(); i++) {
    char *tmp = CubitUtil::util_strdup((char*)(debugFlag[i].description));
    if (tmp && strlen(tmp) > 0) {
      CubitString debug_description(tmp);
      debug_description.to_lower();
      if (debug_description.find(match, 0) < debug_description.length()) {
	if (count == 0) {
	  PRINT_INFO("Debug Flag Settings "
		     "(flag number, setting, output to, description):\n");
	}
	debugFlag[i].output();
	count++;
      }
    }
    CubitUtil::util_strdup_free(tmp);
  }
  if (count == 0) {
    PRINT_WARNING("No debug descriptions contain the "
		  "substring '%s'\n", match.c_str());
  }
  PRINT_INFO("\n");
}

void CubitMessage::output_logging_information()
{<--- The function 'output_logging_information' is never used.
  if (loggingStream != NULL)
     PRINT_INFO("logging           = On, log file = '%s'\n", loggingFile->c_str());
  else
     PRINT_INFO("logging           = Off\n");
  if (loggingErrorStream != NULL)
     PRINT_INFO("logging Errors    = On, log file = '%s'\n",loggingErrorFile->c_str());

}

void MessageFlag::output()
{
  CubitMessage::instance()->
    print_info(
        CubitString::format("%2d  %3s  %-16s   %s\n",
               flagNumber, (setting == 1 ? "ON " : "OFF"),
               (filename == NULL ? "terminal" : filename->c_str()),
               description)
        );
}

int CubitMessage::find_file_use(const CubitString &filename)
{
  if (filename == "terminal") {
    // remove_debug_stream has set the outputStream and filename to NULL.
    return -1;
  }

  // See if any of the other debug flags have this file open
  for (int i=number_of_debug_flags(); i > 0; i--) {
    if (debugFlag[i].filename && *(debugFlag[i].filename) == filename) {
      return i;
    }
  }
  if (loggingFile && *(loggingFile) == filename)
    return -2;

  if (loggingErrorFile && *(loggingErrorFile) == filename)
    return -3;

  return 0;
}

int CubitMessage::count_stream_users(const std::ofstream *stream)
{
  int match = 0;
  if (stream != NULL)
  {
    for (int i=number_of_debug_flags(); i > 0; i--)
    {
      if (debugFlag[i].outputStream == stream)
      {
        match++;
      }
    }

    if (loggingStream == stream)
      match++;
    if (loggingErrorStream == stream)
       match++;
  }
  return match;
}

void CubitMessage::set_logging_file_setting(const CubitString &filename, CubitBoolean resume_flag)
{<--- The function 'set_logging_file_setting' is never used.
  // If logging is currently outputting to a file, close it if
  // it is the only thing using that file. (and the filenames don't match)
  if (loggingFile && *loggingFile == filename)
    return;

  if (loggingErrorFile && *loggingErrorFile == filename)
  {
    PRINT_ERROR("Can't set the logging file to be the same as the Error logging file.\n");
    return;
  }

  int users = count_stream_users(loggingStream);
  if (users == 1) { // Just us...
    loggingStream->close();
    delete loggingStream;
    loggingStream = NULL;
    delete loggingFile;
    loggingFile = NULL;
  }

  int match = find_file_use(filename);

  if (match == -1) // Filename is 'terminal'
    return;
  else if (match != 0)
  {
    loggingFile   = debugFlag[match].filename;
    loggingStream = debugFlag[match].outputStream;
  }
  else
  {
    loggingFile   = new CubitString(filename);
    if(resume_flag)
       loggingStream = new std::ofstream(CubitString::toNative(filename).c_str(), std::ios::out | std::ios::app);
    else
       loggingStream = new std::ofstream(CubitString::toNative(filename).c_str());
  }
}

void CubitMessage::set_debug_file_setting(const int index, const CubitString &filename)
{<--- The function 'set_debug_file_setting' is never used.
  // If this flag is currently outputting to a file, close it if
  // this is the only flag using that file.
  remove_debug_stream(index);

  int match = find_file_use(filename);

  if (match == -1) // Filename is 'terminal'
    return;
  if (match == -2 || match == -3) {// Filename is same as loggingFile or loggingErrorFile;
    debugFlag[index].filename = loggingFile;
    debugFlag[index].outputStream = loggingStream;
  }
  else if (match == index)
    return;
  else if (match != 0) {
    debugFlag[index].filename = debugFlag[match].filename;
    debugFlag[index].outputStream = debugFlag[match].outputStream;
  }
  else {
    debugFlag[index].filename = new CubitString(filename);
    debugFlag[index].outputStream = new std::ofstream(CubitString::toNative(filename).c_str());
  }
}

void CubitMessage::remove_debug_stream(const int index)
{
  // NOTE: DO NOT USE PRINT_* CALLS, THIS IS CALLED FROM DESTRUCTOR.

  // Multiple debug flags may be using the same output stream,
  // Go through the list and count who is using this stream,
  // If only one use, close and delete the stream.
  if (debugFlag[index].outputStream == NULL)
    return;

  int match = count_stream_users(debugFlag[index].outputStream);

  if (match == 1) {
    debugFlag[index].outputStream->close();
    delete debugFlag[index].outputStream;
    delete debugFlag[index].filename;
  }
  debugFlag[index].filename = NULL;
  debugFlag[index].outputStream = NULL;
}

void CubitMessage::set_message_handler(CubitMessageHandler *handler)
{<--- The function 'set_message_handler' is never used.
  CubitMessage::mHandler = handler;
  if(CubitMessage::mHandler == NULL)
    CubitMessage::mHandler = &mDefaultHandler;
}

CubitMessageHandler* CubitMessage::get_message_handler()
{<--- The function 'get_message_handler' is never used.
  return CubitMessage::mHandler;
}

void CubitMessage::set_error_handler(CubitMessageErrorHandler *handler)
{<--- The function 'set_error_handler' is never used.
  CubitMessage::mErrorHandler = handler;
}

CubitMessageErrorHandler* CubitMessage::get_error_handler()
{<--- The function 'get_error_handler' is never used.
  return CubitMessage::mErrorHandler;
}

MessageFlag::MessageFlag(int flag_number, const char *desc)
    : flagNumber(flag_number), setting(CUBIT_FALSE),
      description(desc), filename(NULL), outputStream(NULL)
{
}

MessageFlag::MessageFlag()
{
  flagNumber   = 0;
  setting      = CUBIT_FALSE;
  description  = NULL;
  filename     = NULL;
  outputStream = NULL;
}


void CubitMessage::set_logging_file_setting(const char* filename)
{
  if (loggingFile && *loggingFile == filename)
     return;

  if (CubitUtil::compare(filename,"terminal")) { // Filename is 'terminal'
    if (loggingStream != NULL) {
      loggingStream->close();
      delete loggingStream;
      loggingStream = NULL;
      delete loggingFile;
      loggingFile = NULL;
    }
    return;
  }
  else {
    loggingFile   = new CubitString(filename);
    loggingStream = new std::ofstream(CubitString::toNative(filename).c_str(), std::ios::out | std::ios::app );
  }
}

void CubitMessage::set_error_logging_file_setting(const char* filename, CubitBoolean resume_flag)
{<--- The function 'set_error_logging_file_setting' is never used.
  if (loggingErrorFile && *loggingErrorFile == filename)
     return;

  if(loggingFile && *loggingFile == filename)
  {
    PRINT_ERROR("Can't explicitly set the Error logging file to be the same as the logging file.\n");
    return;
  }
 
  if (CubitUtil::compare(filename,"terminal")) { // Filename is 'terminal'
    if (loggingErrorStream != NULL) {
      loggingErrorStream->close();
      delete loggingErrorStream;
      loggingErrorStream = NULL;
      delete loggingErrorFile;
      loggingErrorFile = NULL;
    }
    return;
  }
  else {
    loggingErrorFile   = new CubitString(filename);
    if(resume_flag)
        loggingErrorStream = new std::ofstream(CubitString::toNative(filename).c_str(), std::ios::out | std::ios::app );
    else
        loggingErrorStream = new std::ofstream(CubitString::toNative(filename).c_str());
  }
}

//Initialize all settings in this class
void CubitMessage::initialize_settings()
{

  SettingHandler::instance()->add_setting("Info",
                                          CubitMessage::set_info_flag,
					  CubitMessage::get_info_flag);

  /*SettingHandler::instance()->add_setting("Logging",
                                          CubitMessage::set_logging_file_setting,
					  CubitMessage::get_logging_file_setting);*/

  SettingHandler::instance()->add_setting("Diagnostic",
					 CubitMessage::set_diagnostic_flag,
					 CubitMessage::get_diagnostic_flag);

  SettingHandler::instance()->add_setting("Warning",
					  CubitMessage::set_warning_flag,
					  CubitMessage::get_warning_flag);
}

CubitString CubitMessage::logging_filename() const
{<--- The function 'logging_filename' is never used.
  CubitString temp_string;
  if(loggingStream != NULL)
    temp_string = loggingFile->c_str();

  return temp_string;
}

CubitString CubitMessage::logging_errors_filename() const
{<--- The function 'logging_errors_filename' is never used.
  CubitString temp_string;
  if(loggingErrorStream != NULL)
     temp_string = loggingErrorFile->c_str();

  return temp_string;
}

void CubitMessage::start_expected_error_count(int error_count, bool less_than_accepted)
{<--- The function 'start_expected_error_count' is never used.
  this->expectedStartErrorCount = errorCount;
  this->expectedEndErrorCount = errorCount + error_count;
  this->expectedLessErrorCountAccepted = less_than_accepted;
}

void CubitMessage::stop_expected_error_count(const CubitString &message)
{<--- The function 'stop_expected_error_count' is never used.
  bool has_error = false;
  if((expectedEndErrorCount != expectedStartErrorCount && expectedLessErrorCountAccepted == false) ||
     (expectedEndErrorCount < expectedStartErrorCount && expectedLessErrorCountAccepted == true))
  {
    has_error = true;
  }
  this->expectedStartErrorCount = -1;
  this->expectedEndErrorCount = -1;
  if(has_error)
  {
    CubitString msg = message;
    if(message.length() == 0)
    {
      msg = "unexpected errors";
    }
    print_error(msg + "\n");
  }
}

MessageFlag::~MessageFlag()
{
  // It is not safe to delete either the stream or the filename here
  // since multiple instances (debug flags) may refer to the same memory
}

#ifdef STANDALONE
void main() {
CubitMessage::instance()->output_debug_information(1, 10, 2);
CubitMessage::instance()->output_debug_information(12);
CubitMessage::instance()->set_debug_file(5, "Debug_Test.file");
DEBUG_FLAG(5, CUBIT_TRUE);
PRINT_DEBUG_5("This is a test\n");
CubitMessage::instance()->output_debug_information(5,5);
}
#endif