DebStream.cc 23.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
// (C) Copyright 2014 by Autodesk, Inc.
//
// The information contained herein is confidential, proprietary
// to Autodesk,  Inc.,  and considered a trade secret as defined
// in section 499C of the penal code of the State of California.
// Use of  this information  by  anyone  other  than  authorized
// employees of Autodesk, Inc.  is granted  only under a written
// non-disclosure agreement,  expressly  prescribing  the  scope
// and manner of such use.

11
#include "DebUtils.hh"
12
13
14

#ifdef DEB_ON

15
16
17
18
19
#include <string>
#include <fstream>
#include <time.h>
#include <vector>
#include <iostream>
20
#include <map>
21

22
23
namespace { // LOCAL_PROC
bool is_html_filename(const char* const str)
24
{
25
  if (str == nullptr) return false;
26
  const char* dot = strrchr(str, '.');
27
28
  if (dot == nullptr) return false;
  ++dot;
29
  return (!strncmp(dot, "htm", 3)) || (!strncmp(dot, "HTM", 3)) ;
30
}
31
}
32

33
34
35
namespace ReForm {

class FunctionCallSequence
36
{
37
38
39
40
41
42
  std::string func_name_;
  std::vector<DebEnter*> debs_;  // These may not have sequential counts when multithreaded.

public:
  FunctionCallSequence(const char* _func_name, DebEnter* _deb)
    : func_name_(_func_name)
43
  {
44
45
46
    debs_.push_back(_deb);
  }
  ~FunctionCallSequence() {}
47

48
49
50
  bool add(const char* _func_name, DebEnter* _deb)
  {
    if (func_name_ == _func_name)
51
    {
52
      debs_.push_back(_deb);
53
      return true;
54
    }
55
56
    return false;
  }
57

58
59
60
  bool pop()
  {
    if (debs_.size() > 1)
61
    {
62
63
      debs_.pop_back();
      return true;
64
    }
65
66
67
    debs_.clear();
    return false;
  }
68

69
70
71
72
73
  int number_calls() const
  {
    if (!this) return 0;
    return (int)debs_.size();
  }
74

75
76
77
78
79
80
  int count(int i = 0) const
  {
    int num = number_calls();
    if (i < num) return debs_[num - 1 - i]->count_;
    return -1;
  }
81

82
83
84
85
  const char* name() const
  {
    return func_name_.c_str();
  }
86

87
88
89
90
91
92
  // Replace interior of < > in function name with . for brevity
  void compact_name(std::string& str) const
  {
    int cnt = 0;
    const char* ptr = func_name_.c_str();
    while (ptr && (*ptr != '\0'))
93
    {
94
95
96
97
      char c = *ptr;
      if (c == '>') --cnt;
      if (cnt == 0) str.append(1, c);
      if (c == '<')
98
      {
99
100
        if (cnt == 0) str.append(".");
        ++cnt;
101
      }
102
      ++ptr;
103
    }
104
  }
105

106
107
108
109
110
111
112
  // Get single call stack element string
  void get(std::string& _str, const bool _strip_angled, bool _with_counts) const
  {
    if (_strip_angled) compact_name(_str);
    else _str.append(name());
    _str.append("[");
    if (_with_counts)
113
    {
114
115
116
117
118
119
120
      int num = number_calls();
      int prev = -2;
      int seq_cnt = 0;
      for (int i = 0; i < num; ++i)
      {
        int cnt = debs_[i]->count_;
        if (cnt != prev + 1)
121
        {
122
          char buffer[64];
123

124
125
126
127
          if (seq_cnt > 0)
          {
            _str.append("-");
            sprintf_s(buffer, sizeof(buffer), "%i", prev);
128
            _str.append(buffer);
129
          }
130
131
132
133
134
135
136
137
138
139
140
141
142
143
          if (i > 0) _str.append(",");
          sprintf_s(buffer, sizeof(buffer), "%i", cnt);
          _str.append(buffer);
          seq_cnt = 0;
        }
        else
          ++seq_cnt;
        prev = cnt;
      } // endfor i
    } // endif _with_counts
    else
      _str.append("*");
    _str.append("]");
  } // endfunc get
144

145
  void get_indent(std::string& _str, DebFile* _dfile, bool _is_html);
146

147
}; // endclass FunctionCallSequence
148
149


150
151
152
153
154
155
156
class CallStack
{
  std::vector<FunctionCallSequence> calls_;
  int depth_;
public:
  CallStack() : depth_(0) {}
  ~CallStack() {}
157

158
159
160
161
162
163
  void add(const char* _func_name, DebEnter* _deb)
  {
    if (calls_.empty() || !calls_.back().add(_func_name, _deb))
      calls_.push_back(FunctionCallSequence(_func_name, _deb));
    ++depth_;
  }
164

165
166
167
168
169
170
  void pop()
  {
    if (!calls_.back().pop())
      calls_.pop_back();
    --depth_;
  }
171

172
173
174
175
176
177
  const FunctionCallSequence* call(int _up = 0)
  {
    int num = (int)calls_.size();
    if (_up < num) return &calls_[num - 1 - _up];
    return nullptr;
  }
178

179
180
181
182
183
  // Read a particular call stack element
  bool read(int _up, const char*& _funcname, int& _count)
  {
    const FunctionCallSequence* fcs = call(_up);
    if (fcs != nullptr)
184
    {
185
186
187
      _funcname = fcs->name();
      _count = fcs->count(0); // Return most recent deb_enter_count
      return true;
188
    }
189
190
    return false;
  }
191

192
193
194
195
196
197
  // Get a full call stack sting.
  // returns number of call stack function elements
  int get(std::string& _str, bool _with_counts = true) const
  {
    int num = (int)calls_.size();
    for (int i = 0; i < num; ++i)
198
    {
199
200
      if (i > 0) _str.append("->");
      calls_[i].get(_str, true, _with_counts);
201
    }
202
203
    return num;
  }
204

205
206
207
208
  int depth() const
  {
    return depth_;
  }
209

210
  bool get_indent(std::string& _str, DebFile* _dfile, const bool is_html);
211
}; // endclass CallStack
212

213
214


215
class DebFile
216
217
{
private:
218
219
220
221
222
  class module_stats
  {
  public:
    int lev_; // Level set for this module
    int col_; // Colour (24 bit RGB, MSB=Red) for this module's DEB_out
223
    module_stats(int _lev = 0) : lev_(_lev), col_(0x808080) {}
224
225
226
227
228
229
    ~module_stats() {}
  };

  std::map<std::string, module_stats> module_map_;
  typedef std::map<std::string, module_stats>::iterator module_map_itr;
  typedef std::map<std::string, module_stats>::const_iterator const_module_map_itr;
230

231
  DebStream::StreamType type_;
232
233
  int lev_;
  int num_flush_;
234
  int priority_; // Last permission granted
235
  int indent_size_;
236

237
238
  bool at_line_start_;

239
240
241
242
  std::string current_;
  std::string output_;
  std::string file_name_;
  std::fstream file_stream_;
243
244

  std::string double_format_;
245
  //std::string indent_string_;
246
  DebStream* deb_stream_;
247
248
249
250
  CallStack call_stack_;

public:

251
252
253
254
  CallStack& call_stack()
  {
    return call_stack_;
  }
255

256
257
  bool is_kept_open() const
  {
258
    return 0 != (type_ & DebStream::StreamType::KEEP_OPEN);
259
260
261
  }
  bool is_html() const
  {
262
    return 0 != (type_ & DebStream::StreamType::HTML);
263
264
265
  }
  bool is_retained() const
  {
266
    return 0 != (type_ & DebStream::StreamType::RETAIN);
267
268
269
  }
  bool is_appended() const
  {
270
    return 0 != (type_ & DebStream::StreamType::APPEND);
271
  }
272
  // Only applies to HTML DEB_out
273
274
275
276
277
278
279
280
  bool is_white_on_black() const
  {
    return true;
  }
  int indent_size()
  {
    return indent_size_;
  }
281

282
283
284
285
286
287
288
289
  bool file_is_open() const
  {
    return file_stream_.is_open();
  }
  int priority() const
  {
    return priority_;
  }
290

291
292
293
294
295
296
297
298
  bool fork_to_cout()
  {
    return false;
  }
  bool fork_to_cerr()
  {
    return true;
  }
299
300


301
  const char* file_name() const
302
  {
303
    if (this && (!file_name_.empty())) return file_name_.c_str();
304
305
    return nullptr;
  }
306

307
308
309
310
311
312
  void clear()
  {
    current_.clear();
    output_.clear();
    file_name_.clear();
  }
313

314
315
316
317
318
319
320
  char prev_char() const
  {
    if (!current_.empty()) return current_.back();
    if (!output_.empty()) return output_.back();
    return '\0';
  }

321

322
  void indent(bool _full_text)
323
  {
324
    std::string str;
325
    if (call_stack().get_indent(str, this,  _full_text && is_html()))
326
      current_.append(str);
327
328
  }

329

330
  void line_break(bool _with_indent = false)
331
  {
332
    if (is_html()) current_.append("<br>");   // Don't bother with matching </br>
333
334
    current_.append("\n", 1);

335
336
    if (_with_indent) indent(false);
    else at_line_start_ = true;
337

338
339
  }

340
341
342
343
344
345
  void set_level(int _lev)
  {
    lev_ = _lev;
  }

  void print_direct(const std::string& _s)
346
347
348
349
350
  {
    current_.append(_s);
  }


351
352
  void print(const char _c)
  {
353
    if (_c == '\n')
354
355
356
357
358
    {
      line_break();
      return;
    }

359
360
    if (at_line_start_)
    {
361
      indent(true);
362
363
364
365
      at_line_start_ = false;
    }


366
367
368
    if (is_html())
    {
      // translate the esoteric characters used in IGM DEB_out
369

370
      if (_c == '')  // -62
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
        return;

      if (_c == '') // 167 = -89
      {
        current_.append("&sect;");
        return;
      }

      if (_c == '') // -80
      {
        current_.append("&deg;");
        return;
      }
    }
    current_.append(&_c, 1);
386
387
  }

388
  void print(const char* const _s, bool _fork = true)
389
390
391
  {
    if (_s != nullptr)
    {
392
      for (int i = 0; ; ++i)
393
394
      {
        const char c = _s[i];
395
        if (c == '\0') break;
396
397
398
399
        print(c);
      }
      if (_fork)
      {
400
401
402
403
        if (fork_to_cout())
          std::cout << _s;
        if (fork_to_cerr())
          std::cerr << _s;
404
      }
405
    }
406
407
408
409
410
411
412
413
  }

  void print(int _i)
  {
    char buffer[64];
    sprintf_s(buffer, sizeof(buffer), "%i", _i);
    print(buffer);
  }
414

415

416
  const char* double_format() const
417
418
419
420
421
422
  {
    if (double_format_.empty())
      return "%.17g";
    return double_format_.c_str();
  }

423
  void set_double_format(const char* const str)
424
  {
425
    if (str == nullptr)
426
427
428
429
430
431
      double_format_.clear();
    else
      double_format_ = str;
  }


432
433
434
  void print(double _d)
  {
    char buffer[64];
435
    sprintf_s(buffer, sizeof(buffer), double_format(), _d);
436
437
438
    print(buffer);
  }

439
440
  void print(const DebCommand& _co)
  {
441
442
    switch (_co.com())
    {
443
    case DebCommand::END :
444
      if (is_html()) print_direct("</FONT>");
445
      break;
446
    case DebCommand::END_ERR :
447
448
449
    // Powerdown DEB_error font
    // if (is_html()) print_direct("</BLINK>");
    // fall through
450
    case DebCommand::END_LF :
451
      if (is_html()) print_direct("</FONT>");
452
453
454
455
456
      line_break();
      break;
    }
  }

457
458
459
460
  DebStream& stream()
  {
    return *deb_stream_;
  }
461

462
463
  // Append current asctime to given string
  bool add_time(std::string& str)
464
465
  {
    time_t rawtime;
466
    time(&rawtime);
467
    struct tm timeinfo;
468
    errno_t err = localtime_s(&timeinfo, &rawtime);
469
470
471
472
473
474
    if (err == 0)
    {
      char buffer[256];
      err = asctime_s(buffer, sizeof(buffer), &timeinfo);
      if (err == 0)
      {
475
        str.append(buffer);
476
477
478
479
480
481
        return true;
      }
    }
    return false;
  }

482

483
#if 1
484
  bool hover(std::string& _str, const std::string& _hover, const bool _open)
485
486
487
488
  {
    if (is_html())
    {
      char buffer[1024];
489
490
      if (_open)  sprintf_s(buffer, sizeof(buffer),
                              "<span title=\"%s\">", _hover.c_str());
491
492
493
494
495
496
497
498
      else sprintf_s(buffer, sizeof(buffer), "</span>");
      _str.append(buffer);
      return true;
    }
    return false;
  }
#endif

499
  bool anchor(std::string& _str, const int _id, const char* _tag, const bool _open)
500
501
502
503
504
505
506
507
508
509
510
511
  {
    if (is_html())
    {
      char buffer[1024];
      if (_open)  sprintf_s(buffer, sizeof(buffer), "<A name=\"%08X_%s\">", _id, _tag);
      else sprintf_s(buffer, sizeof(buffer), "</A>");
      _str.append(buffer);
      return true;
    }
    return false;
  }

512
  bool link_to(std::string& _str, const int _id, const char* _tag, const std::string& _hover, const bool _open)
513
514
515
516
517
518
519
520
521
522
  {
    if (is_html())
    {
      char buffer[2048];
      if (_open)
      {
        // HTML title hover text is cropped to 64 char in Firefox but displays
        // OK in Chrome. We could use javascript to avoid this limit but HTML
        // is simpler.
        if (_hover.empty()) sprintf_s(buffer, sizeof(buffer),
523
524
525
                                        "<A href=\"#%08X_%s\">", _id, _tag);
        else sprintf_s(buffer, sizeof(buffer),
                         "<A href=\"#%08X_%s\" title=\"%s\">", _id, _tag, _hover.c_str());
526
527
528
529
530
531
532
533
534
      }
      else sprintf_s(buffer, sizeof(buffer), "</A>");
      _str.append(buffer);
      return true;
    }
    return false;
  }


535
536
537
  void header(std::string& str)
  {
    if (is_html())
538
539
540
541
542
543
544
545
546
    {
      str.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\">");
      str.append("\n<HTML><HEAD>");
      str.append("\n<TITLE>ReForm DEB_out");
      str.append("</TITLE>");
      // javascript lib loads go here
      // stylesheet loads go here
      // within HEAD javascript goes here
      str.append("\n</HEAD>");
547
      if (is_white_on_black())
548
      {
549
        str.append("\n<BODY BGCOLOR=\"#000000\" TEXT=\"#FFFFFF\" LINK=\"#%00FFFF\" VLINK=\"#FFFF00\" >");
550
        //str.append( "\n<BODY BGCOLOR=\"#000000\" TEXT=\"#FFFFFF\"  >");
551
552
553
554
      }
      else
      {
        str.append("\n<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\" LINK=\"#%FF0000\" VLINK=\"#0000FF\" >");
555
        //str.append( "\n<BODY BGCOLOR=\"#000000\" TEXT=\"#FFFFFF\" >");
556
557
558
559
560
561
      }
      str.append("\n");
    } // endif is_html
    bool date_header = true;
    if (date_header)
    {
562
      if (!file_name_.empty())
563
564
565
566
      {
        str.append(file_name_);
        str.append(" opened ");
      }
567
      add_time(str);
568
      str.append("[ Build: " __TIME__  " " __DATE__  "] ");
569
570
      if (is_html()) str.append("<BR>");
      str.append("\n");
571
    }
572
   }
573
574
575
576
577
578

  void footer()
  {
    bool date_footer = true;
    if (date_footer)
    {
579
      std::string str("\n");
580
      if (!file_name_.empty()) str.append(file_name_);
581
      str.append(" Closed: ");
582
583
      add_time(str);
      stream() << str << "\n";
584
585
586
    }

    if (is_html())
587
      stream().print("\n</BODY></HTML>", false);
588
  }
589

590
591
592
593
  bool is_first_flush()
  {
    return num_flush_ == 0 ;
  }
594

595
596
597
  int flush()
  {
    int res = 0;
598
    if (!current_.empty())
599
    {
600
601
      const char* fname = file_name();
      if ((fname != nullptr) || file_is_open())
602
      {
603
        if (!file_is_open())
604
        {
605
606
607
          file_stream_.open(fname,
                            std::fstream::out | ((is_appended() && !is_first_flush()) ?
                                                 std::fstream::app  :  std::fstream::trunc));
608
609
610
611
612
        }

        if (file_stream_.is_open())
        {
          std::string hdr;
613
614
615
          if (!is_appended())
          {
            // Reoutput entire file
616
617
618
619
620
621
            header(hdr);
            output_.append(hdr);
            file_stream_ << output_;
          }
          else
          {
622
            if (is_first_flush())
623
624
            {
              header(hdr);
625
626
              if (is_retained())
                output_.append(hdr);
627
628
629
630
631
632
              file_stream_ << hdr;
            }
            ++num_flush_;
          }
          file_stream_ << current_;

633
          if (is_retained())
634
635
            output_.append(current_);

636
637
          current_.clear();
          if (!is_kept_open())
638
639
640
641
642
643
            file_stream_.close();
        } // endif fs.is_open
        else
          res = -1;
      }
    } // endif current empty
644
    return res;
645
646
  }  // endfunc flush

647
648
649
650
  // Use with extreme caution.
  const char * string_out() const
 { return current_.c_str(); }

651
652
653
654
655
656
657
  void close()
  {
    footer();
    flush();
  }


658
  void set_file_name(const char* _name)
659
  {
660
    file_name_ = _name ? _name : "";
661
    if (is_html_filename(_name))
662
      type_ = (DebStream::StreamType)(type_ | DebStream::StreamType::HTML);
663
  }
664
665

  void set_module_level(const char* const _module, const int _lev)
666
  {
667
668
669
670
671
672
    if (this)
    {
      std::pair<module_map_itr, bool> ins =  module_map_.insert(
          std::pair<std::string, module_stats>(_module, module_stats(_lev)));
      if (!ins.second) ins.first->second.lev_ = _lev;
    }
673
674
  }

675
  int module_level(const char* const _module) const
676
  {
677
    if (!this) return -1;
678
679
680
681
682
    const_module_map_itr it = module_map_.find(std::string(_module));
    if (it == module_map_.end()) return lev_;
    return it->second.lev_;
  }

683
  void set_module_color(const char* const _module, const int _col)
684
  {
685
686
687
688
689
690
691
    if (this)
    {
      std::pair<module_map_itr, bool> ins =  module_map_.insert(
          std::pair<std::string, module_stats>(_module, module_stats(lev_)));
      ins.first->second.col_ = _col;
    }

692
693
  }

694
  int get_module_color(const char* const _module) const
695
696
  {
    const_module_map_itr it = module_map_.find(std::string(_module));
697
698
699
700
701
    if (it == module_map_.end())
    {
      if (is_white_on_black()) return 0xFFFFFF;
      else return 0x000000;
    }
702
703
    return it->second.col_;
  }
704
705

  int permission(const int _lev, const int _warn, const char* const _module)
706
707
  {
    _warn;
708
    int lev = module_level(_module);
709
    lev -= _lev;
710
    if (lev < 0) lev = 0;
711
712
713
    if (lev > 0) priority_ = lev;
    return lev;
  }
714
715

  bool is_at_line_start()
716
  {
717
    return at_line_start_;
718
719
720
721
722
  }


  DebFile(
    DebStream* _deb_stream,
723
    DebStream::StreamType _type = (DebStream::StreamType)(DebStream::APPEND | DebStream::RETAIN),
724
725
    const char* _file_name = nullptr) :
    type_(_type), lev_(5), deb_stream_(_deb_stream), num_flush_(0)
726
727
  {
    set_file_name(_file_name);
728
    indent_size_ = 3;
729
    at_line_start_ = false; // Don't want to indent header
730
731
    //    indent_string_ = ".";
    set_module_color("PARA", 0xFF8000);
732
733
    set_module_color("SOLV", 0x00FF00);
    set_module_color("NSLV", 0xFFFF00);
734
    set_module_color("FELD", 0x0080FF);
735
    set_module_color("CURV", 0x00FFFF);
736
737
    set_module_color("QMGN", 0xFF00FF);
    // Don't set a TEST module colour here because Test is not part of Reform
738
739
  }

740
741
742
743
744
  ~DebFile() 
  {
    // The closing of the primary DEB_out file is a good place to flush and close
    // any subsidary DEB files
  }
745

746

747
}; // endclass DebFile
748
749
750
751
752

// =====================================
//      DebEnter member funcs
// =====================================

753
DebEnter::DebEnter(const char* const _funcname, const int _count, const char* const _module)
754
{
755
  // TODO: this might have to be atomic
756
  static int id_cnt = 0;
757
  module_ = _module;
758
  deb_outs_ = 0;
759
  deb_lines_ = 0;
760
  id_ = ++id_cnt;
761
762
  count_ = _count;
  stream(0, false).dfile()->call_stack().add(_funcname, this);
763
764
}

765
DebEnter::~DebEnter()
766
{
767
  DebFile* impl = stream(0, false).dfile();
768
  impl->call_stack().pop();
769

770
  std::string str;
771
  if (((deb_outs_ > 0) || (deb_lines_ > 0)) && impl->anchor(str, id_, "exit", true))
772
  {
773
    impl->anchor(str, id_, "exit", false);
774
775
    impl->print_direct(str);
  }
776
777
}

778
DebStream& DebEnter::stream(const int _warn, const bool _print)
779
{
780
781
  DebStream& ds = ReForm::DebStream::get_global(_warn);
  DebFile* impl = ds.dfile();
782

783
  if (_print)
784
  {
785
786
    if (impl->is_html())
    {
787
      bool is_deb_error = (_warn == 2);
788
      // DEB_error font powerup goes here. BLINK is deprecated sadly.
789
      // if (is_deb_error)  impl->print_direct("<BLINK>");
790
      int col = impl->get_module_color(module_);
791
      if (is_deb_error) col = 0xFF0000; // RED
792
      char buffer[256];
793
794
      sprintf_s(buffer, sizeof(buffer), "<FONT COLOR=\"#%06X\" SIZE=%i>",
                col, impl->priority() + 1);
795
      impl->print_direct(buffer);
796
797
    }

798
    if (deb_outs_ < 1)
799
800
    {
      // First DEB_out in this function so output callstack, and flush.
801
      impl->indent(true);
802
803
804
805
      std::string str;
      bool is_html = impl->is_html();
      if (is_html)
      {
806
        str.append("<FONT SIZE=2><u>");
807
        impl->anchor(str, id_, "enter", true);
808
      }
809
      else
810
811
      {
        // .txt call stack lead in
812
813
814
        str.append("****>");
      }

815
      impl->call_stack().get(str);
816
      if (is_html) str.append("</u></FONT>");
817
818
      impl->print_direct(str.c_str()); // Don't fork callstack to cerr etc.
      impl->line_break();
819
      ds.dfile()->flush();
820
821
822
823
824
825
826
    }
    ++deb_outs_;
  }

  return ds;
}

827
int DebEnter::permission(const int _lev, const int _warn)
828
{
829
830
  int res = ReForm::DebStream::get_global().dfile()->permission(_lev, _warn, module_);
  return res;
831
832
}

833

834
void FunctionCallSequence::get_indent(std::string& _str, DebFile* _dfile, bool _is_html)
835
836
{
  int num = number_calls();
837
838
839
  for (int i = 0; i < num; ++i)
  {
    DebEnter* deb = debs_[i];
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
    if (_is_html)
    {
      /* HTML indent element is <L> with span title the name and count
      of the function and with < linking to the entry anchor (if present) and
      > linking to the exit anchor (will be present).
      L is the first letter of the module name */
      char hovert[1024];
      sprintf_s(hovert, sizeof(hovert), "%s[%i]", func_name_.c_str(), deb->count_);
      int col = _dfile->get_module_color(deb->module_);
      char buffer[1024];
      sprintf_s(buffer, sizeof(buffer), "<FONT COLOR=\"#%06X\">.", col);

      _dfile->hover(_str, std::string(hovert), true);

      _str.append(buffer);
      std::string cstk;
      //impl->call_stack().get(cstk);
857
      if ((deb->deb_outs_ > 0) && _dfile->link_to(_str, deb->id_, "enter", cstk, true))
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
      {
        _str.append("&lt;");
        _dfile->link_to(_str, deb->id_, "enter", cstk,  false);
      }
      else _str.append("&lt;");

      _str.append(deb->module_, 1);

      if (_dfile->link_to(_str, deb->id_, "exit", cstk, true))
      {
        _str.append("&gt;");
        _dfile->link_to(_str, deb->id_, "exit", cstk,  false);
        ++deb->deb_lines_;
      }

      _dfile->hover(_str, std::string(hovert), false);

      _str.append("</FONT>");
    } // endif html
    else _str.append("  ");
  }
}

881
bool CallStack::get_indent(std::string& _str, DebFile* _dfile, const bool is_html)
882
883
884
885
886
887
888
889
890
891
{

  if (_dfile->indent_size() == 0) return false;
  if (is_html)
  {
    char buffer[64];
    sprintf_s(buffer, sizeof(buffer), "<FONT SIZE=%i>", _dfile->indent_size());
    _str.append(buffer);
  }
  int num = (int)calls_.size();
892
893
  int i0 = 0;
  if (!is_html) ++i0; // Don't waste whitespace on first level indent if .txt
894
  for (int i = i0; i < num; ++i)
895
    calls_[i].get_indent(_str, _dfile,  is_html);
896
897
898
899
900
  if (is_html) _str.append(":&nbsp;</FONT>\n");
  return true;
}


901
902
903
904
905
// =====================================
//        DebStream member funcs
// =====================================


906
DebStream::DebStream(const char* _file_name, StreamType _type)
907
908
{
  // NB. disable DEB_out over this new if new contains DEB_out
909
  dfile_ = new DebFile(this, _type, _file_name);
910
911
912
913
}

DebStream::~DebStream()
{
914
  if (dfile_ != nullptr)
915
  {
916
917
    dfile()->close();
    dfile()->clear();
918
    // NB. disable DEB_out over this delete if delete contains DEB_out
919
920
    delete dfile_;
    dfile_ = nullptr;
921
922
923
  }
}

924
925
926
927

const char * DebStream::string_out() const 
{ return dfile()->string_out();}

928
929
DebStream& DebStream::print(const int _i)
{
930
  dfile_->print(_i);
931
932
933
934
935
  return *this;
};

DebStream& DebStream::print(const double _d)
{
936
  dfile_->print(_d);
937
938
939
  return *this;
};

940
DebStream& DebStream::print(const char* _s, bool _fork)
941
{
942
  dfile_->print(_s, _fork);
943
944
945
946
947
  return *this;
};

DebStream& DebStream::print(const char _c)
{
948
949
950
951
  dfile_->print(_c);
  return *this;
};

952
DebStream& DebStream::print(const DebCommand& _co)
953
954
{
  dfile_->print(_co);
955
956
957
  return *this;
};

958
DebStream& DebStream::get_global(int _warn)
959
{
960
  _warn;
961
  // TODO: Replace with a Singleton?? ThreadArray??
962
#if 1
963
  static DebStream g_ds__("reform_deb_out.txt");
964
965
966
#else
  static DebStream g_ds__("reform_deb_out.htm");
#endif
967
968
969
  return g_ds__;
}

970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
DebStream& operator<<(DebStream& _ds, const int _i)
{
  return _ds.print(_i);
}
DebStream& operator<<(DebStream& _ds, const double _d)
{
  return _ds.print(_d);
}
DebStream& operator<<(DebStream& _ds, const char* const _s)
{
  return _ds.print(_s);
}
DebStream& operator<<(DebStream& _ds, const char _c)
{
  return _ds.print(_c);
}

DebStream& operator<<(DebStream& _ds, const size_t _i)
988
{
989
  return _ds.print((int)_i);
990
}
991
DebStream& operator<<(DebStream& _ds, const unsigned int _i)
992
{
993
  return _ds.print((int)_i);
994
}
995
DebStream& operator<<(DebStream& _ds, const float _f)
996
{
997
  return _ds.print((double)_f);
998
}
999
DebStream& operator<<(DebStream& _ds, const std::string& _s)
1000
{
1001
  return _ds.print(_s.c_str());
1002
}
1003

1004
DebStream& operator<<(DebStream& _ds, const DebCommand& _co)
1005
{
1006
  return _ds.print(_co);
1007
}
1008
1009
1010
1011
1012

// ==================================================
//  DebController member funcs (currently all static)
// ==================================================

1013
void DebController::set_module_level(const char* _mod , const int _lev)
1014
{
1015
  DebStream::get_global().dfile()->set_module_level(_mod, _lev);
1016
}
1017
1018


1019
void DebController::set_double_format(const char* _fmt)
1020
{
1021
  DebStream::get_global().dfile()->set_double_format(_fmt);
1022
}